This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//Nguyen Huu Hoang Minh
#include <bits/stdc++.h>
#define sz(x) int(x.size())
#define all(x) x.begin(),x.end()
#define reset(x) memset(x, 0,sizeof(x))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define N 2005
#define remain(x) if (x > MOD) x -= MOD
#define ii pair<int, int>
#define iiii pair< ii , ii >
#define viiii vector< iiii >
#define vi vector<int>
#define vii vector< ii >
#define bit(x, i) (((x) >> (i)) & 1)
#define Task "test"
#define int long long
using namespace std;
typedef long double ld;
const int inf = 1e10;
const int minf = -1e10;
int m, n;
char a[N][N];
int vst[N][N];
void readfile()
{
ios_base::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
if (fopen(Task".inp","r"))
{
freopen(Task".inp","r",stdin);
//freopen(Task".out","w",stdout);
}
cin >> n >> m;
for(int i=1; i<=n; i++){
for(int j=1; j<=m; j++){
cin >> a[i][j];
}
}
}
vector<ii> vec[N];
int dx[4] = {1,-1,0,0};
int dy[4] = {0,0,1,-1};
bool ok(int x, int y){
return 1 <= x && x <= n && 1 <= y && y <= m;
}
void dfs(int u, int v, int &l, int &r, int &dep){
for(int i=0; i<4; i++){
int x = u + dx[i];
int y = v + dy[i];
if (!ok(x,y) || a[x][y]=='.' || vst[x][y]) continue;
vst[x][y] = 1;
l = min(l,y);
r = max(r,y);
dep += a[x][y]-'0';
dfs(x,y,l,r,dep);
}
}
int g[N][N];
vector<vector<int>> f(2001,vector<int>(2001,0));
void solve(int cur, int l, int r, int otpl, int otpr){
if (l > r) return;
int mid = (l+r)>>1;
ii Best = {minf,-1};
for(int k=otpl; k<=min(mid,otpr); k++){
Best = max(Best,{f[cur-1][k-1] + g[k][mid],k});
}
f[cur][mid] = Best.fi;
solve(cur,l,mid-1,otpl,Best.se);
solve(cur,mid+1,r,Best.se,otpr);
}
void proc(){
for(int i=1; i<=n; i++){
for(int j=1; j<=m; j++){
if (a[i][j]!='.' && !vst[i][j]){
vst[i][j] = 1;
int l = j, r = j, dep = a[i][j]-'0';
dfs(i,j,l,r,dep);
vec[l].pb(ii(r,dep));
}
}
}
vector<int> cnt(m+2,0);
for(int i=m; i>=1; i--){
for(auto x : vec[i]){
for(int j=i; j<=x.fi; j++) cnt[j]+=x.se;
}
for(int j=i; j<=m; j++) g[i][j] = cnt[j];
}
for(int i=1; i<=m; i++){
solve(i,1,m,1,m);
int ans = 0;
for(int j=1; j<=m; j++) ans = max(ans,f[i][j]);
cout << ans << '\n';
}
}
signed main()
{
readfile();
proc();
return 0;
}
Compilation message (stderr)
nafta.cpp: In function 'void readfile()':
nafta.cpp:37:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
37 | freopen(Task".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |