#include<iostream>
#include<stack>
#include<map>
#include<vector>
#include<string>
#include<unordered_map>
#include <queue>
#include<cstring>
#include<float.h>
#include<limits.h>
#include <cassert>
#include<cmath>
#include<set>
#include<algorithm>
#include <iomanip>
#include<numeric> //gcd(a,b)
#include<bitset>
using namespace std;
#define ll long long
#define f first
#define endl "\n"
#define s second
#define pii pair<int,ll>
#define ppii pair<int,pii>
#define vi vector<int>
#define pb push_back
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define F(n) for(int i=0;i<n;i++)
#define lb lower_bound
#define ub upper_bound
using namespace std;
#define int long long
#define double long double
#define fastio ios::sync_with_stdio(false);cin.tie(NULL);
#pragma GCC optimize ("03,unroll-loops")
const int mod=1e9+7,mxn=3e5,lg=30,inf=1e18,minf=-1e9,Mxn=100000;
int n,m;
char grid[2002][2002];
int dp[2002][2002],val[2002],cost[2002][2002],on[2002];
bool vis[2002][2002];
int u[4]={0,0,1,-1},r[4]={1,-1,0,0};
int ans=0,sum=0;
int mn,mx;
void dfs(int cx,int cy){
on[cy]=1;
sum+=(grid[cx][cy]-'0');
mn=min(mn,cy);
mx=max(mx,cy);
vis[cx][cy]=true;
for(int i=0;i<4;i++){
int nx=cx+u[i],ny=cy+r[i];
if(nx<0||ny<0||nx>=n||ny>=m)continue;
if(vis[nx][ny]||grid[nx][ny]=='.')continue;
dfs(nx,ny);
}
}
void solve(int l,int r,int opl,int opr,int k){
if(l>r)return;
int mid=l+(r-l)/2;
int opt=mid;
for(int j=opl;j<min(mid,opr);j++)if(dp[opt][k-1]-cost[opt][mid]<dp[j][k-1]-cost[j][mid])opt=j;
dp[mid][k]=dp[opt][k-1]-cost[opt][mid]+val[mid];
if(dp[mid][k]<val[mid])dp[mid][k]=val[mid],opt=mid;
solve(l,mid-1,opl,opt,k);
solve(mid+1,r,opt,opr,k);
}
int32_t main(){
fastio
int sub=0;
cin>>n>>m;
for(int i=0;i<n;i++)for(int j=0;j<m;j++)cin>>grid[i][j];
for(int i=0;i<n;i++)for(int j=0;j<m;j++){
if(vis[i][j]||grid[i][j]=='.')continue;
sum=0;
mn=inf,mx=minf;
dfs(i,j);
for(int j=mn;j<=mx;j++)val[j]+=sum;
cost[mn][mx]+=sum;
}
for(int i=0;i<m;i++)for(int j=0;j<m;j++)if(i)cost[i][j]+=cost[i-1][j];
for(int i=0;i<m;i++)for(int j=m-1;j>=0;j--)cost[i][j]+=cost[i][j+1];/*
for(int i=1;i<=m;i++){
for(int j=0;j<m;j++){
dp[j][i]=val[j];
if(j)dp[j][i]=max(dp[j][i],dp[j-1][i]);
for(int l=0;l<j;l++)dp[j][i]=max(dp[j][i],dp[l][i-1]-cost[l][j]+val[j]);
}
}
for(int i=1;i<=m;i++)cout<<dp[m-1][i]<<'\n';
*/
for(int i=1;i<=m;i++)solve(0,m-1,0,m-1,i);
for(int i=1;i<=m;i++){
int ans=0;
for(int j=0;j<m;j++)ans=max(ans,dp[j][i]);
cout<<ans<<'\n';
}
}
Compilation message
nafta.cpp: In function 'int32_t main()':
nafta.cpp:70:9: warning: unused variable 'sub' [-Wunused-variable]
70 | int sub=0;
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
8540 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
8540 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
8540 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |