답안 #447245

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
447245 2021-07-25T12:22:06 Z jwvg0425 카멜레온의 사랑 (JOI20_chameleon) C++17
100 / 100
95 ms 592 KB
#include "chameleon.h"
#include <stdio.h>
#include <vector>
#include <queue>
#include <algorithm>
#include <iostream>
#include <string>
#include <bitset>
#include <map>
#include <set>
#include <tuple>
#include <string.h>
#include <math.h>
#include <random>
#include <functional>
#include <assert.h>
#include <math.h>
#define all(x) (x).begin(), (x).end()
#define xx first
#define yy second

using namespace std;

template<typename T, typename Pr = less<T>>
using pq = priority_queue<T, vector<T>, Pr>;
using i64 = long long int;
using ii = pair<int, int>;
using ii64 = pair<i64, i64>;

vector<int> con[1005];
bool visit[1005];
bool answered[1005];
int l[1005];

int Query(const vector<int>& ys, int x, int s, int e)
{
	vector<int> v = { x };
	for (int i = s; i <= e; i++)
		v.push_back(ys[i]);

	return Query(v);
}

int Bisearch(const vector<int>& ys, int x, int l, int r, int d)
{
	int lo = l, hi = r;
	int res = -1;

	while (lo <= hi)
	{
		int mid = (lo + hi) / 2;
		int q = Query(ys, x, l, mid);
		int sz = mid - l + 2;
		if (q <= sz + d)
		{
			res = mid;
			hi = mid - 1;
		}
		else
		{
			lo = mid + 1;
		}
	}

	return res;
}

vector<int> xs;
vector<int> ys;
int color[1005];

void dfs(int root, int c)
{
	if (color[root] != -1)
		return;

	color[root] = c;

	for (auto& e : con[root])
		dfs(e, (c + 1) % 2);
}

void Rebuild(int n)
{
	// 1 .. i를 적절히 분류
	memset(color, -1, sizeof(color));

	for (int i = 1; i <= n; i++)
		dfs(i, 0);

	xs.clear();
	ys.clear();

	for (int i = 1; i <= n; i++)
	{
		if (color[i] == 0)
			xs.push_back(i);
		else
			ys.push_back(i);
	}
}

void Solve(int N)
{
	for (int i = 1; i <= 2 * N; i++)
	{
		vector<int> over;

		while (Query(xs, i, 0, (int)xs.size() - 1) != xs.size() + 1)
		{
			auto id = Bisearch(xs, i, 0, (int)xs.size() - 1, -1);
			over.push_back(xs[id]);
			xs.erase(xs.begin() + id);
		}

		while (Query(ys, i, 0, (int)ys.size() - 1) != ys.size() + 1)
		{
			auto id = Bisearch(ys, i, 0, (int)ys.size() - 1, -1);
			over.push_back(ys[id]);
			ys.erase(ys.begin() + id);
		}

		for (auto& o : over)
		{
			con[i].push_back(o);
			con[o].push_back(i);
		}

		Rebuild(i);
	}

	for (int i = 1; i <= 2 * N; i++)
	{
		if (visit[i])
			continue;

		if (con[i].size() == 1)
		{
			Answer(i, con[i][0]);
			visit[i] = visit[con[i][0]] = true;
			answered[i] = answered[con[i][0]] = true;
			continue;
		}

		if (con[con[i][0]].size() == 1)
		{
			Answer(i, con[i][0]);
			visit[i] = visit[con[i][0]] = true;
			answered[i] = answered[con[i][0]] = true;
		}
		else if (con[con[i][1]].size() == 1)
		{
			Answer(i, con[i][1]);
			visit[i] = visit[con[i][1]] = true;
			answered[i] = answered[con[i][1]] = true;
		}
		else if (con[con[i][2]].size() == 1)
		{
			Answer(i, con[i][2]);
			visit[i] = visit[con[i][2]] = true;
			answered[i] = answered[con[i][2]] = true;
		}
		else
		{
			vector<int> a = { i, con[i][0], con[i][1] };
			vector<int> b = { i, con[i][0], con[i][2] };
			vector<int> c = { i, con[i][1], con[i][2] };

			auto aq = Query(a);
			auto bq = Query(b);

			if (aq == 1)
			{
				visit[i] = true;
				l[i] = con[i][2];
			}
			else if (bq == 1)
			{
				visit[i] = true;
				l[i] = con[i][1];
			}
			else
			{
				visit[i] = true;
				l[i] = con[i][0];
			}
		}
	}

	for (int i = 1; i <= 2 * N; i++)
	{
		if (answered[i])
			continue;

		for (auto& c : con[i])
		{
			if (c == l[i] || l[c] == i || answered[c])
				continue;

			Answer(i, c);
			answered[i] = answered[c] = true;
			break;
		}
	}
}

Compilation message

chameleon.cpp: In function 'void Solve(int)':
chameleon.cpp:109:46: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  109 |   while (Query(xs, i, 0, (int)xs.size() - 1) != xs.size() + 1)
      |          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~
chameleon.cpp:116:46: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  116 |   while (Query(ys, i, 0, (int)ys.size() - 1) != ys.size() + 1)
      |          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 328 KB Output is correct
2 Correct 0 ms 328 KB Output is correct
3 Correct 38 ms 420 KB Output is correct
4 Correct 39 ms 480 KB Output is correct
5 Correct 40 ms 352 KB Output is correct
6 Correct 40 ms 328 KB Output is correct
7 Correct 39 ms 364 KB Output is correct
8 Correct 39 ms 328 KB Output is correct
9 Correct 44 ms 364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 328 KB Output is correct
2 Correct 0 ms 328 KB Output is correct
3 Correct 0 ms 328 KB Output is correct
4 Correct 0 ms 328 KB Output is correct
5 Correct 1 ms 328 KB Output is correct
6 Correct 1 ms 328 KB Output is correct
7 Correct 0 ms 328 KB Output is correct
8 Correct 0 ms 328 KB Output is correct
9 Correct 1 ms 328 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 328 KB Output is correct
2 Correct 0 ms 328 KB Output is correct
3 Correct 0 ms 328 KB Output is correct
4 Correct 0 ms 328 KB Output is correct
5 Correct 1 ms 328 KB Output is correct
6 Correct 1 ms 328 KB Output is correct
7 Correct 0 ms 328 KB Output is correct
8 Correct 0 ms 328 KB Output is correct
9 Correct 1 ms 328 KB Output is correct
10 Correct 1 ms 328 KB Output is correct
11 Correct 1 ms 328 KB Output is correct
12 Correct 1 ms 328 KB Output is correct
13 Correct 1 ms 328 KB Output is correct
14 Correct 1 ms 328 KB Output is correct
15 Correct 1 ms 328 KB Output is correct
16 Correct 2 ms 328 KB Output is correct
17 Correct 2 ms 328 KB Output is correct
18 Correct 1 ms 328 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 328 KB Output is correct
2 Correct 0 ms 328 KB Output is correct
3 Correct 89 ms 360 KB Output is correct
4 Correct 93 ms 376 KB Output is correct
5 Correct 92 ms 352 KB Output is correct
6 Correct 90 ms 328 KB Output is correct
7 Correct 95 ms 368 KB Output is correct
8 Correct 91 ms 328 KB Output is correct
9 Correct 94 ms 592 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 328 KB Output is correct
2 Correct 0 ms 328 KB Output is correct
3 Correct 38 ms 420 KB Output is correct
4 Correct 39 ms 480 KB Output is correct
5 Correct 40 ms 352 KB Output is correct
6 Correct 40 ms 328 KB Output is correct
7 Correct 39 ms 364 KB Output is correct
8 Correct 39 ms 328 KB Output is correct
9 Correct 44 ms 364 KB Output is correct
10 Correct 1 ms 328 KB Output is correct
11 Correct 0 ms 328 KB Output is correct
12 Correct 0 ms 328 KB Output is correct
13 Correct 0 ms 328 KB Output is correct
14 Correct 1 ms 328 KB Output is correct
15 Correct 1 ms 328 KB Output is correct
16 Correct 0 ms 328 KB Output is correct
17 Correct 0 ms 328 KB Output is correct
18 Correct 1 ms 328 KB Output is correct
19 Correct 1 ms 328 KB Output is correct
20 Correct 1 ms 328 KB Output is correct
21 Correct 1 ms 328 KB Output is correct
22 Correct 1 ms 328 KB Output is correct
23 Correct 1 ms 328 KB Output is correct
24 Correct 1 ms 328 KB Output is correct
25 Correct 2 ms 328 KB Output is correct
26 Correct 2 ms 328 KB Output is correct
27 Correct 1 ms 328 KB Output is correct
28 Correct 0 ms 328 KB Output is correct
29 Correct 0 ms 328 KB Output is correct
30 Correct 89 ms 360 KB Output is correct
31 Correct 93 ms 376 KB Output is correct
32 Correct 92 ms 352 KB Output is correct
33 Correct 90 ms 328 KB Output is correct
34 Correct 95 ms 368 KB Output is correct
35 Correct 91 ms 328 KB Output is correct
36 Correct 94 ms 592 KB Output is correct
37 Correct 79 ms 356 KB Output is correct
38 Correct 40 ms 364 KB Output is correct
39 Correct 84 ms 484 KB Output is correct
40 Correct 76 ms 364 KB Output is correct
41 Correct 76 ms 380 KB Output is correct
42 Correct 39 ms 364 KB Output is correct
43 Correct 94 ms 464 KB Output is correct
44 Correct 75 ms 328 KB Output is correct
45 Correct 77 ms 488 KB Output is correct