제출 #1293388

#제출 시각아이디문제언어결과실행 시간메모리
1293388lambd47버섯 세기 (IOI20_mushrooms)C++20
91.87 / 100
6 ms996 KiB
#include<bits/stdc++.h> #include "mushrooms.h" using namespace std; #define ll long long #define sz(v) ((int)(v).size()) #define all(v) (v).begin(), (v).end() #define L(i, j, k) for(int i = (j); i <= (k); ++i) #define R(i, j, k) for(int i = (j); i >= (k); --i) int count_mushrooms(int n) { vector<int> ids(n-1); iota(all(ids),1); reverse(all(ids)); vector<int> tipo[2]; vector<vector<int>> adj(n); tipo[0].clear();tipo[1].clear(); tipo[0].push_back(0); vector<int> aux; int resp=0; vector<int> fila; while(!ids.empty() && sz(tipo[0])<3 && sz(tipo[1])<3){ aux.clear(); aux.push_back(0); aux.push_back(ids.back()); int v=use_machine(aux); tipo[v].push_back(ids.back()); ids.pop_back(); } auto dfs=[&](auto&& self, int node,int pai,int at)->void{ tipo[at].push_back(node); for(auto a:adj[node]){ if(a==pai)continue; self(self,a,node,at^1); } }; auto op=[&](int a,int b, int c, int at)->void{ aux.clear(); aux={tipo[at][0],a,tipo[at][1],b,tipo[at][2],c}; int v=use_machine(aux); if(v&1){ dfs(dfs,c,c,at^1); v--; } else dfs(dfs,c,c,at); if(v==4){ dfs(dfs,a,a,at^1); dfs(dfs,b,b,at^1); } else if(v==2){ adj[a].push_back(b); fila.push_back(a); } else{ dfs(dfs,a,a,at); dfs(dfs,b,b,at); } }; int at=0; if(sz(tipo[1])>=3)at=1; int m=85; while(sz(ids)+sz(fila)>2 && sz(tipo[0])<m && sz(tipo[1])<m){ while(sz(fila)<3){ fila.push_back(ids.back()); ids.pop_back(); } int a=fila.back();fila.pop_back(); int b=fila.back();fila.pop_back(); int c=fila.back();fila.pop_back(); op(a,b,c,at); } if(sz(ids)<=2){ while(sz(ids)>0){ fila.push_back(ids.back()); ids.pop_back(); } } for(auto a:fila){ aux.clear();aux={0,a}; dfs(dfs,a,a,use_machine(aux)); } if(sz(ids)==0){ return resp+sz(tipo[0]); } at=0; if(sz(tipo[1])>=sz(tipo[0]))at=1; while(!ids.empty()){ int vida=sz(tipo[at])-1; aux.clear(); while(vida>=0 && !ids.empty()){ aux.push_back(tipo[at][vida]); aux.push_back(ids.back()); ids.pop_back(); vida--; } int v=use_machine(aux); int oposto=0; if(v&1){ v++; oposto=1; } if(at==1){ resp+=v/2; if(oposto==1){ resp--; tipo[0].push_back(aux.back()); } else{ tipo[1].push_back(aux.back()); } } else{ resp+=sz(aux)/2-v/2; if(oposto==1){ tipo[1].push_back(aux.back()); } else{ tipo[0].push_back(aux.back()); resp--; } } if(sz(tipo[at^1])>sz(tipo[at]))at^=1; } return resp+sz(tipo[0]); }
#Verdict Execution timeMemoryGrader output
Fetching results...