This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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);
}
}
void calc(int d,int idx,int optl,int optr)
{
int sum=0;
for(int i=idx;i>=optl;--i)
{
sum+=pref[i][m]-pref[i][idx-1];
}
for(int i=optl;i<=optr&&i<idx;++i)
{
sum-=pref[i][m]-pref[i][idx-1];
if(dp[i][d-1]+sum>dp[idx][d])
{
dp[idx][d]=dp[i][d-1]+sum;
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);
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)
{
for(int j=1;j<=m;++j)
{
cin>>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 j=1;j<=m;++j)
{
for(int i=1;i<=m;++i)
{
pref[j][i]=pref[j][i]+pref[j][i-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 time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |