제출 #1081779

#제출 시각아이디문제언어결과실행 시간메모리
1081779oscar1fJobs (BOI24_jobs)C++17
11 / 100
207 ms61632 KiB
#include<bits/stdc++.h> using namespace std; #define int long long const int MAX_SOM=300*1000+5; int nbSom,argDeb; int val[MAX_SOM],pere[MAX_SOM]; vector<int> listeFils[MAX_SOM]; int posEcri[MAX_SOM]; priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> memo[MAX_SOM]; void afficher(int pos) { auto temp=memo[pos]; cout<<memo[pos].size()<<endl; while (!temp.empty()) { cout<<temp.top().first<<" "<<temp.top().second<<endl; temp.pop(); } cout<<endl; } void insere(int pos,int petit) { while (!memo[petit].empty()) { memo[pos].push({memo[petit].top().first,memo[petit].top().second}); memo[petit].pop(); } } void ajout(int pos,int valAjout) { memo[pos].push({0,valAjout}); } void retrait(int pos,int valSuppr) { //cout<<"!!!!!"<<pos<<" "<<valSuppr<<" "<<gainSur[pos]<<endl; int gain=-valSuppr,dernDec=0; while (!memo[pos].empty() and (gain<=0 or memo[pos].top().first<valSuppr)) { dernDec=memo[pos].top().first; gain+=memo[pos].top().second; memo[pos].pop(); } if (gain>0) { memo[pos].push({max(dernDec,valSuppr),gain}); } } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin>>nbSom>>argDeb; for (int i=1;i<=nbSom;i++) { cin>>val[i]>>pere[i]; listeFils[pere[i]].push_back(i); } val[0]=argDeb; for (int i=nbSom;i>=0;i--) { int valMax=-1,maxFils=i; for (int j:listeFils[i]) { if (!memo[posEcri[j]].empty() and (int)memo[posEcri[j]].size()>valMax) { valMax=memo[posEcri[j]].size(); maxFils=j; } } if (valMax==-1) { posEcri[i]=i; } else { posEcri[i]=posEcri[maxFils]; } for (int j:listeFils[i]) { //cout<<"?"<<i<<" "<<j<<endl; if (j!=maxFils) { insere(posEcri[i],posEcri[j]); } } if (val[i]>0) { ajout(posEcri[i],val[i]); } else if (val[i]<0) { retrait(posEcri[i],-val[i]); } /*cout<<posEcri[i]<<endl; afficher(posEcri[i]);*/ } int argFin=0; while (!memo[posEcri[0]].empty() and argFin>=memo[posEcri[0]].top().first) { argFin+=memo[posEcri[0]].top().second; memo[posEcri[0]].pop(); } cout<<argFin-argDeb<<endl; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...