# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
630156 | nextgenxing | 드문 곤충 (IOI22_insects) | C++17 | 0 ms | 0 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#include "fish.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define F0R(i, n) for(int i = 0; i < (n); i++)
#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define F0Rd(i, n) for(int i = (n)-1; i >= 0; i--)
#define FORd(i, a, b) for(int i = (b)-1; i >= (a); i--)
#define ff first
#define ss second
const int MAX_N = 2e5+5;
const ll MOD = 1e9+7;
//#define LOCAL
#ifdef LOCAL
static inline 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);
}
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());
}
#endif
int n, colors;
bool check(int x){
vector<int> inserted;
F0R(i, n){
move_inside(i);
if(press_button() > x) move_outside(i);
else inserted.push_back(i);
}
int num1 = inserted.size();
for(auto x : inserted) move_outside(x);
if(num1 == x*colors) return true;
else return false;
}
int min_cardinality(int N){
n = N;
vector<int> inserted, non;
F0R(i, n){
move_inside(i);
if(press_button() > 1) move_outside(i), non.push_back(i);
else inserted.push_back(i);
}
colors = inserted.size();
inserted.clear();
bool f = 0;
int l = 0, r = n+1;
while(l+1 < r){
int m = (l+r)/2;
if(!f){
vector<int> b;
inserted.clear();
for(auto x : non){
move_inside(x);
if(press_button() > m) move_outside(x), b.push_back(x);
else inserted.push_back(x);
}
non = b;
} else{
non.clear();
for(auto x : inserted) move_outside(x);
vector<int> a;
for(auto x : inserted){
move_inside(x);
if(press_button() > m) move_outside(x), non.push_back(x);
else a.push_back(x);
}
inserted = a;
}
int num1 = inserted.size();
if(num1 == (m-1)*colors) l = m, f = 0;
else r = m, f = 1;
}
return l;
}
#ifdef LOCAL
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;
}
#endif