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 <iostream>
#include <stdio.h>
#include <cstring>
#include <queue>
using namespace std;
#define int long long
#define endl '\n'
const int N=2e3+9;
typedef pair <int,int> ii;
char a[N][N];
bool check[N][N];
int n,m,cost[N][N],f[N][N],dem;
int dx[4]={0,0,-1,1};
int dy[4]={1,-1,0,0};
bool inside(int x,int y)
{
return (x>=1&&x<=n&&y>=1&&y<=m);
}
void bfs(int x,int y,int &maxx)
{
maxx=0;
int xnew,ynew;
queue <ii> q;
q.push({x,y});
check[x][y]=true;
while (!q.empty())
{
x=q.front().first;
y=q.front().second;
dem+=a[x][y]-'0';
maxx=max(maxx,y);
// cout<<x<<" "<<y<<" "<<a[x][y]<<endl;
q.pop();
for (int i=0;i<=3;i++)
{
xnew=x+dx[i];
ynew=y+dy[i];
if (check[xnew][ynew]||!inside(xnew,ynew)) continue;
check[xnew][ynew]=true;
q.push({xnew,ynew});
}
}
}
void dp(int k,int lfind,int rfind,int l,int r)
{
if (l>r) return;
int mid=(l+r)/2;
int lim=min(mid-1,rfind),pos;
for (int i=lfind;i<=lim;i++)
{
int tmp=f[k-1][i]+cost[i+1][mid];;
if (f[k][mid]<tmp)
{
f[k][mid]=tmp;
pos=i;
}
}
// cout<<pos<<endl;
// cout<<f[1][1]<<" "<<k<<" "<<mid<<endl;
dp(k,lfind,pos,l,mid-1);
dp(k,pos,rfind,mid+1,r);
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);
//freopen("test.inp","r",stdin);
cin>>n>>m;
for (int i=1;i<=n;i++)
for (int j=1;j<=m;j++)
{
cin>>a[i][j];
if (a[i][j]=='.') check[i][j]=true;
}
for (int j=1;j<=m;j++)
{
for (int i=1;i<=n;i++)
if (!check[i][j])
{
dem=0;
int maxx;
bfs(i,j,maxx);
cost[j][j]+=dem;
cost[j][maxx+1]-=dem;
}
}
for (int i=1;i<=m;i++)
for (int j=i;j<=m;j++)
cost[i][j]+=cost[i][j-1];
memset(f,-1,sizeof(f));
for (int i=0;i<=m;i++)
f[0][i]=0;
for (int i=m;i>=1;i--)
for (int j=i;j<=m;j++)
cost[i][j]+=cost[i+1][j];
for (int i=1;i<=m;i++)
{
dp(i,0,m,1,m);
//break;
}
//cout<<cost[4][5]<<endl;
for (int i=1;i<=m;i++)
{
int maxx=0;
for (int j=1;j<=m;j++)
maxx=max(maxx,f[i][j]);
cout<<maxx<<endl;
}
return 0;
}
Compilation message (stderr)
nafta.cpp: In function 'void dp(long long int, long long int, long long int, long long int, long long int)':
nafta.cpp:60:7: warning: 'pos' may be used uninitialized in this function [-Wmaybe-uninitialized]
60 | dp(k,lfind,pos,l,mid-1);
| ~~^~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |