Submission #411295

# Submission time Handle Problem Language Result Execution time Memory
411295 2021-05-25T00:53:04 Z ScarletS Nafta (COI15_nafta) C++17
34 / 100
1000 ms 93360 KB
#include <bits/stdc++.h>
using namespace std;

const int N = 2003;
int n,m,dp[N][N],C[N],ol[N][N],p[N][N],ct[N],l,r,t;
char s[N][N];
bitset<N> done[N];
vector<array<int,2>> d = {{0,-1},{0,1},{-1,0},{1,0}};

bool ok(int x, int y)
{
    if (0<x&&0<y&&x<=n&&y<=m&&s[x][y]!='.'&&!done[x][y])
        return 1;
    return 0;
}

void dfs(int x, int y)
{
    done[x][y]=1;
    l=min(l,y);
    r=max(r,y);
    t+=(s[x][y]-'0');
    for (auto i : d)
        if (ok(x+i[0],y+i[1]))
            dfs(x+i[0],y+i[1]);
}

void solve(int i, int l, int r, int optL, int optR)
{
    if (l>r)
        return;
    int m=l+(r-l)/2,best=-1,k=-1;
    for (int j=optL;j<=m;++j)
        if (best<dp[i-1][j-1]+C[m]-ol[j-1][m])
        {
            best=dp[i-1][j-1]+C[m]-ol[j-1][m];
            k=j;
        }
    dp[i][m]=best;
    solve(i,l,m-1,optL,k);
    solve(i,m+1,r,k,optR);
}

int main()
{
    ios_base::sync_with_stdio(0); cin.tie(0);
    cin>>n>>m;
    for (int i=1;i<=n;++i)
        for (int j=1;j<=m;++j)
            cin>>s[i][j];
    for (int i=1;i<=n;++i)
        for (int j=1;j<=m;++j)
            if (ok(i,j))
            {
                t=0;
                l=j;
                r=j;
                dfs(i,j);
                ct[l]+=t;
                ct[r+1]-=t;
                p[l][l]+=t;
                p[l][r+1]-=t;
            }
    for (int i=1;i<=m;++i)
    {
        ct[i]+=ct[i-1];
        C[i]+=ct[i];
    }
    for (int i=1;i<=m;++i)
        for (int j=i;j<=m;++j)
        {
            p[i][j]+=p[i][j-1];
            ol[i][j]=p[i][j]+ol[i-1][j];
        }
    for (int i=1;i<=m;++i)
    {
        solve(i,1,m,1,m);
        for (int j=2;j<=m;++j)
            dp[i][j]=max(dp[i][j-1],dp[i][j]);
        cout<<dp[i][m]<<"\n";
    }
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 972 KB Output is correct
2 Correct 1 ms 972 KB Output is correct
3 Correct 1 ms 1100 KB Output is correct
4 Correct 1 ms 972 KB Output is correct
5 Correct 1 ms 972 KB Output is correct
6 Correct 1 ms 972 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 972 KB Output is correct
2 Correct 1 ms 972 KB Output is correct
3 Correct 1 ms 1100 KB Output is correct
4 Correct 1 ms 972 KB Output is correct
5 Correct 1 ms 972 KB Output is correct
6 Correct 1 ms 972 KB Output is correct
7 Correct 8 ms 5324 KB Output is correct
8 Correct 10 ms 5324 KB Output is correct
9 Correct 13 ms 6352 KB Output is correct
10 Correct 8 ms 5324 KB Output is correct
11 Correct 10 ms 5324 KB Output is correct
12 Correct 14 ms 5324 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 972 KB Output is correct
2 Correct 1 ms 972 KB Output is correct
3 Correct 1 ms 1100 KB Output is correct
4 Correct 1 ms 972 KB Output is correct
5 Correct 1 ms 972 KB Output is correct
6 Correct 1 ms 972 KB Output is correct
7 Correct 8 ms 5324 KB Output is correct
8 Correct 10 ms 5324 KB Output is correct
9 Correct 13 ms 6352 KB Output is correct
10 Correct 8 ms 5324 KB Output is correct
11 Correct 10 ms 5324 KB Output is correct
12 Correct 14 ms 5324 KB Output is correct
13 Correct 342 ms 47976 KB Output is correct
14 Correct 388 ms 48008 KB Output is correct
15 Correct 446 ms 93360 KB Output is correct
16 Correct 329 ms 48172 KB Output is correct
17 Correct 831 ms 48028 KB Output is correct
18 Execution timed out 1087 ms 46820 KB Time limit exceeded