Submission #876616

# Submission time Handle Problem Language Result Execution time Memory
876616 2023-11-22T05:43:06 Z dsyz Xylophone (JOI18_xylophone) C++17
100 / 100
39 ms 856 KB
#include <bits/stdc++.h>
#include "xylophone.h"
using namespace std;
using ll = long long;
#define MAXN (1000005)
 
void solve(int N) {
	ll low = 1;
	ll high = N + 1;
	while(high - low > 1){
		ll mid = (high + low) / 2;
		ll diff = query(mid,N);
		if(diff == N - 1){
			low = mid;
		}else{
			high = mid;
		}
	}
	ll posof1 = low;
	ll val[N + 1]; //pos of value i
	memset(val,-1,sizeof(val));
	val[1] = posof1;
	ll pos[N + 1]; //value of pos i
	memset(pos,-1,sizeof(pos));
	pos[posof1] = 1;
	for(ll i = posof1 + 1;i <= N;i++){ //all index are 1-indexed
		ll adjdiff = query(i - 1,i);
		if(pos[i - 1] - adjdiff < 1 || val[pos[i - 1] - adjdiff] != -1){
			pos[i] = pos[i - 1] + adjdiff;
			val[pos[i - 1] + adjdiff] = i;
		}else if(pos[i - 1] + adjdiff > N || val[pos[i - 1] + adjdiff] != -1){
			pos[i] = pos[i - 1] - adjdiff;
			val[pos[i - 1] - adjdiff] = i;
		}else{ //I can prove this is at least 2 spaces away from val 1
			ll diff = query(i - 2,i);
			ll I1 = pos[i - 1] - adjdiff;
			ll I2 = pos[i - 1] + adjdiff;
			if(diff == abs(pos[i - 1] - pos[i - 2])){
				assert(pos[i - 1] != pos[i - 2]);
				if(pos[i - 1] > pos[i - 2]){
					pos[i] = pos[i - 1] - adjdiff;
					val[pos[i - 1] - adjdiff] = i;
				}else{
					pos[i] = pos[i - 1] + adjdiff;
					val[pos[i - 1] + adjdiff] = i;
				}
			}else if(diff == max({I1,pos[i - 1],pos[i - 2]}) - min({I1,pos[i - 1],pos[i - 2]})){
				pos[i] = I1;
				val[I1] = i;
			}else if(diff == max({I2,pos[i - 1],pos[i - 2]}) - min({I2,pos[i - 1],pos[i - 2]})){
				pos[i] = I2;
				val[I2] = i;
			}
		}
	}
	
	for(ll i = posof1 - 1;i >= 1;i--){ //all index are 1-indexed
		ll adjdiff = query(i,i + 1);
		if(pos[i + 1] - adjdiff < 1 || val[pos[i + 1] - adjdiff] != -1){
			pos[i] = pos[i + 1] + adjdiff;
			val[pos[i + 1] + adjdiff] = i;
		}else if(pos[i + 1] + adjdiff > N || val[pos[i + 1] + adjdiff] != -1){
			pos[i] = pos[i + 1] - adjdiff;
			val[pos[i + 1] - adjdiff] = i;
		}else{ //I can prove this is at least 2 spaces away from val 1
			ll diff = query(i,i + 2);
			ll I1 = pos[i + 1] - adjdiff;
			ll I2 = pos[i + 1] + adjdiff;
			if(diff == abs(pos[i + 1] - pos[i + 2])){
				assert(pos[i + 1] != pos[i + 2]);
				if(pos[i + 1] > pos[i + 2]){
					pos[i] = pos[i + 1] - adjdiff;
					val[pos[i + 1] - adjdiff] = i;
				}else{
					pos[i] = pos[i + 1] + adjdiff;
					val[pos[i + 1] + adjdiff] = i;
				}
			}else if(diff == max({I1,pos[i + 1],pos[i + 2]}) - min({I1,pos[i + 1],pos[i + 2]})){
				pos[i] = I1;
				val[I1] = i;
			}else if(diff == max({I2,pos[i + 1],pos[i + 2]}) - min({I2,pos[i + 1],pos[i + 2]})){
				pos[i] = I2;
				val[I2] = i;
			}
		}
	}
	for(ll i = 1;i <= N;i++){
		answer(i,pos[i]);
	}
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 596 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
7 Correct 1 ms 344 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
12 Correct 1 ms 344 KB Output is correct
13 Correct 1 ms 344 KB Output is correct
14 Correct 1 ms 344 KB Output is correct
15 Correct 1 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 596 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
7 Correct 1 ms 344 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
12 Correct 1 ms 344 KB Output is correct
13 Correct 1 ms 344 KB Output is correct
14 Correct 1 ms 344 KB Output is correct
15 Correct 1 ms 344 KB Output is correct
16 Correct 1 ms 432 KB Output is correct
17 Correct 3 ms 856 KB Output is correct
18 Correct 5 ms 600 KB Output is correct
19 Correct 5 ms 436 KB Output is correct
20 Correct 8 ms 692 KB Output is correct
21 Correct 6 ms 692 KB Output is correct
22 Correct 6 ms 600 KB Output is correct
23 Correct 4 ms 448 KB Output is correct
24 Correct 4 ms 692 KB Output is correct
25 Correct 4 ms 696 KB Output is correct
26 Correct 5 ms 856 KB Output is correct
27 Correct 5 ms 692 KB Output is correct
28 Correct 5 ms 692 KB Output is correct
29 Correct 6 ms 436 KB Output is correct
30 Correct 5 ms 436 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 596 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
7 Correct 1 ms 344 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
12 Correct 1 ms 344 KB Output is correct
13 Correct 1 ms 344 KB Output is correct
14 Correct 1 ms 344 KB Output is correct
15 Correct 1 ms 344 KB Output is correct
16 Correct 1 ms 432 KB Output is correct
17 Correct 3 ms 856 KB Output is correct
18 Correct 5 ms 600 KB Output is correct
19 Correct 5 ms 436 KB Output is correct
20 Correct 8 ms 692 KB Output is correct
21 Correct 6 ms 692 KB Output is correct
22 Correct 6 ms 600 KB Output is correct
23 Correct 4 ms 448 KB Output is correct
24 Correct 4 ms 692 KB Output is correct
25 Correct 4 ms 696 KB Output is correct
26 Correct 5 ms 856 KB Output is correct
27 Correct 5 ms 692 KB Output is correct
28 Correct 5 ms 692 KB Output is correct
29 Correct 6 ms 436 KB Output is correct
30 Correct 5 ms 436 KB Output is correct
31 Correct 10 ms 688 KB Output is correct
32 Correct 22 ms 680 KB Output is correct
33 Correct 29 ms 476 KB Output is correct
34 Correct 27 ms 684 KB Output is correct
35 Correct 35 ms 684 KB Output is correct
36 Correct 24 ms 684 KB Output is correct
37 Correct 21 ms 504 KB Output is correct
38 Correct 28 ms 436 KB Output is correct
39 Correct 31 ms 484 KB Output is correct
40 Correct 35 ms 484 KB Output is correct
41 Correct 23 ms 684 KB Output is correct
42 Correct 21 ms 488 KB Output is correct
43 Correct 23 ms 492 KB Output is correct
44 Correct 31 ms 688 KB Output is correct
45 Correct 39 ms 484 KB Output is correct
46 Correct 28 ms 492 KB Output is correct
47 Correct 29 ms 484 KB Output is correct
48 Correct 25 ms 504 KB Output is correct
49 Correct 26 ms 680 KB Output is correct
50 Correct 37 ms 484 KB Output is correct
51 Correct 33 ms 492 KB Output is correct
52 Correct 31 ms 484 KB Output is correct
53 Correct 24 ms 684 KB Output is correct
54 Correct 32 ms 680 KB Output is correct
55 Correct 33 ms 680 KB Output is correct
56 Correct 27 ms 684 KB Output is correct
57 Correct 34 ms 484 KB Output is correct
58 Correct 27 ms 688 KB Output is correct
59 Correct 29 ms 492 KB Output is correct
60 Correct 27 ms 488 KB Output is correct