# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
834861 | esomer | 드문 곤충 (IOI22_insects) | C++17 | 0 ms | 0 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
#include "insects.h"
using namespace std;
mt19937 gen(time(0));
int groups, lft;
vector<bool> done;
int get_groups(int N){
int groups = 0;
for(int i = 0; i < N; i++){
move_inside(i);
int rep = press_button();
if(rep > 1) { move_outside(i); }
else { groups++; done[i] = 1; }
}
return groups;
}
int choose_best(int lo, int hi){
//If I choose mid. Then in the case that all the groups are of size at least mid,
//I discard (mid - lo + 1) * groups.
//In the other case, I discard left - chosen (and chosen can be at most (mid - lo + 1) * groups).
//So I want min((mid - lo + 1) * groups, left - (mid - lo + 1) * groups) to be the maximum.
int x = lft / 2;
int bst = x / groups;
int mn1 = min(bst * groups, lft - bst * groups + 1);
bst++;
int mn2 = min(bst * groups, lft - bst * groups + 1);
if(mn1 < mn2) bst--;
return max(lo, min(hi, lo + bst - 1));
}
int min_cardinality(int N) {
done.assign(N, 0);
int ans = 1;
groups = get_groups(N);
lft = N - groups;
int cnt = groups;
int lo = 2;
int hi = N / groups;
while(lo <= hi && lft > 0) {
int mid = choose_best(lo, hi);
vector<int> good, bad;
vector<int> possible;
for(int i = 0; i < N; i++) {
if(!done[i]) possible.push_back(i);
}
while((int)possible.size() > 0){
if((cnt + (int)good.size()) == mid * groups) {
bad.push_back(possible.back());
possible.pop_back();
continue;
}
int ind = gen() % ((int)possible.size());
int i = possible[ind];
move_inside(i);
int rep = press_button();
if(rep > mid) {
bad.push_back(i);
move_outside(i);
} else { good.push_back(i); }
if(ind != (int)possible.size() - 1) swap(possible[ind], possible[(int)possible.size() - 1]);
possible.pop_back();
}
if((int)good.size() + cnt == mid * groups) {
ans = mid;
lo = mid + 1;
for(int x : good) done[x] = 1;
cnt += (int)good.size();
lft -= (int)good.size();
} else {
hi = mid - 1;
for(int x : good) move_outside(x);
for(int x : bad) done[x] = 1;
lft -= (int)bad.size();
}
}
return ans;
}
static constexpr int kMaxQueries = 40000;
static int N;
// Insect types are compressed to colors in the range [0, N).
static std::vector<int> color;
static std::vector<bool> in_box;
static std::vector<int> color_occurrences;
static std::multiset<int> max_occurrences;
static std::vector<int> op_counter(3, 0);
static void protocol_violation(std::string message) {
printf("Protocol Violation: %s\n", message.c_str());
exit(0);
}
void move_inside(int i) {
if (i < 0 || i >= N) {
protocol_violation("invalid parameter");
}
++op_counter[0];
if (op_counter[0] > kMaxQueries) {
protocol_violation("too many calls");
}
if (!in_box[i]) {
in_box[i] = true;
max_occurrences.erase(max_occurrences.find(color_occurrences[color[i]]));
++color_occurrences[color[i]];
max_occurrences.insert(color_occurrences[color[i]]);
}
}
void move_outside(int i) {
if (i < 0 || i >= N) {
protocol_violation("invalid parameter");
}
++op_counter[1];
if (op_counter[1] > kMaxQueries) {
protocol_violation("too many calls");
}
if (in_box[i]) {
in_box[i] = false;
max_occurrences.erase(max_occurrences.find(color_occurrences[color[i]]));
--color_occurrences[color[i]];
max_occurrences.insert(color_occurrences[color[i]]);
}
}
int press_button() {
++op_counter[2];
if (op_counter[2] > kMaxQueries) {
protocol_violation("too many calls");
}
return *(max_occurrences.rbegin());
}
int main() {
assert(1 == scanf("%d", &N));
color.resize(N);
in_box.assign(N, false);
std::map<int, int> type_to_color;
for (int i = 0; i < N; ++i) {
int Ti;
assert(1 == scanf("%d", &Ti));
if (type_to_color.find(Ti) == type_to_color.end()) {
int new_color = type_to_color.size();
type_to_color[Ti] = new_color;
max_occurrences.insert(0);
}
color[i] = type_to_color[Ti];
}
color_occurrences.assign(type_to_color.size(), 0);
int answer = min_cardinality(N);
int Q = *std::max_element(op_counter.begin(), op_counter.end());
printf("%d\n", answer);
printf("%d\n", Q);
return 0;
}