This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#include "insects.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
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |