Submission #220803

# Submission time Handle Problem Language Result Execution time Memory
220803 2020-04-08T23:10:31 Z mahmoudbadawy Nafta (COI15_nafta) C++17
100 / 100
523 ms 104312 KB
#include <bits/stdc++.h>
#define F first
#define S second

using namespace std;

const int N=2005;

char gr[N][N];
int n,m,idc;
int mn[N*N],mx[N*N],sum[N*N];
int id[N][N];
int sm[N][N];
int dp[N][N];

int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};

void go(int x,int y)
{
	if(x<0||x>=n||y<0||y>=m) return;
	if(gr[x][y]=='.') return;
	if(id[x][y]) return;
	id[x][y]=idc;
	sum[idc]+=gr[x][y]-'0';
	mn[idc]=min(mn[idc],y);
	mx[idc]=max(mx[idc],y);
	for(int i=0;i<4;i++)
		go(x+dx[i],y+dy[i]);
}

int get(int l,int r)
{
	// [l,r] , [r,m-1];
	int ans=sm[r][m-1];
	if(l) ans-=sm[l-1][m-1];
	if(r) ans-=sm[r][r-1];
	if(l&&r) ans+=sm[l-1][r-1];
	return ans;
}

void solve(int cur,int l,int r,int lo,int ro)
{
	if(r<l) return;
	int mid=(l+r)/2;
	int opt=lo;
	// mid is the point to drill
	for(int i=lo;i<=min(ro,mid);i++)
	{
		// i-1 is the last drilled hole
		int curans=(i?dp[cur-1][i-1]:0)+get(i,mid);
		if(curans>dp[cur][mid])
		{
			dp[cur][mid]=curans;
			opt=i;
		}
	}
	solve(cur,l,mid-1,lo,opt);
	solve(cur,mid+1,r,opt,ro);
}

int main()
{
	scanf("%d %d",&n,&m);
	for(int i=0;i<n;i++)
		scanf("%s",gr[i]);
	memset(mn,'?',sizeof mn);
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<m;j++)
		{
			if(!id[i][j] && gr[i][j]!='.')
			{
				idc++;
				go(i,j);
			}
		}
	}
	for(int i=1;i<=idc;i++)
	{
		sm[mn[i]][mx[i]]+=sum[i];
	}
	for(int i=0;i<m;i++)
	{
		for(int j=0;j<m;j++)
		{
			if(i) sm[i][j]+=sm[i-1][j];
			if(j) sm[i][j]+=sm[i][j-1];
			if(i&&j) sm[i][j]-=sm[i-1][j-1];
		}
	}
	// sm[i][j] is the sum of components with l<=i ,r<=j
	for(int i=1;i<=m;i++)
	{
		solve(i,0,m-1,0,m-1);
		int ans=0;
		for(int j=0;j<m;j++) ans=max(ans,dp[i][j]);
		printf("%d\n",ans);
	}
}

Compilation message

nafta.cpp: In function 'int main()':
nafta.cpp:64:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d %d",&n,&m);
  ~~~~~^~~~~~~~~~~~~~~
nafta.cpp:66:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%s",gr[i]);
   ~~~~~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 13 ms 16768 KB Output is correct
2 Correct 13 ms 16896 KB Output is correct
3 Correct 13 ms 16896 KB Output is correct
4 Correct 13 ms 16768 KB Output is correct
5 Correct 13 ms 16640 KB Output is correct
6 Correct 13 ms 16768 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 16768 KB Output is correct
2 Correct 13 ms 16896 KB Output is correct
3 Correct 13 ms 16896 KB Output is correct
4 Correct 13 ms 16768 KB Output is correct
5 Correct 13 ms 16640 KB Output is correct
6 Correct 13 ms 16768 KB Output is correct
7 Correct 21 ms 21504 KB Output is correct
8 Correct 22 ms 21504 KB Output is correct
9 Correct 23 ms 22264 KB Output is correct
10 Correct 20 ms 20736 KB Output is correct
11 Correct 21 ms 20480 KB Output is correct
12 Correct 21 ms 20608 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 16768 KB Output is correct
2 Correct 13 ms 16896 KB Output is correct
3 Correct 13 ms 16896 KB Output is correct
4 Correct 13 ms 16768 KB Output is correct
5 Correct 13 ms 16640 KB Output is correct
6 Correct 13 ms 16768 KB Output is correct
7 Correct 21 ms 21504 KB Output is correct
8 Correct 22 ms 21504 KB Output is correct
9 Correct 23 ms 22264 KB Output is correct
10 Correct 20 ms 20736 KB Output is correct
11 Correct 21 ms 20480 KB Output is correct
12 Correct 21 ms 20608 KB Output is correct
13 Correct 415 ms 75092 KB Output is correct
14 Correct 466 ms 73208 KB Output is correct
15 Correct 523 ms 104312 KB Output is correct
16 Correct 391 ms 69112 KB Output is correct
17 Correct 484 ms 67448 KB Output is correct
18 Correct 472 ms 67492 KB Output is correct