Submission #58798

# Submission time Handle Problem Language Result Execution time Memory
58798 2018-07-19T13:31:50 Z TadijaSebez Xylophone (JOI18_xylophone) C++11
100 / 100
82 ms 708 KB
#include "xylophone.h"
#include <stdio.h>
#include <vector>
#include <algorithm>
using namespace std;
const int N=5050;
int min(int a, int b){ return a>b?b:a;}
int max(int a, int b){ return a>b?a:b;}
//-----------------------//
/*
int _a[N],_b[N];
int _N;
int query(int l, int r)
{
	int Max=0,Min=_N+1,i;
	for(i=l;i<=r;i++) Max=max(Max,_a[i]),Min=min(Min,_a[i]);
	return Max-Min;
}
void answer(int i, int x){ _b[i]=x;}
*/
//-----------------------//
int sol[N];
bool was[N];
int abs(int x){ return x>0?x:-x;}
void solve(int n)
{
	int top=n-1,bot=2,mid,ans=1;
	while(top>=bot)
	{
		mid=top+bot>>1;
		if(query(mid,n)==n-1) ans=mid,bot=mid+1;
		else top=mid-1;
	}
	int i,cnt=0,j;
	sol[ans]=1;was[1]=1;cnt++;
	sol[ans+1]=query(ans,ans+1)+1;was[sol[ans+1]]=1;cnt++;
	if(ans!=1) sol[ans-1]=query(ans-1,ans)+1,was[sol[ans-1]]=1,cnt++;
	for(i=ans+2;i<=n;i++)
	{
		if(cnt==n-1)
		{
			for(j=1;j<=n;j++) if(!was[j]) break;
			sol[i]=j;
			was[j]=1;
			cnt++;
		}
		else
		{
			if(sol[i-1]<sol[i-2])
			{
				int q1=query(i-1,i);
				int d=abs(sol[i-1]-sol[i-2]);
				int h=sol[i-1];
				if(h-q1<=0 || was[h-q1])
				{
					sol[i]=h+q1;
					was[h+q1]=1;
					cnt++;
				}
				else if(h+q1>n || was[h+q1])
				{
					sol[i]=h-q1;
					was[h-q1]=1;
					cnt++;
				}
				else
				{
					int q2=query(i-2,i);
					if(q2==q1+d)
					{
						sol[i]=h-q1;
						was[h-q1]=1;
						cnt++;
					}
					else
					{
						sol[i]=h+q1;
						was[h+q1]=1;
						cnt++;
					}
				}
			}
			else
			{
				int q1=query(i-1,i);
				int d=abs(sol[i-1]-sol[i-2]);
				int h=sol[i-1];
				if(h-q1<=0 || was[h-q1])
				{
					sol[i]=h+q1;
					was[h+q1]=1;
					cnt++;
				}
				else if(h+q1>n || was[h+q1])
				{
					sol[i]=h-q1;
					was[h-q1]=1;
					cnt++;
				}
				else
				{
					int q2=query(i-2,i);
					if(q2==q1+d)
					{
						sol[i]=h+q1;
						was[h+q1]=1;
						cnt++;
					}
					else
					{
						sol[i]=h-q1;
						was[h-q1]=1;
						cnt++;
					}
				}
			}
		}
	}
	for(i=ans-2;i>=1;i--)
	{
		if(cnt==n-1)
		{
			for(j=1;j<=n;j++) if(!was[j]) break;
			sol[i]=j;
			was[j]=1;
			cnt++;
		}
		else
		{
			if(sol[i+1]<sol[i+2])
			{
				int q1=query(i,i+1);
				int d=abs(sol[i+1]-sol[i+2]);
				int h=sol[i+1];
				if(h-q1<=0 || was[h-q1])
				{
					sol[i]=h+q1;
					was[h+q1]=1;
					cnt++;
				}
				else if(h+q1>n || was[h+q1])
				{
					sol[i]=h-q1;
					was[h-q1]=1;
					cnt++;
				}
				else
				{
					int q2=query(i,i+2);
					if(q2==q1+d)
					{
						sol[i]=h-q1;
						was[h-q1]=1;
						cnt++;
					}
					else
					{
						sol[i]=h+q1;
						was[h+q1]=1;
						cnt++;
					}
				}
			}
			else
			{
				int q1=query(i,i+1);
				int d=abs(sol[i+1]-sol[i+2]);
				int h=sol[i+1];
				if(h-q1<=0 || was[h-q1])
				{
					sol[i]=h+q1;
					was[h+q1]=1;
					cnt++;
				}
				else if(h+q1>n || was[h+q1])
				{
					sol[i]=h-q1;
					was[h-q1]=1;
					cnt++;
				}
				else
				{
					int q2=query(i,i+2);
					if(q2==q1+d)
					{
						sol[i]=h+q1;
						was[h+q1]=1;
						cnt++;
					}
					else
					{
						sol[i]=h-q1;
						was[h-q1]=1;
						cnt++;
					}
				}
			}
		}
	}
	for(i=1;i<=n;i++) answer(i,sol[i]);
}
//-----------------------//
/*
int main()
{
	int i;
	scanf("%i",&_N);
	for(i=1;i<=_N;i++) scanf("%i",&_a[i]);
	solve(_N);
	for(i=1;i<=_N;i++) printf("%i ",_b[i]);
	printf("\n");
	return 0;
}
*/
//-----------------------//

Compilation message

xylophone.cpp: In function 'void solve(int)':
xylophone.cpp:30:10: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   mid=top+bot>>1;
       ~~~^~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 296 KB Output is correct
2 Correct 2 ms 440 KB Output is correct
3 Correct 3 ms 440 KB Output is correct
4 Correct 4 ms 448 KB Output is correct
5 Correct 3 ms 464 KB Output is correct
6 Correct 3 ms 464 KB Output is correct
7 Correct 3 ms 464 KB Output is correct
8 Correct 4 ms 464 KB Output is correct
9 Correct 3 ms 464 KB Output is correct
10 Correct 3 ms 556 KB Output is correct
11 Correct 4 ms 556 KB Output is correct
12 Correct 2 ms 572 KB Output is correct
13 Correct 3 ms 572 KB Output is correct
14 Correct 3 ms 572 KB Output is correct
15 Correct 4 ms 572 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 296 KB Output is correct
2 Correct 2 ms 440 KB Output is correct
3 Correct 3 ms 440 KB Output is correct
4 Correct 4 ms 448 KB Output is correct
5 Correct 3 ms 464 KB Output is correct
6 Correct 3 ms 464 KB Output is correct
7 Correct 3 ms 464 KB Output is correct
8 Correct 4 ms 464 KB Output is correct
9 Correct 3 ms 464 KB Output is correct
10 Correct 3 ms 556 KB Output is correct
11 Correct 4 ms 556 KB Output is correct
12 Correct 2 ms 572 KB Output is correct
13 Correct 3 ms 572 KB Output is correct
14 Correct 3 ms 572 KB Output is correct
15 Correct 4 ms 572 KB Output is correct
16 Correct 5 ms 572 KB Output is correct
17 Correct 9 ms 572 KB Output is correct
18 Correct 12 ms 572 KB Output is correct
19 Correct 8 ms 572 KB Output is correct
20 Correct 15 ms 572 KB Output is correct
21 Correct 17 ms 572 KB Output is correct
22 Correct 21 ms 572 KB Output is correct
23 Correct 11 ms 572 KB Output is correct
24 Correct 10 ms 572 KB Output is correct
25 Correct 7 ms 572 KB Output is correct
26 Correct 12 ms 572 KB Output is correct
27 Correct 11 ms 572 KB Output is correct
28 Correct 18 ms 572 KB Output is correct
29 Correct 18 ms 572 KB Output is correct
30 Correct 8 ms 572 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 296 KB Output is correct
2 Correct 2 ms 440 KB Output is correct
3 Correct 3 ms 440 KB Output is correct
4 Correct 4 ms 448 KB Output is correct
5 Correct 3 ms 464 KB Output is correct
6 Correct 3 ms 464 KB Output is correct
7 Correct 3 ms 464 KB Output is correct
8 Correct 4 ms 464 KB Output is correct
9 Correct 3 ms 464 KB Output is correct
10 Correct 3 ms 556 KB Output is correct
11 Correct 4 ms 556 KB Output is correct
12 Correct 2 ms 572 KB Output is correct
13 Correct 3 ms 572 KB Output is correct
14 Correct 3 ms 572 KB Output is correct
15 Correct 4 ms 572 KB Output is correct
16 Correct 5 ms 572 KB Output is correct
17 Correct 9 ms 572 KB Output is correct
18 Correct 12 ms 572 KB Output is correct
19 Correct 8 ms 572 KB Output is correct
20 Correct 15 ms 572 KB Output is correct
21 Correct 17 ms 572 KB Output is correct
22 Correct 21 ms 572 KB Output is correct
23 Correct 11 ms 572 KB Output is correct
24 Correct 10 ms 572 KB Output is correct
25 Correct 7 ms 572 KB Output is correct
26 Correct 12 ms 572 KB Output is correct
27 Correct 11 ms 572 KB Output is correct
28 Correct 18 ms 572 KB Output is correct
29 Correct 18 ms 572 KB Output is correct
30 Correct 8 ms 572 KB Output is correct
31 Correct 27 ms 580 KB Output is correct
32 Correct 29 ms 592 KB Output is correct
33 Correct 70 ms 592 KB Output is correct
34 Correct 79 ms 592 KB Output is correct
35 Correct 67 ms 592 KB Output is correct
36 Correct 48 ms 592 KB Output is correct
37 Correct 34 ms 592 KB Output is correct
38 Correct 60 ms 608 KB Output is correct
39 Correct 53 ms 708 KB Output is correct
40 Correct 64 ms 708 KB Output is correct
41 Correct 56 ms 708 KB Output is correct
42 Correct 60 ms 708 KB Output is correct
43 Correct 23 ms 708 KB Output is correct
44 Correct 54 ms 708 KB Output is correct
45 Correct 72 ms 708 KB Output is correct
46 Correct 46 ms 708 KB Output is correct
47 Correct 53 ms 708 KB Output is correct
48 Correct 68 ms 708 KB Output is correct
49 Correct 57 ms 708 KB Output is correct
50 Correct 29 ms 708 KB Output is correct
51 Correct 62 ms 708 KB Output is correct
52 Correct 48 ms 708 KB Output is correct
53 Correct 61 ms 708 KB Output is correct
54 Correct 79 ms 708 KB Output is correct
55 Correct 58 ms 708 KB Output is correct
56 Correct 45 ms 708 KB Output is correct
57 Correct 82 ms 708 KB Output is correct
58 Correct 76 ms 708 KB Output is correct
59 Correct 81 ms 708 KB Output is correct
60 Correct 55 ms 708 KB Output is correct