Submission #871382

#TimeUsernameProblemLanguageResultExecution timeMemory
871382rainboySecret Permutation (RMI19_permutation)C++17
66.04 / 100
24 ms592 KiB
#include "permutation.h" #include <cstring> #include <vector> using namespace std; typedef vector<int> vi; const int N = 256; unsigned int X = 12345; int rand_() { return (X *= 3) >> 1; } int abs_(int a) { return a > 0 ? a : -a; } int min(int a, int b) { return a < b ? a : b; } int max(int a, int b) { return a > b ? a : b; } int rr[N], dd[N], ss[N]; char usedl[N * 2 + 1], usedr[N * 2 + 1]; void solve(int n) { for (int i = 0; i < n; i++) rr[i] = i; for (int i = 0; i < n; i++) { int j = rand_() % (i + 1), tmp; tmp = rr[i], rr[i] = rr[j], rr[j] = tmp; } vi ii(n); for (int i = 0; i < n; i++) { for (int h = 0; h < n; h++) ii[h] = rr[(i + h) % n] + 1; dd[i] = query(ii); } int sum = 0; for (int i = 0; i < n; i++) sum += dd[i]; sum /= (n - 1); for (int i = 0; i < n; i++) dd[i] = sum - dd[i]; memset(ss, 0, n * sizeof *ss); for (int t = 0; t < n; t++) for (int i = 1; i + 1 < n; i++) if (ss[i + 1] == 0) { memset(usedl, 0, (n * 2 + 1) * sizeof *usedl); ss[i + 1] = 1; int lmn = 0, lmx = 0; for (int h = i, s = -1, l = 0; h > 0 && ss[h + 1]; h--) { s *= ss[h + 1], l += s == 1 ? dd[h] : -dd[h], lmn = min(lmn, l), lmx = max(lmx, l); usedl[n + l] = 1; } memset(usedr, 0, (n * 2 + 1) * sizeof *usedr); int rmn = 0, rmx = 0; for (int j = i + 1, s = 1, r = 0; j < n && ss[j]; j++) { s *= ss[j], r += s == 1 ? dd[j] : -dd[j], rmn = min(rmn, r), rmx = max(rmx, r); usedr[n + r] = 1; } ss[i + 1] = 0; if (lmx - rmn >= n || rmx - lmn >= n) ss[i + 1] = -1; else if (lmx - -rmx >= n || -rmn - lmn >= n) ss[i + 1] = 1; else for (int x = -n; x <= n; x++) { if (usedl[n + x] && usedr[n + x]) { ss[i + 1] = -1; break; } if (usedl[n + x] && usedr[n - x]) { ss[i + 1] = 1; break; } } } vi pp(n), qq(n); pp[0] = 0, pp[1] = dd[1]; for (int i = 2; i < n; i++) if (ss[i]) pp[i] = pp[i - 1] + ((pp[i - 2] < pp[i - 1]) == (ss[i] == 1) ? dd[i] : -dd[i]); else { for (int h = 0; h < n; h++) ii[h] = rr[h < i ? i - 1 - h : h] + 1; pp[i] = query(ii) - (sum - dd[0]) + dd[i]; if (abs_(pp[i] - pp[i - 1]) != dd[i]) pp[i] = -pp[i]; } int p = 0; for (int i = 0; i < n; i++) p = min(p, pp[i]); for (int i = 0; i < n; i++) pp[i] -= p - 1; for (int i = 0; i < n; i++) qq[rr[i]] = pp[i]; for (int i = 0; i < n; i++) pp[i] = qq[i]; answer(pp); }

Compilation message (stderr)

stub.cpp: In function 'int query(int*)':
stub.cpp:15:9: warning: ignoring return value of 'int fscanf(FILE*, const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   15 |   fscanf(stdin, "%d", &x);
      |   ~~~~~~^~~~~~~~~~~~~~~~~
stub.cpp: In function 'int main(int, char**)':
stub.cpp:48:9: warning: ignoring return value of 'int fscanf(FILE*, const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   48 |   fscanf(stdin, "%d", &N);
      |   ~~~~~~^~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...