This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "permutation.h"
#include <cstring>
#include <vector>
using namespace std;
typedef vector<int> vi;
const int N = 256;
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; }
unsigned int X = 12345;
int rand_() {
return (X *= 3) >> 1;
}
char used[N];
int check(int *pp, int n, int m) {
int p = 0;
for (int i = 0; i < m; i++)
p = min(p, pp[i]);
memset(used, 0, n * sizeof *used);
for (int i = 0; i < m; i++) {
if (pp[i] - p >= n || used[pp[i] - p])
return 0;
used[pp[i] - p] = 1;
}
return 1;
}
int rr[N], dd[N], ppp[N + 1][N], ppp_[N + 1][N], m, m_;
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];
vi pp(n), qq(n);
pp[0] = 0, pp[1] = dd[1];
for (int i = 2; i < n; i++) {
int m = 0;
for (int j = 0; j < i; j++)
ppp[m][j] = pp[j];
m++;
while (i < n) {
m_ = 0;
for (int h = 0; h < m; h++) {
memcpy(ppp_[m_], ppp[h], i * sizeof *ppp[h]), ppp_[m_][i] = ppp[h][i - 1] + dd[i];
if (check(ppp_[m_], n, i + 1))
m_++;
if (m_ > n)
break;
memcpy(ppp_[m_], ppp[h], i * sizeof *ppp[h]), ppp_[m_][i] = ppp[h][i - 1] - dd[i];
if (check(ppp_[m_], n, i + 1))
m_++;
if (m_ > n)
break;
}
memset(used, 0, n * sizeof *used);
for (int h = 0; h < m_; h++) {
int p = abs_(ppp_[h][i]);
if (used[p])
goto out;
used[p] = 1;
}
m = m_;
for (int h = 0; h < m_; h++)
memcpy(ppp[h], ppp_[h], (i + 1) * sizeof *ppp_[h]);
i++;
}
out:
if (i == n) {
for (int h = 0; h < m; h++)
if (abs_(ppp[h][0] - ppp[h][n - 1]) == dd[0]) {
for (int j = 0; j < n; j++)
pp[j] = ppp[h][j];
break;
}
} else {
i--;
for (int h = 0; h < n; h++)
ii[h] = rr[h < i ? i - 1 - h : h] + 1;
int p = query(ii) - (sum - dd[0]) + dd[i];
for (int h = 0; h < m; h++)
if (abs_(ppp[h][i]) == p) {
for (int j = 0; j <= i; j++)
pp[j] = ppp[h][j];
break;
}
}
}
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 time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |