Submission #649234

#TimeUsernameProblemLanguageResultExecution timeMemory
649234blueRarest Insects (IOI22_insects)C++17
100 / 100
63 ms452 KiB
#include "insects.h" #include <bits/stdc++.h> using namespace std; using vi = vector<int>; using vvi = vector<vi>; #define sz(x) int(x.size()) const int mx = 2'000; vi ins(mx, 0); int ct = 0; vi dp(mx + 5, 0); vi opt(mx + 5, 0); int getRandom(int l, int r) { return rand() % (r-l+1); } void move_in(int i) { // cerr << "insert " << i << '\n'; if(ins[i]) return; ins[i] = 1; move_inside(i); ct++; } void move_out(int i) { // cerr << "erase " << i << '\n'; if(!ins[i]) return; ins[i] = 0; move_outside(i); ct--; } int Z = 0; //number of species int ans = -1; void solve(vi lst, int b) //b = basic value { if(sz(lst) < Z) return; int mx = sz(lst)/Z; int med = (sz(lst)/Z)/2; if(med == 0) med++; vi ins, notins; int to_exc = -1; bool flag = 0; for(int t = 0; t < 4*sz(lst); t++) { int u = getRandom(0, sz(lst)-1); int v = getRandom(0, sz(lst)-1); swap(lst[u], lst[v]); } for(int i : lst) { if(ct == (med + b) * Z) { notins.push_back(i); continue; } move_in(i); int pb = press_button(); if(pb - b > med) { move_out(i); notins.push_back(i); } else { if(pb - b == med && flag == 0) { to_exc = i; flag = 1; } ins.push_back(i); } } if(ct == (med + b) * Z) { solve(notins, b + med); } else { if(sz(lst) >= 2*Z) { for(int i : ins) move_out(i); vi new_ins; for(int i : ins) if(i != to_exc) new_ins.push_back(i); solve(new_ins, b); } else ans = (ct - sz(ins))/Z; } } int min_cardinality(int N) { vi lst; for(int i = 0; i < N; i++) { move_in(i); if(press_button() >= 2) { move_out(i); lst.push_back(i); } else Z++; } for(int i = 0; i <= N; i++) { dp[i] = 5'000'000; opt[i] = -1; if(i < Z) { dp[i] = 0; opt[i] = 0; } else if(i < 2*Z) { dp[i] = i; opt[i] = 1; } else { for(int j = 1; j <= i/Z; j++) { int cand = i + max(dp[i - j*Z], dp[j*Z]); if(cand < dp[i]) { dp[i] = cand; opt[i] = j; } dp[i] = min(dp[i], i + max(dp[i - j*Z], dp[j*Z])); } } } solve(lst, 1); if(ans != -1) return ans; return ct / Z; }

Compilation message (stderr)

insects.cpp: In function 'void solve(vi, int)':
insects.cpp:54:6: warning: unused variable 'mx' [-Wunused-variable]
   54 |  int mx = sz(lst)/Z;
      |      ^~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...