#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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1132 KB |
Output is correct |
2 |
Correct |
1 ms |
1132 KB |
Output is correct |
3 |
Correct |
1 ms |
1260 KB |
Output is correct |
4 |
Correct |
1 ms |
1132 KB |
Output is correct |
5 |
Correct |
1 ms |
1132 KB |
Output is correct |
6 |
Correct |
1 ms |
1132 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1132 KB |
Output is correct |
2 |
Correct |
1 ms |
1132 KB |
Output is correct |
3 |
Correct |
1 ms |
1260 KB |
Output is correct |
4 |
Correct |
1 ms |
1132 KB |
Output is correct |
5 |
Correct |
1 ms |
1132 KB |
Output is correct |
6 |
Correct |
1 ms |
1132 KB |
Output is correct |
7 |
Correct |
13 ms |
5228 KB |
Output is correct |
8 |
Correct |
16 ms |
5228 KB |
Output is correct |
9 |
Correct |
16 ms |
6508 KB |
Output is correct |
10 |
Correct |
13 ms |
5228 KB |
Output is correct |
11 |
Correct |
17 ms |
5228 KB |
Output is correct |
12 |
Correct |
13 ms |
5228 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1132 KB |
Output is correct |
2 |
Correct |
1 ms |
1132 KB |
Output is correct |
3 |
Correct |
1 ms |
1260 KB |
Output is correct |
4 |
Correct |
1 ms |
1132 KB |
Output is correct |
5 |
Correct |
1 ms |
1132 KB |
Output is correct |
6 |
Correct |
1 ms |
1132 KB |
Output is correct |
7 |
Correct |
13 ms |
5228 KB |
Output is correct |
8 |
Correct |
16 ms |
5228 KB |
Output is correct |
9 |
Correct |
16 ms |
6508 KB |
Output is correct |
10 |
Correct |
13 ms |
5228 KB |
Output is correct |
11 |
Correct |
17 ms |
5228 KB |
Output is correct |
12 |
Correct |
13 ms |
5228 KB |
Output is correct |
13 |
Execution timed out |
1014 ms |
56700 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |