Submission #539316

# Submission time Handle Problem Language Result Execution time Memory
539316 2022-03-18T17:12:18 Z mars4 Nafta (COI15_nafta) C++17
34 / 100
1000 ms 67964 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp> 

using namespace std;
using namespace __gnu_pbds; 

#define ff              first
#define ss              second
#define ll              int64_t
#define ld              long double
#define nl              cout<<"\n"
#define i128            __int128_t
#define all(v)          v.begin(),v.end()
#define mset(a,v)       memset((a),(v),sizeof(a))
#define forn(i,a,b)     for(int64_t i=int64_t(a);i<int64_t(b);++i)
#define forb(i,a,b)     for(int64_t i=int64_t(a);i>=int64_t(b);--i)
#define fastio()        ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);

#define mod         1'000'000'007
#define mod2        998'244'353 
#define inf         1'000'000'000'000'007
#define pi          3.14159265358979323846

template<class key,class cmp=std::less<key>>
using ordered_set=tree<key,null_type,cmp,rb_tree_tag,tree_order_statistics_node_update>;

template<class L,class R> ostream& operator<<(ostream& out,pair<L,R> &p)        {return out<<"("<<p.ff<<", "<<p.ss<<")";}
template<class T> ostream& operator<<(ostream& out,vector<T> &v)                {out<<"[";for(auto it=v.begin();it!=v.end();++it){if(it!=v.begin())out<<", ";out<<*it;}return out<<"]";}
template<class T> ostream& operator<<(ostream& out,deque<T> &v)                 {out<<"[";for(auto it=v.begin();it!=v.end();++it){if(it!=v.begin())out<<", ";out<<*it;}return out<<"]";}
template<class T> ostream& operator<<(ostream& out,set<T> &s)                   {out<<"{";for(auto it=s.begin();it!=s.end();++it){if(it!=s.begin())out<<", ";out<<*it;}return out<<"}";}
template<class T> ostream& operator<<(ostream& out,ordered_set<T> &s)           {out<<"{";for(auto it=s.begin();it!=s.end();++it){if(it!=s.begin())out<<", ";out<<*it;}return out<<"}";}
template<class L,class R> ostream& operator<<(ostream& out,map<L,R> &m)         {out<<"{";for(auto it=m.begin();it!=m.end();++it){if(it!=m.begin())out<<", ";out<<*it;}return out<<"}";}

void dbg_out() {cerr<<"]\n";}
template<typename Head,typename... Tail> 
void dbg_out(Head H,Tail... T) {cerr<<H;if(sizeof...(Tail))cerr<<", ";dbg_out(T...);}
#ifdef LOCAL
#define dbg(...) cerr<<"["<<#__VA_ARGS__<<"] = [",dbg_out(__VA_ARGS__)
#else
#define dbg(...)
#endif

//---------------------------------mars4---------------------------------

ll cnt[2000][2000];
ll dp[2000][2000];

void calc(ll l,ll r,ll optL,ll optR,ll level)
{
	if(l>r)
	{
		return;
	}
	ll mid=(l+r)>>1;
	pair<ll,ll> best={-1,-1};
	forn(i,optL,min(mid,optR)+1)
	{
		ll val=(i?dp[level-1][i-1]+cnt[i-1][mid]:0);
		if(val>best.ff)
		{
			best={val,i};
		}
	}
	dp[level][mid]=best.ff;
	ll opt=best.ss;
	calc(l,mid-1,optL,opt,level);
	calc(mid+1,r,opt,optR,level);
}

int main()
{
	fastio();
	ll z,n,m,t,k,i,j,l,d,h,r;
	cin>>n>>m;
	vector<string> a(n);
	for(auto &i:a)
	{
		cin>>i;
	}
	vector<ll> dx={1,0,0,-1};
	vector<ll> dy={0,1,-1,0};
	vector<ll> val(n*m);
	vector<vector<ll>> mark(n,vector<ll>(m,-1));
	ll id=0;
	forn(i,0,n)
	{
		forn(j,0,m)
		{
			if(a[i][j]!='.' and mark[i][j]==-1)
			{
				queue<pair<ll,ll>> q;
				q.push({i,j});
				mark[i][j]=id;
				while(!q.empty())
				{
					auto [r,c]=q.front();
					q.pop();
					val[id]+=(a[r][c]-'0');
					forn(k,0,4)
					{
						ll nr=r+dx[k];
						ll nc=c+dy[k];
						if(nr>=0 and nr<n and nc>=0 and nc<m and a[nr][nc]!='.' and mark[nr][nc]==-1)
						{
							q.push({nr,nc});
							mark[nr][nc]=id;
						}
					}
				}
				id++;
			}
		}
	}
	forn(i2,0,m)
	{
		set<ll> s;
		forn(j,0,n)
		{
			if(mark[j][i2]!=-1)
			{
				s.insert(mark[j][i2]);
			}
		}
		for(ll i:s)
		{
			cnt[i2][i2]+=val[i];
		}
		forn(i1,0,i2)
		{
			cnt[i1][i2]=cnt[i2][i2];
			set<ll> s1;
			forn(j,0,n)
			{
				if(mark[j][i1]!=-1)
				{
					s1.insert(mark[j][i1]);
				}
			}
			for(ll i:s1)
			{
				if(s.count(i))
				{
					cnt[i1][i2]-=val[i];
				}
			}
		}
	}
	forn(i,0,m)
	{
		dp[0][i]=cnt[i][i];
	}
	vector<ll> ans;
	ans.push_back(*max_element(dp[0],dp[0]+m));
	forn(i,1,m)
	{
		calc(i,m-1,i,m-1,i);
		ans.push_back(*max_element(dp[i],dp[i]+m));
	}
	for(ll i:ans)
	{
		cout<<i<<"\n";
	}
	cerr<<"\nTime elapsed: "<<1000*clock()/CLOCKS_PER_SEC<<"ms\n";
	return 0;
}

Compilation message

nafta.cpp: In function 'int main()':
nafta.cpp:74:5: warning: unused variable 'z' [-Wunused-variable]
   74 |  ll z,n,m,t,k,i,j,l,d,h,r;
      |     ^
nafta.cpp:74:11: warning: unused variable 't' [-Wunused-variable]
   74 |  ll z,n,m,t,k,i,j,l,d,h,r;
      |           ^
nafta.cpp:74:13: warning: unused variable 'k' [-Wunused-variable]
   74 |  ll z,n,m,t,k,i,j,l,d,h,r;
      |             ^
nafta.cpp:74:15: warning: unused variable 'i' [-Wunused-variable]
   74 |  ll z,n,m,t,k,i,j,l,d,h,r;
      |               ^
nafta.cpp:74:17: warning: unused variable 'j' [-Wunused-variable]
   74 |  ll z,n,m,t,k,i,j,l,d,h,r;
      |                 ^
nafta.cpp:74:19: warning: unused variable 'l' [-Wunused-variable]
   74 |  ll z,n,m,t,k,i,j,l,d,h,r;
      |                   ^
nafta.cpp:74:21: warning: unused variable 'd' [-Wunused-variable]
   74 |  ll z,n,m,t,k,i,j,l,d,h,r;
      |                     ^
nafta.cpp:74:23: warning: unused variable 'h' [-Wunused-variable]
   74 |  ll z,n,m,t,k,i,j,l,d,h,r;
      |                       ^
nafta.cpp:74:25: warning: unused variable 'r' [-Wunused-variable]
   74 |  ll z,n,m,t,k,i,j,l,d,h,r;
      |                         ^
# Verdict Execution time Memory Grader output
1 Correct 2 ms 724 KB Output is correct
2 Correct 2 ms 724 KB Output is correct
3 Correct 2 ms 724 KB Output is correct
4 Correct 2 ms 724 KB Output is correct
5 Correct 2 ms 724 KB Output is correct
6 Correct 2 ms 724 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 724 KB Output is correct
2 Correct 2 ms 724 KB Output is correct
3 Correct 2 ms 724 KB Output is correct
4 Correct 2 ms 724 KB Output is correct
5 Correct 2 ms 724 KB Output is correct
6 Correct 2 ms 724 KB Output is correct
7 Correct 394 ms 5040 KB Output is correct
8 Correct 452 ms 5004 KB Output is correct
9 Correct 130 ms 5000 KB Output is correct
10 Correct 457 ms 4936 KB Output is correct
11 Correct 405 ms 4936 KB Output is correct
12 Correct 418 ms 4888 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 724 KB Output is correct
2 Correct 2 ms 724 KB Output is correct
3 Correct 2 ms 724 KB Output is correct
4 Correct 2 ms 724 KB Output is correct
5 Correct 2 ms 724 KB Output is correct
6 Correct 2 ms 724 KB Output is correct
7 Correct 394 ms 5040 KB Output is correct
8 Correct 452 ms 5004 KB Output is correct
9 Correct 130 ms 5000 KB Output is correct
10 Correct 457 ms 4936 KB Output is correct
11 Correct 405 ms 4936 KB Output is correct
12 Correct 418 ms 4888 KB Output is correct
13 Execution timed out 1091 ms 67964 KB Time limit exceeded
14 Halted 0 ms 0 KB -