Submission #731732

# Submission time Handle Problem Language Result Execution time Memory
731732 2023-04-27T21:39:40 Z rainboy Fire drill (LMIO18_sauga) C++17
0 / 100
1000 ms 11116 KB
#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 - (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) * MD)
			swap(i, j), c = c_;
	}
	for (i = 0; i < n; i++)
		printf("%d\n", ii[i] + 1);
	return 0;
}

Compilation message

sauga.cpp: In function 'void append(int, int, int)':
sauga.cpp:14:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
   14 |  if (o >= 2 && (o & o - 1) == 0) {
      |                     ~~^~~
sauga.cpp: In function 'int main()':
sauga.cpp:83:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   83 |  scanf("%*d%d%d", &n, &c1);
      |  ~~~~~^~~~~~~~~~~~~~~~~~~~
sauga.cpp:89:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   89 |   scanf("%d", &m);
      |   ~~~~~^~~~~~~~~~
sauga.cpp:91:9: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   91 |    scanf("%d", &j), j--;
      |    ~~~~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Execution timed out 1065 ms 10412 KB Time limit exceeded
2 Execution timed out 1000 ms 408 KB Time limit exceeded
3 Execution timed out 1002 ms 436 KB Time limit exceeded
4 Execution timed out 1000 ms 472 KB Time limit exceeded
5 Execution timed out 1000 ms 436 KB Time limit exceeded
6 Execution timed out 1002 ms 516 KB Time limit exceeded
7 Execution timed out 1010 ms 2320 KB Time limit exceeded
8 Execution timed out 1062 ms 11116 KB Time limit exceeded
9 Execution timed out 1008 ms 1972 KB Time limit exceeded
10 Execution timed out 1000 ms 360 KB Time limit exceeded