본문 바로가기

알고리즘

Codeforces Global Round 12

4솔브

 

어제 밤부터 진행된 코드포스 글로벌 라운드 12의 풀이이다.

 

 

A. Avoid Trygub

 

주어진 문자열을 정렬해 출력하기만 해도 trygub을 없앨 수 있다.

 

#include <bits/stdc++.h>
using namespace std;
int main() {
	ios::sync_with_stdio(0); cin.tie(0);
	int T; cin >> T;
	while (T--) {
		long long N; cin >> N;
		string str; cin >> str;
		map<char, int>m;
		for (char i : str)m[i]++;
		for (int i = 0; i < 26; i++) {
			while (m[i + 'a'] > 0) {
				m[i + 'a']--;
				cout << (char)(i + 'a');
			}
		}cout << '\n';
	}
}

 

 

 

 

B. Balls of Steel

 

관찰을 통해 답은 무조건 1 또는 -1임을 알 수 있다. 제한이 작으므로, \(O(N^2)\)을 짜도 통과한다.

 

#include <bits/stdc++.h>
using namespace std;
int main() {
	ios::sync_with_stdio(0); cin.tie(0);
	int T; cin >> T;
	while (T--) {
		long long N, k; cin >> N >> k;
		vector<pair<int, int>>v(N);
		for (int i = 0; i < N; i++) {
			cin >> v[i].first >> v[i].second;
		}
		bool possible = 0;
		for (int i = 0; i < N; i++) {
			int cnt = 0;
			for (int j = 0; j < N; j++) {
				if (llabs(v[i].first - v[j].first) + llabs(v[i].second - v[j].second) <= k) {
					cnt++;
				}
			}
			if (cnt == N) {
				possible = 1; break;
			}
		}
		cout << (possible ? 1 : -1) << '\n';
	}
}

 

 

 

 

C1. Errich-Tac-Toe (Easy Version)

 

아마도 이번 셋에서 가장 욕을 많이 먹은 문제가 아닐까 싶다... 나는 문제를 제대로 읽고 나서 오목의 날일진이 생각났다. 그것과 비슷하게, 이런 식으로 배치하면 흰 색 칸이 어떻게 해도 3칸 이상으로 연속되게 배치되지 못한다.

그런데, 단순히 저 방법으로만 하면 \( \lfloor \frac{k}{3} \rfloor \)조건을 만족시키지 못할 수 있다. 이 때문에, 저 빨간 칸을 놓는 경우의 수를 아래로 한 칸, 두 칸씩 옮긴 경우를 생각해볼 수 있다. 그러면 총 세 가지 경우의 수가 있는데, Pigeonhole Principle에 의해 저 중 하나는 반드시 \( \lfloor \frac{k}{3} \rfloor \)조건을 만족시킴을 증명할 수 있다. 실제 시험에서는 불안해서 저 경우의 수를 좌우대칭시켜 3가지 케이스를 더 검사했다.

 

#include <bits/stdc++.h>
using namespace std;
int main() {
	ios::sync_with_stdio(0); cin.tie(0);
	int T; cin >> T;
	while (T--) {

		int N; cin >> N;
		vector<string> v;
		for (int i = 0; i < N; i++) {
			string str; cin >> str;
			v.push_back(str);
		}
		int cnt[6] = { 0 };
		auto t1 = v, t2 = v, t3 = v, t4 = v, t5 = v, t6 = v;
		for (int i = 0; i < N; i++) {
			for (int j = 0; j < N; j++) {
				if ((i + j) % 3 == 0) {
					if (t1[i][j] == 'X') {
						t1[i][j] = 'O';
						cnt[0]++;
					}
				}
				if ((i + j) % 3 == 1) {
					if (t2[i][j] == 'X') {
						t2[i][j] = 'O';
						cnt[1]++;
					}
				}
				if ((i + j) % 3 == 2) {
					if (t3[i][j] == 'X') {
						t3[i][j] = 'O';
						cnt[2]++;
					}
				}
				if (i >= j) {
					if ((i - j) % 3 == 0) {
						if (t4[i][j] == 'X') {
							t4[i][j] = 'O';
							cnt[3]++;
						}
					}
					if ((i - j) % 3 == 1) {
						if (t5[i][j] == 'X') {
							t5[i][j] = 'O';
							cnt[4]++;
						}
					}
					if ((i - j) % 3 == 2) {
						if (t6[i][j] == 'X') {
							t6[i][j] = 'O';
							cnt[5]++;
						}
					}
				}
				else {
					if ((j - i) % 3 == 0) {
						if (t4[i][j] == 'X') {
							t4[i][j] = 'O';
							cnt[3]++;
						}
					}
					if ((j - i) % 3 == 2) {
						if (t5[i][j] == 'X') {
							t5[i][j] = 'O';
							cnt[4]++;
						}
					}
					if ((j - i) % 3 == 1) {
						if (t6[i][j] == 'X') {
							t6[i][j] = 'O';
							cnt[5]++;
						}
					}
				}
			}
		}

		int ind = min_element(cnt, cnt + 6) - cnt;

		if (ind == 0) {
			for (int i = 0; i < N; i++) {
				cout << t1[i] << '\n';
			}
		}
		if (ind == 1) {
			for (int i = 0; i < N; i++) {
				cout << t2[i] << '\n';
			}
		}
		if (ind == 2) {
			for (int i = 0; i < N; i++) {
				cout << t3[i] << '\n';
			}
		}
		if (ind == 3) {
			for (int i = 0; i < N; i++) {
				cout << t4[i] << '\n';
			}
		}
		if (ind == 4) {
			for (int i = 0; i < N; i++) {
				cout << t5[i] << '\n';
			}
		}
		if (ind == 5) {
			for (int i = 0; i < N; i++) {
				cout << t6[i] << '\n';
			}
		}
	}
}

 

 

 

 

C2. Errich-Tac-Toe (Hard Version)

 

C1을 O에 대해서, X에 대해서 총 2 번 돌리면 된다. 200줄 정도 되는 코드를 짰지만 틀려서 그냥 포기했다.

 

 

 

 

D. Rating Compression

 

이 문제의 정해는 \(O(N)\)이라고 한다. 하지만, 나는 그 방법을 몰라서 \(O(Nlog^2N)\)으로 풀었다. 일단, 관찰을 통해 답의 맨 앞 글자를 제외하면 답이 무조건 000...000111...111 형식으로 나옴을 알 수 있다. 그렇다면, 저 0과 1의 경계선만 이분 탐색으로 찾으면 모든 경우를 다 해보지 않아도 문제를 해결할 수 있다는 것을 알 수 있다.

 

먼저, 맨 앞 글자는 따로 답을 구한다. 그리고, \(2\)번째부터 \(N\)번째까지 답이 될 수 있는 수들을 이분 탐색한다. 탐색 과정은 세그먼트 트리를 사용할 수도 있고, 슬라이딩 윈도우를 사용할 수도 있다. 나는 슬라이딩 윈도우로 결과 벡터를 만들고, set을 이용해 Permutation인지 판별했다.

 

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> p;

int getnum() {
	int num = 0, ch;
	while ((ch = getchar()) != ' ') {
		if (ch == '\n')break;
		else num = num * 10 + ch - '0';
	}
	return num;
}

int main() {
	ios::sync_with_stdio(0); cin.tie(0);
	int T; T = getnum();
	while (T--) {

		int N; N = getnum();
		vector<int> v(N);
		for (auto &i : v)i = getnum();

		auto good = [&](vector<int> v) -> bool {
			bool gd = 1;
			int tot = 0;
			int sz = v.size();
			if (sz == 1 && v[0] == 1)return 1;
			set<int> cnt;
			for (int i = 0; i < sz; i++) {
				cnt.insert(v[i]);
			}
			return (cnt.size() == sz && *cnt.begin() == 1 && *--cnt.end() == sz);
		};
		auto bsearch = [&](int mid) -> vector<int> {
			deque<p>dq;
			vector<int> ans;
			for (int i = 0; i < N; i++) {
				if (!dq.empty() && dq.front().second <= i - mid)dq.pop_front();
				while (!dq.empty() && dq.back().first > v[i])dq.pop_back();
				dq.push_back({ v[i],i });
				if (i >= mid - 1) ans.push_back(dq.front().first);
			}
			return ans;
		};

		int ans = 300001;
		bool fs = good(bsearch(1));
		int st = 2, en = N;
		while (st <= en) {
			int mid = (st + en) / 2;
			bool gd = good(bsearch(mid));
			if (gd) {
				ans = mid;
				en = mid - 1;
			}
			else {
				st = mid + 1;
			}
		}
		if (ans == 300001) {
			if (fs) {
				cout << '1';
				for (int i = 1; i < N; i++) {
					cout << '0';
				}cout << '\n';
			}
			else {
				for (int i = 0; i < N; i++) {
					cout << '0';
				}cout << '\n';
			}
		}
		else {
			if (fs) {
				cout << '1';
				for (int i = 1; i < ans - 1; i++) {
					cout << '0';
				}
				for (int i = ans - 1; i < N; i++) {
					cout << '1';
				}cout << '\n';
			}
			else {
				cout << '0';
				for (int i = 1; i < ans - 1; i++) {
					cout << '0';
				}
				for (int i = ans - 1; i < N; i++) {
					cout << '1';
				}cout << '\n';
			}
		}
	}
}

'알고리즘' 카테고리의 다른 글

Codeforces Round #689 (Div.2)  (0) 2020.12.13
Codeforces Round #687 (Div.2)  (0) 2020.11.30
Codeforces Round #686 (Div.3)  (0) 2020.11.25
Codeforces Round #685 (Div.2)  (0) 2020.11.24
Educational Codeforces Round 98 (Div.2)  (0) 2020.11.21