답안 #731753

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
731753 2023-04-27T21:51:03 Z rainboy Fire drill (LMIO18_sauga) C++17
63.6699 / 100
969 ms 9208 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 - (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 * 8) * 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--;
      |    ~~~~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Partially correct 958 ms 8460 KB Output is partially correct
2 Partially correct 901 ms 396 KB Output is partially correct
3 Partially correct 901 ms 420 KB Output is partially correct
4 Partially correct 902 ms 468 KB Output is partially correct
5 Partially correct 901 ms 428 KB Output is partially correct
6 Partially correct 902 ms 500 KB Output is partially correct
7 Partially correct 913 ms 2012 KB Output is partially correct
8 Partially correct 969 ms 9208 KB Output is partially correct
9 Partially correct 908 ms 1868 KB Output is partially correct
10 Correct 19 ms 340 KB Output is correct