Submission #129897

#TimeUsernameProblemLanguageResultExecution timeMemory
129897alexpetrescuUntitled (POI11_rot)C++14
45 / 100
1082 ms65540 KiB
#include <cstdio> #include <vector> #include <algorithm> //FILE *fin = fopen("a.in", "r"), *fout = fopen("a.out", "w"); #define fin stdin #define fout stdout #define LOGN 18 long long ans; struct myc { int sz; std::vector < int > v[LOGN + 1]; }; inline long long calc(myc &a, myc &b) { long long ans = 0; for (int i = 0; (1 << i) <= a.sz; i++) { for (auto &x : a.v[i]) { for (int j = 0; (1 << j) <= b.sz; j++) { if (!b.v[j].empty() && b.v[j][0] < x) { int rez = 0; for (int pas = (1 << j) / 2; pas; pas >>= 1) if (b.v[j][rez + pas] < x) rez += pas; ans += rez + 1; } } } } return ans; } inline void goMerge(std::vector < int > &a, std::vector < int > &b) { if (a.empty()) { a = b; return ; } if (b.empty()) return ; int i = 0, j = 0, k = 0; std::vector < int > c(a.size() + b.size()); while (i < (int)a.size() && j < (int)b.size()) { if (a[i] < b[j]) c[k++] = a[i++]; else c[k++] = b[j++]; } while (i < (int)a.size()) c[k++] = a[i++]; while (j < (int)b.size()) c[k++] = b[j++]; a = c; } myc dfs() { int x; fscanf(fin, "%d", &x); myc a; if (x) { a.sz = 1; a.v[0].push_back(x); return a; } myc b = dfs(); myc c = dfs(); long long r; if (b.sz < c.sz) r = calc(b, c); else r = calc(c, b); ans += std::min(r, 1LL * b.sz * c.sz - r); a.sz = b.sz + c.sz; std::vector < int > tr; for (int i = 0; (1 << i) <= a.sz; i++) { goMerge(tr, b.v[i]); goMerge(tr, c.v[i]); if (a.sz & (1 << i)) { for (int j = 0; j < (1 << i); j++) { a.v[i].push_back(tr.back()); tr.pop_back(); } std::reverse(a.v[i].begin(), a.v[i].end()); } } return a; } int main() { int degeaba; fscanf(fin, "%d", &degeaba); myc a = dfs(); fprintf(fout, "%lld\n", ans); return 0; }

Compilation message (stderr)

rot.cpp: In function 'myc dfs()':
rot.cpp:60:11: warning: ignoring return value of 'int fscanf(FILE*, const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     fscanf(fin, "%d", &x);
     ~~~~~~^~~~~~~~~~~~~~~
rot.cpp: In function 'int main()':
rot.cpp:98:11: warning: ignoring return value of 'int fscanf(FILE*, const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     fscanf(fin, "%d", &degeaba);
     ~~~~~~^~~~~~~~~~~~~~~~~~~~~
#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...
#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...
#Verdict Execution timeMemoryGrader output
Fetching results...