Submission #47347

# Submission time Handle Problem Language Result Execution time Memory
47347 2018-05-01T06:54:04 Z dqhungdl Orchard (NOI14_orchard) C++17
25 / 25
360 ms 16364 KB
#include <bits/stdc++.h>
using namespace std;

int m,n,res=1e9,c0[1000005],c1[1000005],s[2][155][5005],s1[2][3][1000005];

int Sum(int t,int u1,int v1,int u2,int v2)
{
    return s[t][u2][v2]-s[t][u1-1][v2]-s[t][u2][v1-1]+s[t][u1-1][v1-1];
}

void Sub1()
{
    int x;
    for(int i=1;i<=m;i++)
        for(int j=1;j<=n;j++)
        {
            cin>>x;
            s[0][i][j]+=s[0][i-1][j]+s[0][i][j-1]-s[0][i-1][j-1]+(x==0);
            s[1][i][j]+=s[1][i-1][j]+s[1][i][j-1]-s[1][i-1][j-1]+(x==1);
        }
    for(int i=1;i<=m;i++)
        for(int j=i;j<=m;j++)
        {
            int add=Sum(1,1,1,i-1,n)+Sum(1,j+1,1,m,n);
            for(int t=1;t<=n;t++)
            {
                c0[t]=c0[t-1]+Sum(0,i,t,j,t);
                c1[t]=c1[t-1]+Sum(1,i,t,j,t);
            }
            int minn=1e9;
            for(int t=n;t>=1;t--)
            {
                minn=min(minn,c0[t]-c1[t]);
                res=min(res,c1[t-1]+c1[n]-c0[t-1]+minn+add);
            }
        }
    cout<<res;
}

int Sum1(int t,int u1,int v1,int u2,int v2)
{
    return s1[t][u2][v2]-s1[t][u1-1][v2]-s1[t][u2][v1-1]+s1[t][u1-1][v1-1];
}

void Sub2()
{
    int x;
    for(int i=1;i<=m;i++)
        for(int j=1;j<=n;j++)
        {
            cin>>x;
            s1[0][i][j]+=s1[0][i-1][j]+s1[0][i][j-1]-s1[0][i-1][j-1]+(x==0);
            s1[1][i][j]+=s1[1][i-1][j]+s1[1][i][j-1]-s1[1][i-1][j-1]+(x==1);
        }
    for(int i=1;i<=m;i++)
        for(int j=i;j<=m;j++)
        {
            int add=Sum1(1,1,1,i-1,n)+Sum1(1,j+1,1,m,n);
            for(int t=1;t<=n;t++)
            {
                c0[t]=c0[t-1]+Sum1(0,i,t,j,t);
                c1[t]=c1[t-1]+Sum1(1,i,t,j,t);
            }
            int minn=1e9;
            for(int t=n;t>=1;t--)
            {
                minn=min(minn,c0[t]-c1[t]);
                res=min(res,c1[t-1]+c1[n]-c0[t-1]+minn+add);
            }
        }
    cout<<res;
}

int main()
{
    ios_base::sync_with_stdio(false);
    //freopen("TEST.INP","r",stdin);
    cin>>m>>n;
    if(m<=150&&n<=5000)
        Sub1();
    else
        Sub2();
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 440 KB Output is correct
4 Correct 2 ms 468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 800 KB Output is correct
2 Correct 3 ms 876 KB Output is correct
3 Correct 3 ms 876 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 103 ms 16364 KB Output is correct
2 Correct 97 ms 16364 KB Output is correct
3 Correct 99 ms 16364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 20 ms 16364 KB Output is correct
2 Correct 24 ms 16364 KB Output is correct
3 Correct 21 ms 16364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 16364 KB Output is correct
2 Correct 13 ms 16364 KB Output is correct
3 Correct 13 ms 16364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 327 ms 16364 KB Output is correct
2 Correct 325 ms 16364 KB Output is correct
3 Correct 360 ms 16364 KB Output is correct