#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()
{
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<<endl;
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1132 KB |
Output is correct |
2 |
Correct |
1 ms |
1132 KB |
Output is correct |
3 |
Correct |
2 ms |
1260 KB |
Output is correct |
4 |
Correct |
1 ms |
1132 KB |
Output is correct |
5 |
Correct |
2 ms |
1132 KB |
Output is correct |
6 |
Correct |
2 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 |
2 ms |
1260 KB |
Output is correct |
4 |
Correct |
1 ms |
1132 KB |
Output is correct |
5 |
Correct |
2 ms |
1132 KB |
Output is correct |
6 |
Correct |
2 ms |
1132 KB |
Output is correct |
7 |
Correct |
22 ms |
5228 KB |
Output is correct |
8 |
Correct |
22 ms |
5228 KB |
Output is correct |
9 |
Correct |
25 ms |
6636 KB |
Output is correct |
10 |
Correct |
21 ms |
5228 KB |
Output is correct |
11 |
Correct |
24 ms |
5228 KB |
Output is correct |
12 |
Correct |
39 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 |
2 ms |
1260 KB |
Output is correct |
4 |
Correct |
1 ms |
1132 KB |
Output is correct |
5 |
Correct |
2 ms |
1132 KB |
Output is correct |
6 |
Correct |
2 ms |
1132 KB |
Output is correct |
7 |
Correct |
22 ms |
5228 KB |
Output is correct |
8 |
Correct |
22 ms |
5228 KB |
Output is correct |
9 |
Correct |
25 ms |
6636 KB |
Output is correct |
10 |
Correct |
21 ms |
5228 KB |
Output is correct |
11 |
Correct |
24 ms |
5228 KB |
Output is correct |
12 |
Correct |
39 ms |
5228 KB |
Output is correct |
13 |
Execution timed out |
1092 ms |
60396 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |