Submission #341299

#TimeUsernameProblemLanguageResultExecution timeMemory
341299ogibogi2004Nafta (COI15_nafta)C++14
34 / 100
1014 ms56700 KiB
#include<bits/stdc++.h> using namespace std; const int MAXN=2048; const int INF=1e9; int pref[MAXN][MAXN]; int n,m,val,maxy; char c[MAXN][MAXN]; int dp[MAXN][MAXN]; int opt[MAXN][MAXN]; bool vis[MAXN][MAXN]; int dx[4]={-1,1,0,0}; int dy[4]={0,0,-1,1}; void dfs(int x,int y) { vis[x][y]=1; maxy=max(maxy,y); val+=c[x][y]-'0'; for(int l=0;l<4;++l) { int x1=x+dx[l]; int y1=y+dy[l]; if(x1<1||y1<1||x1>n||y1>m)continue; if(c[x1][y1]=='.')continue; if(vis[x1][y1])continue; dfs(x1,y1); } } int get_sum(int l1,int l2,int r1,int r2) { return pref[l2][r2]-pref[l1-1][r2]-pref[l2][r1-1]+pref[l1-1][r1-1]; } void calc(int d,int idx,int optl,int optr) { for(int i=optl;i<=optr&&i<idx;++i) { int s=get_sum(i+1,idx,idx,m); if(dp[i][d-1]+s>dp[idx][d]) { dp[idx][d]=dp[i][d-1]+s; opt[idx][d]=i; } } } void solve(int d,int l,int r,int optl,int optr) { if(l>r)return; int mid=(l+r)/2; //cout<<mid<<" "<<optl<<" "<<optr<<endl; calc(d,mid,optl,optr); if(l==r)return; solve(d,mid+1,r,opt[mid][d],optr); solve(d,l,mid-1,optl,opt[mid][d]); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin>>n>>m; for(int i=1;i<=n;++i) { cin.get(c[i][0]); for(int j=1;j<=m;++j) { cin.get(c[i][j]); } } //cout<<"*\n"; for(int j=1;j<=m;++j) { for(int i=1;i<=n;++i) { if(vis[i][j])continue; if(c[i][j]=='.')continue; val=0;maxy=0; dfs(i,j); pref[j][maxy]+=val; } } //cout<<"*\n"; for(int i1=1;i1<=m;++i1) { for(int i2=1;i2<=m;++i2) { pref[i1][i2]=pref[i1][i2]+pref[i1-1][i2]+pref[i1][i2-1]-pref[i1-1][i2-1]; } } for(int i=0;i<=m;++i) { for(int j=0;j<=m;++j) { dp[i][j]=-INF; } } dp[0][0]=0; for(int k=1;k<=m;++k) { solve(k,1,m,0,m); int maxdp=0; for(int i=1;i<=m;++i) { maxdp=max(maxdp,dp[i][k]); //cout<<dp[i][k]<<" "<<opt[i][k]<<endl; } cout<<maxdp<<'\n'; } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...