Submission #704229

# Submission time Handle Problem Language Result Execution time Memory
704229 2023-03-02T01:16:23 Z TranGiaHuy1508 Xylophone (JOI18_xylophone) C++17
100 / 100
66 ms 596 KB
#include <bits/stdc++.h>
using namespace std;

#include "xylophone.h"

void solve(int N){
	int pos = 1;
	{
		int l = 1, r = N;
		while (r - l > 1){
			int mid = (l + r) >> 1;
			if (query(mid, N) == N-1) l = mid;
			else r = mid-1;
		}
		if (l != r){
			if (query(r, N) == N-1) pos = r;
			else pos = l;
		}
		else pos = l;
	}

	vector<int> res(N + 1, 0);
	set<int> alr;

	res[pos] = 1; alr.insert(1);

	for (int i = pos + 1; i <= N; i++){
		int diff = query(i - 1, i);
		vector<int> opts;
		for (int sign: {-1, 1}){
			int cand = res[i - 1] + sign * diff;
			if (1 <= cand && cand <= N && !alr.count(cand)) opts.push_back(cand);
		}

		if ((int)opts.size() == 1){
			res[i] = opts[0];
			alr.insert(res[i]);
		}
		else{
			int d2 = query(i - 2, i);
			for (int j = 0; j < (int)opts.size(); j++){
				if (d2 == max({res[i - 2], res[i - 1], opts[j]}) - min({res[i - 2], res[i - 1], opts[j]})){
					res[i] = opts[j];
					alr.insert(res[i]);
					break;
				}
			}
		}
	}

	for (int i = pos - 1; i >= 1; i--){
		int diff = query(i, i + 1);
		vector<int> opts;
		for (int sign: {-1, 1}){
			int cand = res[i + 1] + sign * diff;
			if (1 <= cand && cand <= N && !alr.count(cand)) opts.push_back(cand);
		}

		if ((int)opts.size() == 1){
			res[i] = opts[0];
			alr.insert(res[i]);
		}
		else{
			int d2 = query(i, i + 2);
			for (int j = 0; j < (int)opts.size(); j++){
				if (d2 == max({res[i + 2], res[i + 1], opts[j]}) - min({res[i + 2], res[i + 1], opts[j]})){
					res[i] = opts[j];
					alr.insert(res[i]);
					break;
				}
			}
		}
	}

	for (int i = 1; i <= N; i++) answer(i, res[i]);
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 208 KB Output is correct
2 Correct 0 ms 208 KB Output is correct
3 Correct 1 ms 208 KB Output is correct
4 Correct 2 ms 208 KB Output is correct
5 Correct 1 ms 208 KB Output is correct
6 Correct 1 ms 208 KB Output is correct
7 Correct 1 ms 208 KB Output is correct
8 Correct 2 ms 208 KB Output is correct
9 Correct 1 ms 208 KB Output is correct
10 Correct 1 ms 208 KB Output is correct
11 Correct 1 ms 208 KB Output is correct
12 Correct 1 ms 208 KB Output is correct
13 Correct 2 ms 208 KB Output is correct
14 Correct 2 ms 208 KB Output is correct
15 Correct 2 ms 208 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 208 KB Output is correct
2 Correct 0 ms 208 KB Output is correct
3 Correct 1 ms 208 KB Output is correct
4 Correct 2 ms 208 KB Output is correct
5 Correct 1 ms 208 KB Output is correct
6 Correct 1 ms 208 KB Output is correct
7 Correct 1 ms 208 KB Output is correct
8 Correct 2 ms 208 KB Output is correct
9 Correct 1 ms 208 KB Output is correct
10 Correct 1 ms 208 KB Output is correct
11 Correct 1 ms 208 KB Output is correct
12 Correct 1 ms 208 KB Output is correct
13 Correct 2 ms 208 KB Output is correct
14 Correct 2 ms 208 KB Output is correct
15 Correct 2 ms 208 KB Output is correct
16 Correct 2 ms 208 KB Output is correct
17 Correct 5 ms 308 KB Output is correct
18 Correct 9 ms 332 KB Output is correct
19 Correct 11 ms 324 KB Output is correct
20 Correct 11 ms 336 KB Output is correct
21 Correct 6 ms 296 KB Output is correct
22 Correct 13 ms 336 KB Output is correct
23 Correct 9 ms 316 KB Output is correct
24 Correct 12 ms 312 KB Output is correct
25 Correct 9 ms 304 KB Output is correct
26 Correct 8 ms 320 KB Output is correct
27 Correct 10 ms 316 KB Output is correct
28 Correct 10 ms 336 KB Output is correct
29 Correct 11 ms 336 KB Output is correct
30 Correct 9 ms 304 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 208 KB Output is correct
2 Correct 0 ms 208 KB Output is correct
3 Correct 1 ms 208 KB Output is correct
4 Correct 2 ms 208 KB Output is correct
5 Correct 1 ms 208 KB Output is correct
6 Correct 1 ms 208 KB Output is correct
7 Correct 1 ms 208 KB Output is correct
8 Correct 2 ms 208 KB Output is correct
9 Correct 1 ms 208 KB Output is correct
10 Correct 1 ms 208 KB Output is correct
11 Correct 1 ms 208 KB Output is correct
12 Correct 1 ms 208 KB Output is correct
13 Correct 2 ms 208 KB Output is correct
14 Correct 2 ms 208 KB Output is correct
15 Correct 2 ms 208 KB Output is correct
16 Correct 2 ms 208 KB Output is correct
17 Correct 5 ms 308 KB Output is correct
18 Correct 9 ms 332 KB Output is correct
19 Correct 11 ms 324 KB Output is correct
20 Correct 11 ms 336 KB Output is correct
21 Correct 6 ms 296 KB Output is correct
22 Correct 13 ms 336 KB Output is correct
23 Correct 9 ms 316 KB Output is correct
24 Correct 12 ms 312 KB Output is correct
25 Correct 9 ms 304 KB Output is correct
26 Correct 8 ms 320 KB Output is correct
27 Correct 10 ms 316 KB Output is correct
28 Correct 10 ms 336 KB Output is correct
29 Correct 11 ms 336 KB Output is correct
30 Correct 9 ms 304 KB Output is correct
31 Correct 19 ms 344 KB Output is correct
32 Correct 33 ms 428 KB Output is correct
33 Correct 42 ms 472 KB Output is correct
34 Correct 63 ms 524 KB Output is correct
35 Correct 54 ms 488 KB Output is correct
36 Correct 52 ms 496 KB Output is correct
37 Correct 39 ms 596 KB Output is correct
38 Correct 45 ms 512 KB Output is correct
39 Correct 62 ms 580 KB Output is correct
40 Correct 48 ms 456 KB Output is correct
41 Correct 63 ms 484 KB Output is correct
42 Correct 49 ms 524 KB Output is correct
43 Correct 39 ms 524 KB Output is correct
44 Correct 45 ms 500 KB Output is correct
45 Correct 41 ms 500 KB Output is correct
46 Correct 47 ms 496 KB Output is correct
47 Correct 57 ms 536 KB Output is correct
48 Correct 58 ms 460 KB Output is correct
49 Correct 64 ms 584 KB Output is correct
50 Correct 66 ms 452 KB Output is correct
51 Correct 61 ms 528 KB Output is correct
52 Correct 61 ms 464 KB Output is correct
53 Correct 61 ms 584 KB Output is correct
54 Correct 63 ms 524 KB Output is correct
55 Correct 53 ms 520 KB Output is correct
56 Correct 53 ms 524 KB Output is correct
57 Correct 63 ms 456 KB Output is correct
58 Correct 55 ms 536 KB Output is correct
59 Correct 53 ms 480 KB Output is correct
60 Correct 58 ms 468 KB Output is correct