Submission #528763

# Submission time Handle Problem Language Result Execution time Memory
528763 2022-02-21T11:00:14 Z c28dnv9q3 Monster Game (JOI21_monster) C++17
100 / 100
134 ms 980 KB
#include "monster.h"
#include <bits/stdc++.h>
#define sz(v) ((int)(v).size())
#define all(v) (v).begin(), (v).end()
using namespace std;
using lint = long long;
using pi = pair<int, int>;
namespace {

	bool example_variable;
	vector<int> ans, p;
	map<pi, int> mp;
	bool cmp(int x, int y){
		if(mp.find(pi(x, y)) != mp.end()) return mp[pi(x, y)];
		return mp[pi(x, y)] = !Query(x, y);
	}
}  // namespace


void solve(int l, int r){
	if(l == r) return;
	int m = (l + r) / 2;
	solve(l, m); solve(m + 1, r);
	int p1 = l, p2 = m + 1;
	vector<int> dap;
	while(p1 < m+1 && p2 < r+1){
		if(cmp(p[p1], p[p2])){
			dap.push_back(p[p1++]);
		}
		else dap.push_back(p[p2++]);
	}
	while(p1 < m+1) dap.push_back(p[p1++]);
	while(p2 < r+1) dap.push_back(p[p2++]);
	for(int i=l; i<=r; i++) p[i] = dap[i - l];
}

std::vector<int> Solve(int N) {
	p.resize(N);
	iota(all(p), 0);
	solve(0, N - 1);
	vector<vector<int>> cycles;
	for(int i = 0; i < N; ){
		vector<int> cur_stack;
		int e = i;
		while(e < N && e < i + 3){
			cur_stack.push_back(p[e++]);
		}
		while(e < N && !(cmp(p[e - 1], cur_stack[0]) && !cmp(p[e], cur_stack[0]))){
			cur_stack.push_back(p[e++]);
		}
		cycles.push_back(cur_stack);
		i = e;
	}
	bool normal = 0;
	for(int i = 0; i < sz(cycles); i++){
		if(sz(cycles[i]) >= 4){
			vector<int> v;
			for(int j = 0; j < sz(cycles[i]); j++){
				if(cmp(cycles[i][j], cycles[i][(j+2)%sz(cycles[i])])){
					v.push_back(j);
				}
			}
			assert(sz(v) == 2);
			if(v[1] - v[0] != 1) swap(v[0], v[1]);
			rotate(cycles[i].begin(), cycles[i].begin() + v[1] + 1, cycles[i].end());
			int idx = i;
			{
				for(int i = idx + 1; i < sz(cycles); i++){
					for(int j = 0; j < sz(cycles[i]); j++){
						if(!cmp(cycles[i-1][0], cycles[i][j])){
							rotate(cycles[i].begin(), cycles[i].begin() + j + 1, cycles[i].end());
							break;
						}
					}
				}
				for(int i = idx - 1; i >= 0; i--){
					for(int j = 0; j < sz(cycles[i]); j++){
						if(!cmp(cycles[i][j], cycles[i + 1].back())){
							rotate(cycles[i].begin(), cycles[i].begin() + j, cycles[i].end());
							break;
						}
					}
				}
			}
			normal = 1;
			break;
		}
	}
	if(!normal){
		for(int i = 0; i < sz(cycles[0]); i++){
			for(int j = 0; j < sz(cycles[1]); j++){
				if(!cmp(cycles[0][i], cycles[1][j])){
					rotate(cycles[0].begin(), cycles[0].begin() + i, cycles[0].end());
					rotate(cycles[1].begin(), cycles[1].begin() + j + 1, cycles[1].end());
					break;
				}
			}
		}
		for(int i = 2; i < sz(cycles); i++){
			for(int j = 0; j < sz(cycles[i]); j++){
				if(!cmp(cycles[i-1][0], cycles[i][j])){
					rotate(cycles[i].begin(), cycles[i].begin() + j + 1, cycles[i].end());
					break;
				}
			}
		}
	}
	vector<int> ans;
	for(auto &i : cycles){
		reverse(all(i));
		for(auto &j : i) ans.push_back(j);
	}
	{
		vector<int> S(N);
		for(int i = 0; i < N; i++) S[ans[i]] = i;
		return S;
	}
}

Compilation message

monster.cpp: In function 'bool {anonymous}::cmp(int, int)':
monster.cpp:15:23: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
   15 |   return mp[pi(x, y)] = !Query(x, y);
monster.cpp: At global scope:
monster.cpp:10:7: warning: '{anonymous}::example_variable' defined but not used [-Wunused-variable]
   10 |  bool example_variable;
      |       ^~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 200 KB Output is correct
2 Correct 1 ms 200 KB Output is correct
3 Correct 1 ms 200 KB Output is correct
4 Correct 1 ms 200 KB Output is correct
5 Correct 1 ms 200 KB Output is correct
6 Correct 0 ms 200 KB Output is correct
7 Correct 1 ms 200 KB Output is correct
8 Correct 0 ms 200 KB Output is correct
9 Correct 1 ms 200 KB Output is correct
10 Correct 1 ms 200 KB Output is correct
11 Correct 1 ms 200 KB Output is correct
12 Correct 1 ms 200 KB Output is correct
13 Correct 1 ms 200 KB Output is correct
14 Correct 1 ms 200 KB Output is correct
15 Correct 1 ms 200 KB Output is correct
16 Correct 12 ms 328 KB Output is correct
17 Correct 12 ms 344 KB Output is correct
18 Correct 14 ms 336 KB Output is correct
19 Correct 16 ms 476 KB Output is correct
20 Correct 23 ms 372 KB Output is correct
21 Correct 1 ms 200 KB Output is correct
22 Correct 1 ms 200 KB Output is correct
23 Correct 0 ms 200 KB Output is correct
24 Correct 1 ms 200 KB Output is correct
25 Correct 1 ms 200 KB Output is correct
26 Correct 7 ms 328 KB Output is correct
27 Correct 0 ms 200 KB Output is correct
28 Correct 1 ms 200 KB Output is correct
29 Correct 1 ms 200 KB Output is correct
30 Correct 1 ms 200 KB Output is correct
31 Correct 1 ms 200 KB Output is correct
32 Correct 7 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 200 KB Output is correct
2 Correct 1 ms 200 KB Output is correct
3 Correct 1 ms 200 KB Output is correct
4 Correct 1 ms 200 KB Output is correct
5 Correct 1 ms 200 KB Output is correct
6 Correct 0 ms 200 KB Output is correct
7 Correct 1 ms 200 KB Output is correct
8 Correct 0 ms 200 KB Output is correct
9 Correct 1 ms 200 KB Output is correct
10 Correct 1 ms 200 KB Output is correct
11 Correct 1 ms 200 KB Output is correct
12 Correct 1 ms 200 KB Output is correct
13 Correct 1 ms 200 KB Output is correct
14 Correct 1 ms 200 KB Output is correct
15 Correct 1 ms 200 KB Output is correct
16 Correct 12 ms 328 KB Output is correct
17 Correct 12 ms 344 KB Output is correct
18 Correct 14 ms 336 KB Output is correct
19 Correct 16 ms 476 KB Output is correct
20 Correct 23 ms 372 KB Output is correct
21 Correct 1 ms 200 KB Output is correct
22 Correct 1 ms 200 KB Output is correct
23 Correct 0 ms 200 KB Output is correct
24 Correct 1 ms 200 KB Output is correct
25 Correct 1 ms 200 KB Output is correct
26 Correct 7 ms 328 KB Output is correct
27 Correct 0 ms 200 KB Output is correct
28 Correct 1 ms 200 KB Output is correct
29 Correct 1 ms 200 KB Output is correct
30 Correct 1 ms 200 KB Output is correct
31 Correct 1 ms 200 KB Output is correct
32 Correct 7 ms 348 KB Output is correct
33 Correct 90 ms 940 KB Output is correct
34 Correct 74 ms 832 KB Output is correct
35 Correct 57 ms 832 KB Output is correct
36 Correct 134 ms 788 KB Output is correct
37 Correct 101 ms 860 KB Output is correct
38 Correct 106 ms 848 KB Output is correct
39 Correct 69 ms 888 KB Output is correct
40 Correct 76 ms 980 KB Output is correct
41 Correct 88 ms 828 KB Output is correct
42 Correct 99 ms 816 KB Output is correct
43 Correct 49 ms 628 KB Output is correct
44 Correct 59 ms 716 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 102 ms 808 KB Output is correct
2 Correct 79 ms 852 KB Output is correct
3 Correct 79 ms 956 KB Output is correct
4 Correct 99 ms 888 KB Output is correct
5 Correct 84 ms 896 KB Output is correct
6 Correct 106 ms 748 KB Output is correct
7 Correct 72 ms 704 KB Output is correct
8 Correct 89 ms 832 KB Output is correct
9 Correct 94 ms 848 KB Output is correct
10 Correct 71 ms 936 KB Output is correct
11 Correct 106 ms 844 KB Output is correct
12 Correct 75 ms 832 KB Output is correct
13 Correct 91 ms 820 KB Output is correct
14 Correct 87 ms 756 KB Output is correct
15 Correct 45 ms 656 KB Output is correct
16 Correct 64 ms 640 KB Output is correct
17 Correct 93 ms 820 KB Output is correct
18 Correct 54 ms 616 KB Output is correct