Submission #10696

# Submission time Handle Problem Language Result Execution time Memory
10696 2014-11-07T12:11:17 Z gs14004 Orchard (NOI14_orchard) C++
25 / 25
376 ms 20628 KB
#include <cstdio>
#include <cstdlib>
#include <algorithm>
using namespace std;

int n,m,fcnt;
int* a[155];
int* s[155];

int ca[1000005];

int sum(){
    int me = *max_element(ca+1,ca+m+1);
    if(me < 0) return -me;
    int mv = 0, c = 0;
    for (int j=1; j<=m; j++) {
        c += ca[j];
        if(c < 0) c = 0;
        mv = max(mv,c);
    }
    return -mv;
}

int calc(int x, int y){
    int hi = y - x + 1;
    for (int j=1; j<=m; j++) {
        ca[j] = 2 * (s[y][j] - s[y][j-1] - s[x-1][j] + s[x-1][j-1]) - hi;
    }
    return fcnt + sum();
}

int main(){
    scanf("%d %d",&n,&m);
    for (int i=0; i<=n; i++) {
        a[i] = (int*)malloc(sizeof(int) * (m + 1));
        s[i] = (int*)malloc(sizeof(int) * (m + 1));
    }
    for (int i=0; i<=m; i++) {
        s[0][i] = 0;
    }
    for (int i=0; i<=n; i++) {
        s[i][0] = 0;
    }
    for (int i=1; i<=n; i++) {
        for (int j=1; j<=m; j++) {
            scanf("%d",&a[i][j]);
            s[i][j] = s[i][j-1] + s[i-1][j] - s[i-1][j-1] + a[i][j];
        }
    }
    fcnt = s[n][m];
    int res = 1e9;
    for (int i=1; i<=n; i++) {
        for (int j=i; j<=n; j++) {
            res = min(res,calc(i,j));
        }
    }
    printf("%d",res);
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 4996 KB Output is correct
2 Correct 0 ms 4996 KB Output is correct
3 Correct 0 ms 4996 KB Output is correct
4 Correct 0 ms 4996 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 5172 KB Output is correct
2 Correct 0 ms 5172 KB Output is correct
3 Correct 0 ms 5172 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 120 ms 20628 KB Output is correct
2 Correct 104 ms 20628 KB Output is correct
3 Correct 120 ms 20628 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 28 ms 7348 KB Output is correct
2 Correct 28 ms 7348 KB Output is correct
3 Correct 20 ms 7348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 4996 KB Output is correct
2 Correct 12 ms 5128 KB Output is correct
3 Correct 8 ms 5128 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 356 ms 10880 KB Output is correct
2 Correct 376 ms 10880 KB Output is correct
3 Correct 360 ms 10880 KB Output is correct