답안 #800137

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
800137 2023-08-01T10:49:04 Z ymm Monster Game (JOI21_monster) C++17
100 / 100
85 ms 1256 KB
#include "monster.h"
#include <bits/stdc++.h>
#define Loop(x,l,r) for (ll x = (l); x < (r); ++x)
#define LoopR(x,l,r) for (ll x = (r)-1; x >= (l); --x)
typedef long long ll;
typedef std::pair<int,int> pii;
using namespace std;

namespace {

const int N = 1024;
map<pii,int> mp;

bool cmp(int i, int j)
{
	assert(i != j);
	if (i < j) {
		int &x = mp[pii(i, j)];
		if (!x)
			x = !Query(i, j)+1;
		return x-1;
	} else {
		swap(i, j);
		int &x = mp[pii(i, j)];
		if (!x)
			x = !Query(i, j)+1;
		return 2-x;
	}
}

vector<int> merge(vector<int> a, vector<int> b) {
	vector<int> ans;
	int p0 = 0, p1 = 0;
	while (p0 < a.size() || p1 < b.size()) {
		if (p1 == b.size() || (p0 != a.size() && cmp(a[p0], b[p1])))
			ans.push_back(a[p0++]);
		else
			ans.push_back(b[p1++]);
	}
	return ans;
}

vector<int> chain[N];
struct chain_cmp {
	bool operator()(const vector<int> *a, const vector<int> *b) const { 
		return a->size() > b->size();
	}
};

vector<int> get_chain(int n)
{
	priority_queue<vector<int> *, vector<vector<int> *>, chain_cmp> pq;
	Loop (i,0,n) {
		chain[i] = {i};
		pq.push(chain+i);
	}
	while (pq.size() > 1) {
		auto a = pq.top(); pq.pop();
		auto b = pq.top(); pq.pop();
		*a = merge(*a, *b);
		pq.push(a);
	}
	return *pq.top();
}

vector<int> mysolve(vector<int> vec)
{
	if (vec.size() == 1)
		return vec;
	if (vec.size() == 2)
		return {vec[1], vec[0]};
	assert(vec.size() != 3);
	int n = vec.size();
	vector<int> ans;
	int p0 = 0, p1 = 0;
	if (cmp(vec[0], vec[2]) && cmp(vec[1], vec[3])) {
		ans.push_back(vec[1]);
		ans.push_back(vec[0]);
		p0 = p1 = 2;
	} else if (cmp(vec[0], vec[2]) && !cmp(vec[1], vec[3])) {
		ans.push_back(vec[0]);
		p0 = p1 = 1;
	} else if (cmp(vec[3], vec[1]) && cmp(vec[3], vec[0])) {
		p0 = 0, p1 = 3;
		while (p1 < n-1) {
			if (!cmp(vec[p1+1], vec[1]))
				break;
			++p1;
		}
		LoopR (i,0,p1+1)
			ans.push_back(vec[i]);
		p0 = p1 = p1+1;
	} else {
		if (vec.size() != 6) {
			auto tmp = mysolve(vector<int>(vec.begin()+3, vec.end()));
			if (!cmp(vec[0], tmp[0]))
				ans = {vec[2], vec[1], vec[0]};
			else if (!cmp(vec[1], tmp[0]))
				ans = {vec[0], vec[2], vec[1]};
			else
				ans = {vec[1], vec[0], vec[2]};
			ans.insert(ans.end(), tmp.begin(), tmp.end());
			return ans;
		}
		int lst = -1, fst = -1;
		Loop (i,0,3) Loop (j,3,6) {
			if (!cmp(vec[i], vec[j])) {
				lst = vec[i];
				fst = vec[j];
				break;
			}
		}
		vector<int> v1, v2;
		v1 = {vec[2], vec[1], vec[0]};
		v2 = {vec[5], vec[4], vec[3]};
		while (v1.back() != lst) {
			int x = v1.back();
			v1.pop_back();
			v1.insert(v1.begin(), x);
		}
		while (v2.front() != fst) {
			int x = v2.back();
			v2.pop_back();
			v2.insert(v2.begin(), x);
		}
		ans = v1;
		ans.insert(ans.end(), v2.begin(), v2.end());
		return ans;
	}
	while (p0 < n) {
		while (p1 < n-1) {
			if (!cmp(ans.back(), vec[p1]))
				break;
			++p1;
		}
		LoopR (i,p0,p1+1)
			ans.push_back(vec[i]);
		p0 = p1 = p1+1;
	}
	return ans;
}

}  // namespace

std::vector<int> Solve(int n) {
	mp.clear();
	vector<int> vec = get_chain(n);
	vector<int> ans = mysolve(vec);
	vector<int> fans(n);
	Loop (i,0,n)
		fans[ans[i]] = i;
	return fans;
}

Compilation message

monster.cpp: In function 'std::vector<int> {anonymous}::merge(std::vector<int>, std::vector<int>)':
monster.cpp:34:12: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   34 |  while (p0 < a.size() || p1 < b.size()) {
      |         ~~~^~~~~~~~~~
monster.cpp:34:29: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   34 |  while (p0 < a.size() || p1 < b.size()) {
      |                          ~~~^~~~~~~~~~
monster.cpp:35:10: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   35 |   if (p1 == b.size() || (p0 != a.size() && cmp(a[p0], b[p1])))
      |       ~~~^~~~~~~~~~~
monster.cpp:35:29: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   35 |   if (p1 == b.size() || (p0 != a.size() && cmp(a[p0], b[p1])))
      |                          ~~~^~~~~~~~~~~
monster.cpp: In function 'std::vector<int> {anonymous}::get_chain(int)':
monster.cpp:54:15: warning: narrowing conversion of 'i' from 'll' {aka 'long long int'} to 'int' [-Wnarrowing]
   54 |   chain[i] = {i};
      |               ^
monster.cpp:54:15: warning: narrowing conversion of 'i' from 'll' {aka 'long long int'} to 'int' [-Wnarrowing]
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 208 KB Output is correct
2 Correct 0 ms 208 KB Output is correct
3 Correct 0 ms 208 KB Output is correct
4 Correct 1 ms 208 KB Output is correct
5 Correct 0 ms 208 KB Output is correct
6 Correct 0 ms 208 KB Output is correct
7 Correct 0 ms 208 KB Output is correct
8 Correct 1 ms 208 KB Output is correct
9 Correct 0 ms 208 KB Output is correct
10 Correct 0 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 0 ms 208 KB Output is correct
14 Correct 0 ms 208 KB Output is correct
15 Correct 1 ms 208 KB Output is correct
16 Correct 12 ms 388 KB Output is correct
17 Correct 12 ms 336 KB Output is correct
18 Correct 12 ms 396 KB Output is correct
19 Correct 14 ms 380 KB Output is correct
20 Correct 10 ms 372 KB Output is correct
21 Correct 0 ms 208 KB Output is correct
22 Correct 1 ms 208 KB Output is correct
23 Correct 0 ms 208 KB Output is correct
24 Correct 0 ms 208 KB Output is correct
25 Correct 0 ms 208 KB Output is correct
26 Correct 12 ms 372 KB Output is correct
27 Correct 0 ms 208 KB Output is correct
28 Correct 1 ms 208 KB Output is correct
29 Correct 0 ms 208 KB Output is correct
30 Correct 0 ms 208 KB Output is correct
31 Correct 0 ms 208 KB Output is correct
32 Correct 8 ms 336 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 208 KB Output is correct
2 Correct 0 ms 208 KB Output is correct
3 Correct 0 ms 208 KB Output is correct
4 Correct 1 ms 208 KB Output is correct
5 Correct 0 ms 208 KB Output is correct
6 Correct 0 ms 208 KB Output is correct
7 Correct 0 ms 208 KB Output is correct
8 Correct 1 ms 208 KB Output is correct
9 Correct 0 ms 208 KB Output is correct
10 Correct 0 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 0 ms 208 KB Output is correct
14 Correct 0 ms 208 KB Output is correct
15 Correct 1 ms 208 KB Output is correct
16 Correct 12 ms 388 KB Output is correct
17 Correct 12 ms 336 KB Output is correct
18 Correct 12 ms 396 KB Output is correct
19 Correct 14 ms 380 KB Output is correct
20 Correct 10 ms 372 KB Output is correct
21 Correct 0 ms 208 KB Output is correct
22 Correct 1 ms 208 KB Output is correct
23 Correct 0 ms 208 KB Output is correct
24 Correct 0 ms 208 KB Output is correct
25 Correct 0 ms 208 KB Output is correct
26 Correct 12 ms 372 KB Output is correct
27 Correct 0 ms 208 KB Output is correct
28 Correct 1 ms 208 KB Output is correct
29 Correct 0 ms 208 KB Output is correct
30 Correct 0 ms 208 KB Output is correct
31 Correct 0 ms 208 KB Output is correct
32 Correct 8 ms 336 KB Output is correct
33 Correct 80 ms 920 KB Output is correct
34 Correct 47 ms 1004 KB Output is correct
35 Correct 59 ms 856 KB Output is correct
36 Correct 84 ms 944 KB Output is correct
37 Correct 55 ms 876 KB Output is correct
38 Correct 71 ms 1092 KB Output is correct
39 Correct 54 ms 912 KB Output is correct
40 Correct 51 ms 884 KB Output is correct
41 Correct 79 ms 904 KB Output is correct
42 Correct 52 ms 932 KB Output is correct
43 Correct 76 ms 820 KB Output is correct
44 Correct 54 ms 936 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 63 ms 924 KB Output is correct
2 Correct 57 ms 868 KB Output is correct
3 Correct 64 ms 1256 KB Output is correct
4 Correct 76 ms 888 KB Output is correct
5 Correct 81 ms 876 KB Output is correct
6 Correct 82 ms 944 KB Output is correct
7 Correct 70 ms 1036 KB Output is correct
8 Correct 81 ms 944 KB Output is correct
9 Correct 63 ms 964 KB Output is correct
10 Correct 68 ms 932 KB Output is correct
11 Correct 73 ms 908 KB Output is correct
12 Correct 63 ms 888 KB Output is correct
13 Correct 85 ms 860 KB Output is correct
14 Correct 81 ms 840 KB Output is correct
15 Correct 50 ms 864 KB Output is correct
16 Correct 43 ms 880 KB Output is correct
17 Correct 65 ms 936 KB Output is correct
18 Correct 74 ms 900 KB Output is correct