Submission #640846

#TimeUsernameProblemLanguageResultExecution timeMemory
640846blueRarest Insects (IOI22_insects)C++17
99.95 / 100
116 ms456 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; // cerr << "solve : "; // for(int u : lst) // cerr << u << ' '; // cerr << '\n'; int mx = sz(lst)/Z; // int I = sz(lst); int med = opt[sz(lst)]; // cerr << "med = " << med << '\n'; vi ins, notins; int to_exc = -1; bool flag = 0; 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++; } // cerr << "Z = " << Z << '\n'; 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])); } } // cerr << i << " : " << dp[i] << ' ' << opt[i] << "->" << opt[i]*Z << '\n'; } solve(lst, 1); if(ans != -1) return ans; return ct / Z; }

Compilation message (stderr)

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