Submission #79119

# Submission time Handle Problem Language Result Execution time Memory
79119 2018-10-11T08:33:22 Z psmao Sailing Race (CEOI12_race) C++14
100 / 100
547 ms 6000 KB
#include <bits/stdc++.h>
using namespace std;

#define fo(i,s,t) for(int i = s; i <= t; ++ i)
#define fd(i,s,t) for(int i = s; i >= t; -- i)
#define bf(i,s) for(int i = head[s]; i; i = e[i].next)
#define mp make_pair
#define fi first
#define se second
#define pii pair<int,int>
#define pb push_back
#define VI vector<int>
#define sf scanf
#define pf printf
#define fp freopen
#define SZ(x) ((int)(x).size())
#ifdef MPS
#define D(x...) printf(x)
#else
#define D(x...)
#endif
typedef long long ll;
typedef double db;
typedef unsigned long long ull;
const int inf = 1<<30;
const ll INF = 1ll<<60;
const db Inf = 1e20;
const db eps = 1e-9;

void gmax(int &a,int b){a = (a > b ? a : b);}
void gmin(int &a,int b){a = (a < b ? a : b);}

const int maxn = 505;

int f[maxn][maxn], g[maxn][maxn], md[maxn][maxn], mdc[maxn][maxn], n, sub;
VI adj[maxn], radj[maxn];

int dis(int x, int y)
{
	if(x > y) swap(x, y);
	return min(y-x, n-(y-x));
}
int main()
{
	#ifdef MPS
		fp("1.in","r",stdin);
		fp("1.out","w",stdout);
		db mps = clock();
	#endif
	sf("%d%d",&n,&sub);
	fo(i,1,n) fo(j,1,n) md[i][j] = mdc[i][j] = -inf;
	fo(i,1,n)
	{
		int x;
		while(1)
		{
			sf("%d",&x);
			if(!x) break;
			adj[i].pb(x);
			radj[x].pb(i);
			md[i][x] = mdc[i][x] = 1;
		}
	}
	fo(len,2,n)
	{
		fo(i,1,n)
		{
			int j = i+len-1;
			if(j > n) j = j - n;
			for(auto k : adj[i])
			{
				if(i < j)
				{
					if(k >= i && k < j) 
					{
						gmax(f[i][j], 1 + max(f[k][j], g[k][i]));
						gmax(md[i][j], 1 + md[k][j]);
					}
				}
				else
				{
					if(k >= i || k < j) 
					{
						gmax(f[i][j], 1 + max(f[k][j], g[k][i]));
						gmax(md[i][j], 1 + md[k][j]);
					}
				}
			}
			for(auto k : adj[j])
			{
				if(i < j)
				{
					if(k > i && k <= j) 
					{
						gmax(g[j][i], 1 + max(g[k][i], f[k][j]));
						gmax(mdc[j][i], 1 + mdc[k][i]);
					}
				}
				else
				{
					if(k > i || k <= j) 
					{
						gmax(g[j][i], 1 + max(g[k][i], f[k][j]));
						gmax(mdc[j][i], 1 + mdc[k][i]);
					}
				}
			}	
			D("%d %d %d %d\n",i,j,md[i][j],mdc[j][i]);
		}
	}
	int ans = 0, target;
	fo(s,1,n) 
		for(auto t : adj[s])
		{
			if(max(f[t][s], g[t][s]) + 1 > ans)
			{
				ans = max(f[t][s], g[t][s]) + 1;
				target = s;
			}
		}
	if(sub == 1)
	{
		fo(C,1,n)
			fo(B,1,n) if(B != C)
 			{
				//case 1 : BAC counterclockwise
 				if(C < B)
 				{
	 				int A = 0;
	 				for(auto x : radj[B]) 
	 					if(x > B || x < C)
	 					{
	 						if(!A || dis(A,C) > dis(x,C))
	 							A = x;
	 					}
	 				if(A) // there is such A existing
	 				{
	 					// we want to find a D
	 					for(auto D : adj[C])
	 					{
	 						if(A > B) // B<A<=N
	 						{
	 							if(D > B && D < A)
	 							{
	 								if(1 + mdc[B][C] + 1 + max(g[D][B], f[D][A]) > ans)
	 								{
	 									ans = 1 + mdc[B][C] + 1 + max(g[D][B], f[D][A]);
	 									target = A;
	 									D("a%d %d %d %d %d\n",A,B,C,D,ans);
	 								}
	 							}
	 						}
	 						else // 1<A<C<B<N
	 						{
	 							if(D < A || D > B)
	 							{
	 								if(1 + mdc[B][C] + 1 + max(g[D][B], f[D][A]) > ans)
	 								{
	 									ans = 1 + mdc[B][C] + 1 + max(g[D][B], f[D][A]);
	 									target = A;
	 									D("b%d %d %d %d %d\n",A,B,C,D,ans);
	 								}
	 							}
	 						}
	 					}
	 				}
	 				A = 0;
	 				for(auto x : radj[B]) 
	 					if(x > C && x < B)
	 					{
	 						if(!A || dis(A,C) > dis(x,C))
	 							A = x;
	 					}
	 				if(A)
	 				{
	 					for(auto D : adj[C])
	 					{
	 						if(D > A && D < B)
	 						{
	 							if(1 + md[B][C] + 1 + max(f[D][B], g[D][A]) > ans)
	 							{
	 								ans = 1 + md[B][C] + 1 + max(f[D][B], g[D][A]);
	 								target = A;
	 								D("c%d %d %d %d %d\n",A,B,C,D,ans);
	 							}
	 						}
	 					}
	 				}
	 			}
				else //case 2 : BAC clockwise
	 			{
	 				int A = 0;
	 				for(auto x : radj[B])
	 					if(x < B || x > C)
	 					{
	 						if(!A || dis(A,C) > dis(x,C))
	 							A = x;
	 					}
	 				if(A)
	 				{
	 					for(auto D : adj[C])
	 					{
	 						if(A > C)
	 						{
	 							if(D < B || D > A)
	 							{
	 								if(1 + md[B][C] + 1 + max(f[D][B], g[D][A]) > ans)
	 								{
	 									ans = 1 + md[B][C] + 1 + max(f[D][B], g[D][A]);
	 									target = A;
	 									D("d%d %d %d %d %d\n",A,B,C,D,ans);
	 								}
	 							}
	 						}
	 						else
	 						{
	 							if(D > A && D < B)
	 							{
	 								if(1 + md[B][C] + 1 + max(f[D][B], g[D][A]) > ans)
	 								{
	 									ans = 1 + md[B][C] + 1 + max(f[D][B], g[D][A]);
	 									target = A;
	 									D("e%d %d %d %d %d\n",A,B,C,D,ans);
	 								}	
	 							}
	 						}
	 					}
	 				}
	 				A = 0;
	 				for(auto x : radj[B])
	 					if(x > B && x < C)
	 					{
	 						if(!A || dis(A,C) > dis(x,C))
	 							A = x;
	 					}
	 				if(A)
	 				{
	 					for(auto D : adj[C])
	 						if(D > B && D < A)
	 						{
	 							if(1 + mdc[B][C] + 1 + max(f[D][A], g[D][B]) > ans)
	 							{
	 								ans = 1 + mdc[B][C] + 1 + max(f[D][A], g[D][B]);
	 								target = A;
	 								D("f%d %d %d %d %d\n",A,B,C,D,ans);
	 							}
	 						}
	 				}
	 			}
			}
	}
	pf("%d\n%d\n",ans,target);
	#ifdef MPS
		pf("Time Elapsed: %.3fms\n",clock()-mps);
	#endif
	return 0;
}

Compilation message

race.cpp: In function 'int main()':
race.cpp:50:4: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  sf("%d%d",&n,&sub);
    ^
race.cpp:57:6: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
    sf("%d",&x);
      ^
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 632 KB Output is correct
3 Correct 2 ms 712 KB Output is correct
4 Correct 3 ms 864 KB Output is correct
5 Correct 3 ms 912 KB Output is correct
6 Correct 4 ms 1204 KB Output is correct
7 Correct 5 ms 1244 KB Output is correct
8 Correct 6 ms 1256 KB Output is correct
9 Correct 7 ms 1404 KB Output is correct
10 Correct 18 ms 1544 KB Output is correct
11 Correct 7 ms 1576 KB Output is correct
12 Correct 37 ms 2492 KB Output is correct
13 Correct 69 ms 3152 KB Output is correct
14 Correct 62 ms 4168 KB Output is correct
15 Correct 357 ms 5308 KB Output is correct
16 Correct 464 ms 5672 KB Output is correct
17 Correct 385 ms 5672 KB Output is correct
18 Correct 87 ms 5672 KB Output is correct
19 Correct 538 ms 5960 KB Output is correct
20 Correct 547 ms 6000 KB Output is correct