# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
641051 | TimDee | 드문 곤충 (IOI22_insects) | C++17 | 0 ms | 0 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <cassert>
#include <cstdio>
#include <algorithm>
#include <map>
#include <set>
#include <string>
#include <vector>
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 inline void protocol_violation(std::string message) {
printf("Protocol Violation: %s\n", message.c_str());
exit(0);
}
#include <bits/stdc++.h>
void move_inside(int i) {
//std::cout<<"inside"<<' '<<i<<'\n';
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) {
//std::cout<<"outside"<<' '<<i<<'\n';
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());
}
//#include "insects.h"
#include <bits/stdc++.h>
using namespace std;
int n;
int min_cardinality(int N) {
n=N;
int d=0;
vector<int> in(n,0);
for(int i=0; i<n; ++i) {
move_inside(i);
int x=press_button();
if (x>1) move_outside(i);
else { in[i]=1; ++d; }
}
if (d==1) return n;
if (d==n) return 1;
if (d<=0) {
for (int i=0; i<n; ++i) {
if (in[i]) {
in[i]=0;
move_outside(i);
}
}
int ans=n;
vector<int> vis(n,0);
for (int i=0; i<d; ++i) {
vector<int> bulaneala;
int cnt=0;
for (int i=0; i<n; ++i) {
if (vis[i]) continue;
move_inside(i);
int x=press_button();
if (x==cnt) move_outside(i);
else {
bulaneala.push_back(i);
++cnt;
vis[i]=1;
}
}
ans=min(ans,cnt);
for (auto x:bulaneala) move_outside(x);
}
return ans;
}
int l=1, r=n/d;
int now=d;
vector<int> laststate=in;
while (l<r) {
int mid=(l+r+1)>>1;
int extra=0;
int cnt=now;
for (int i=0; i<n; ++i) {
if (in[i]) continue;
in[i]=1;
move_inside(i);
int x=press_button();
if (x>mid) {
extra=1;
move_outside(i);
in[i]=0;
} else ++cnt;
}
if (cnt==mid*d) {
if (extra==0) {
return mid;
} else {
l=mid;
now=cnt;
}
laststate=in;
} else {
now=0;
r=mid-1;
for (int i=0; i<n; ++i) { if ((in[i])&&(laststate[i]==0)) move_outside(i); in[i]=0; }
}
}
return r;
}
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);
sort(color.begin(), color.end());
//for (auto x:color) cout<<x<<' '; cout<<'\n';
int Q = *std::max_element(op_counter.begin(), op_counter.end());
printf("%d\n", answer);
printf("%d\n", Q);
return 0;
}