Submission #137392

#TimeUsernameProblemLanguageResultExecution timeMemory
137392wilwxkAncient Books (IOI17_books)C++14
0 / 100
2078 ms376 KiB
#include "books.h" #include <bits/stdc++.h> using namespace std; typedef long long ll; const int MAXN=1e6+6; vector<int> v; int n, ori, atual, bons; ll respf, ciclos; bool melhora() { if(atual>=n) return 0; while(bons<n&&v[atual]==atual) atual++; // printf("atual %d\n", atual); int mao=v[atual]; v[atual]=-1; while(mao!=-1) { respf+=abs(mao-atual); atual=mao; swap(mao, v[atual]); if(v[atual]==atual) bons++; } // for(auto cur : v) printf("%d ", cur); // printf(" >> %lld // %d\n", respf, atual); return 1; } long long minimum_walk(std::vector<int> P, int S) { n=P.size(); v=P; ori=S; atual=ori; for(int i=0; i<n; i++) if(v[i]==i) bons++; while(v[atual]==atual) atual++, respf++; while(bons<n) melhora(), ciclos++; respf+=abs(atual-ori); // printf("%lld // %d\n", respf, ciclos); respf-=(ciclos-1); return respf; }
#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...