# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
731746 | rainboy | Fire drill (LMIO18_sauga) | C++17 | 993 ms | 9200 KiB |
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 <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define N 1000
#define MD 0x7fffffff
int *ej[N], *ed[N], eo[N];
void append(int i, int j, int d) {
int o = eo[i]++;
if (o >= 2 && (o & o - 1) == 0) {
ej[i] = (int *) realloc(ej[i], o * 2 * sizeof *ej[i]);
ed[i] = (int *) realloc(ed[i], o * 2 * sizeof *ed[i]);
}
ej[i][o] = j, ed[i][o] = d;
}
int ii[N], pp[N], dd[N], c, c_;
void swap(int i, int j) {
int p = pp[i], q = pp[j], o;
for (o = eo[i]; o--; ) {
int k = ej[i][o], d = ed[i][o];
if (d == 0) {
if (pp[i] < pp[k] && --dd[i] == 0)
c--;
} else {
if (pp[k] < pp[i] && --dd[k] == 0)
c--;
}
}
for (o = eo[j]; o--; ) {
int k = ej[j][o], d = ed[j][o];
if (k == i)
continue;
if (d == 0) {
if (pp[j] < pp[k] && --dd[j] == 0)
c--;
} else {
if (pp[k] < pp[j] && --dd[k] == 0)
c--;
}
}
ii[pp[i] = q] = i, ii[pp[j] = p] = j;
for (o = eo[i]; o--; ) {
int k = ej[i][o], d = ed[i][o];
if (d == 0) {
if (pp[i] < pp[k] && dd[i]++ == 0)
c++;
} else {
if (pp[k] < pp[i] && dd[k]++ == 0)
c++;
}
}
for (o = eo[j]; o--; ) {
int k = ej[j][o], d = ed[j][o];
if (k == i)
continue;
if (d == 0) {
if (pp[j] < pp[k] && dd[j]++ == 0)
c++;
} else {
if (pp[k] < pp[j] && dd[k]++ == 0)
c++;
}
}
}
int main() {
int n, m, c1, i, j;
clock_t beg;
double temp;
srand(12345);
scanf("%*d%d%d", &n, &c1);
for (i = 0; i < n; i++) {
ej[i] = (int *) malloc(2 * sizeof *ej[i]);
ed[i] = (int *) malloc(2 * sizeof *ed[i]);
}
for (i = 0; i < n; i++) {
scanf("%d", &m);
while (m--) {
scanf("%d", &j), j--;
append(i, j, 0), append(j, i, 1);
if (i < j && dd[i]++ == 0)
c++;
}
}
for (i = 0; i < n; i++)
ii[pp[i] = i] = i;
beg = clock();
while (c > c1 && (temp = 0.9 - (double) (clock() - beg) / CLOCKS_PER_SEC) > 0) {
i = rand() % n;
while ((j = rand() % n) == i);
c_ = c;
swap(i, j);
if (c > c_ && (rand() * 76543LL + rand()) % MD >= exp((c_ - c) / temp * 20) * MD)
swap(i, j), c = c_;
}
for (i = 0; i < n; i++)
printf("%d\n", ii[i] + 1);
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |