BUPT 202 chocolate mashine

北邮多校联合赛H
BUPT 202 chocolate mashine

[Description]

There is a Vending machine sales which sell magic chocolate in Renren office. The chocolate is shaped like a n*m rectangle, and

each 1*1 unit is assigned with a delicious degree. A consumer can chose a Staircase-shaped chocolate (whose bottom and right edge must be a straight line) to buy, if there isn't

any units' delicious degree is less than K. And the price of this block is equal to the sum of delicious degree it contains.

Chould you help the manager to figer out how much can he earn at most?

          *

       ***

  ******

*******

This is a sample of a Staircase-shaped chocolate. Noting that a rectangle is also a Special case of Staircase-shaped

chocolate.

[Input]

The first line contains a positive integer: the number of test cases, at most 100.

After that per test case:

One line with three integers n, m and K (1 ≤ n, m ≤ 1 000, K ≤ 1000000): the dimensions of chocolate.

Next n lines, each with m positive integers(less than 1000000). The jth colume of the ith line represents the delicious

degree of i*j unit.

[Output]

.....

[Sample input]

1

2 3 2

2 3 4

2 2 1

[Sample output]

9

[Hint]

We can sold:

2 3

2 2

or

2 3 4


#include <stdio.h>
#include <stdlib.h>

int N, M, K;
int choc[1001][1001];
int sum[1001][1001];
int frow[1001];
int fvol[1001];

int main(){
	int i, j, k ,Q;
	long long ans;
	scanf("%d", &Q);
	while(Q--){
		scanf("%d%d%d", &N, &M, &K);
		for (j = 0; j <= M; j++){
			sum[0][j] = 0;
		}
		for (i = 1; i <= N; i++){
			frow[i] = 1;
			sum[i][0] = 0;
			for (j = 1; j <= M; j++){
				scanf("%d", &choc[i][j]);
				sum[i][j] = sum[i][j - 1] + choc[i][j];
			}
		}
		
		ans = fvol[0] = 0;
		for (j = 1; j <= M; j++){
			frow[0] = j + 1;
			for (i = 1; i <= N; i++){
				fvol[i] = 0;
				if (choc[i][j] < K){
					frow[i] = j + 1;
					continue;
				}
				k = i - 1;
				while(frow[i] > frow[k] && k > 0){
					fvol[i] += sum[k][j] - sum[k][frow[i] - 1];
					k--;
				}
				fvol[i] += fvol[k] + sum[i][j] - sum[i][frow[i] - 1];
				if (ans < fvol[i])
					ans = fvol[i];
			}
		}
		printf("%d\n", ans);
	}
	return 0;
}
/**************
2011.7.16北邮多校联合赛H
暴力扫描过
sum[i][j] = sum{choc[i][1] ~ choc[i][j]};
frow[i][j] : (i,j)在第i行往左最远能到的列
fvol[i][j] : 以(i,j)为右下角的斜阵最大值

北邮网上题手抖,打错N,M范围,实则只有1000...
导致数次MLE...
***************/

你可能感兴趣的:(BUPT 202 chocolate mashine)