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 <stdio.h>
#define N 8
#define N_ 40320 /* N_ = N! */
int ff[N + 1];
void init() {
int i;
ff[0] = 1;
for (i = 1; i <= N; i++)
ff[i] = ff[i - 1] * i;
}
int encode(int *pp, int n) {
int i, p, k, b;
b = 0, k = 0;
for (i = 0; i < n; i++) {
for (p = 0; p < pp[i]; p++)
if ((b & 1 << p) == 0)
k += ff[n - 1 - i];
b |= 1 << pp[i];
}
return k;
}
void decode(int *pp, int n, int k) {
int i, b;
b = 0;
for (i = 0; i < n; i++)
for (pp[i] = 0; pp[i] < n; pp[i]++)
if ((b & 1 << pp[i]) == 0) {
if (k < ff[n - 1 - i]) {
b |= 1 << pp[i];
break;
}
k -= ff[n - 1 - i];
}
}
int main() {
int t;
init();
scanf("%d", &t);
while (t--) {
static int aa[N], pp[N], qq[N], qu[N_], dd[N_];
int n, cnt, h, i, j, u, v, d, decr;
scanf("%d", &n);
for (i = 0; i < n; i++)
scanf("%d", &aa[i]);
for (u = 0; u < ff[n]; u++)
dd[u] = ff[n];
cnt = 0;
dd[0] = 0, qu[cnt++] = 0;
for (h = 0; h < cnt; h++) {
u = qu[h], d = dd[u] + 1;
decode(pp, n, u);
decr = 1;
for (i = 1; i < n; i++)
if (aa[pp[i - 1]] < aa[pp[i]]) {
decr = 0;
break;
}
if (decr) {
printf("%d\n", dd[u]);
break;
}
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++)
qq[j] = j < i ? pp[j] : pp[i + n - 1 - j];
v = encode(qq, n);
decode(qq, n, v);
if (dd[v] > d)
dd[v] = d, qu[cnt++] = v;
}
}
}
return 0;
}
Compilation message (stderr)
pancake.c: In function 'main':
pancake.c:48:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
48 | scanf("%d", &t);
| ^~~~~~~~~~~~~~~
pancake.c:53:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
53 | scanf("%d", &n);
| ^~~~~~~~~~~~~~~
pancake.c:55:4: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
55 | scanf("%d", &aa[i]);
| ^~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |