답안 #207889

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
207889 2020-03-09T11:32:47 Z E869120 Building Skyscrapers (CEOI19_skyscrapers) C++14
54 / 100
991 ms 91384 KB
#include <iostream>
#include <map>
#include <queue>
#include <vector>
#include <algorithm>
#include <functional>
using namespace std;
#pragma warning (disable: 4996)

int N, T, X[1 << 18], Y[1 << 18];
map<pair<int, int>, int> Map;
vector<int> G[1 << 18];
bool used[1 << 18];

void solve_subtask1() {
	for (int i = 1; i <= N; i++) Map[make_pair(X[i], Y[i])] = i;
	for (int i = 1; i <= N; i++) {
		int dx[8] = { 1, 1, 1, 0, -1, -1, -1, 0 };
		int dy[8] = { -1, 0, 1, 1, 1, 0, -1, -1 };
		for (int j = 0; j < 8; j++) {
			int ex = X[i] + dx[j], ey = Y[i] + dy[j];
			if (Map[make_pair(ex, ey)] >= 1) {
				G[i].push_back(Map[make_pair(ex, ey)]);
			}
		}
	}

	priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> Q;
	Q.push(make_pair(X[1], Y[1])); vector<int> I;
	while (!Q.empty()) {
		pair<int, int> pos = Q.top(); Q.pop();
		int idx = Map[pos]; if (used[idx] == true) continue;
		used[idx] = true; I.push_back(idx);
		for (int i : G[idx]) {
			if (used[i] == true) continue;
			Q.push(make_pair(X[i], Y[i]));
		}
	}

	if ((int)I.size() != N) {
		printf("NO\n");
	}
	else {
		printf("YES\n");
		for (int i = 0; i < I.size(); i++) printf("%d\n", I[i]);
	}
}

bool reached[2009][2009];
bool colored[2009][2009];
int nums[2009][2009];
queue<pair<int, int>> Que;

void calc() {
	while (!Que.empty()) {
		pair<int, int> pos = Que.front(); Que.pop();
		int dx[4] = { -1, 0, 1, 0 };
		int dy[4] = { 0, 1, 0, -1 };
		for (int i = 0; i < 4; i++) {
			int ex = pos.first + dx[i], ey = pos.second + dy[i];
			if (ex < 0 || ey < 0 || ex > N + 1 || ey > N + 1) continue;
			if (reached[ex][ey] == true || colored[ex][ey] == true) continue;
			reached[ex][ey] = true;
			Que.push(make_pair(ex, ey));
		}
	}
}

bool cond1[2009], cond2[2009];
int low[2009], ord[2009], cnts;

void dfs2(int pos, int par) {
	ord[pos] = cnts;
	low[pos] = cnts;

	int deg = 0;
	for (int i : G[pos]) {
		if (ord[i] == -1) {
			dfs2(i, pos); deg++;
			low[pos] = min(low[pos], low[i]);
			if (ord[pos] <= low[i]) cond2[pos] = false;
		}
		else {
			if (i != par) low[pos] = min(low[pos], ord[i]);
		}
	}
	if (pos == 1 && deg >= 2) cond2[pos] = false;
}

int solve() {
	for (int i = 1; i <= N; i++) { G[i].clear(); cond1[i] = false; cond2[i] = true; low[i] = -1; ord[i] = -1; }
	for (int i = 1; i <= N; i++) {
		int dx[4] = { -1, 0, 1, 0 };
		int dy[4] = { 0, 1, 0, -1 };
		for (int j = 0; j < 4; j++) {
			int ex = X[i] + dx[j], ey = Y[i] + dy[j];
			if (reached[ex][ey] == true) { cond1[i] = true; break; }
		}
	}
	for (int i = 1; i <= N; i++) {
		if (colored[X[i]][Y[i]] == false) continue;
		int dx[8] = { 1, 1, 1, 0, -1, -1, -1, 0 };
		int dy[8] = { -1, 0, 1, 1, 1, 0, -1, -1 };
		for (int j = 0; j < 8; j++) {
			int ex = X[i] + dx[j], ey = Y[i] + dy[j];
			if (ex < 0 || ey < 0 || ex > N + 1 || ey > N + 1) continue;
			if (nums[ex][ey] >= 1 && colored[ex][ey] == true) G[i].push_back(nums[ex][ey]);
		}
	}

	dfs2(1, -1);

	for (int i = 1; i <= N; i++) {
		if (colored[X[i]][Y[i]] == true && ord[i] == -1) return -1;
	}

	for (int i = N; i >= 1; i--) {
		if (colored[X[i]][Y[i]] == true && cond1[i] == true && cond2[i] == true) return i;
	}
	return -2;
}

void solve_subtask2() {
	int lx = (1 << 30), ly = (1 << 30);
	for (int i = 1; i <= N; i++) {
		lx = min(lx, X[i]);
		ly = min(ly, Y[i]);
	}
	for (int i = 1; i <= N; i++) {
		X[i] -= lx; X[i]++;
		Y[i] -= ly; Y[i]++;
		if (X[i] >= N + 1 || Y[i] >= N + 1) {
			printf("NO\n");
			return;
		}
		colored[X[i]][Y[i]] = true;
		nums[X[i]][Y[i]] = i;
	}

	for (int i = 1; i <= N; i++) { Que.push(make_pair(0, i)); reached[0][i] = true; }
	for (int i = 1; i <= N; i++) { Que.push(make_pair(N + 1, i)); reached[N + 1][i] = true; }
	for (int i = 1; i <= N; i++) { Que.push(make_pair(i, 0)); reached[i][0] = true; }
	for (int i = 1; i <= N; i++) { Que.push(make_pair(i, N + 1)); reached[i][N + 1] = true; }
	calc();

	vector<int> I;
	for (int i = 1; i <= N; i++) {
		int idx = solve(); if (idx == -1) break;
		I.push_back(idx);
		colored[X[idx]][Y[idx]] = false;
	}
	reverse(I.begin(), I.end());

	if ((int)I.size() != N) {
		printf("NO\n");
	}
	else {
		printf("YES\n");
		for (int i = 0; i < I.size(); i++) printf("%d\n", I[i]);
	}
}

int main() {
	scanf("%d%d", &N, &T);
	for (int i = 1; i <= N; i++) scanf("%d%d", &X[i], &Y[i]);
	
	if (T == 1) {
		solve_subtask1();
	}
	else {
		solve_subtask2();
	}
	return 0;
}

Compilation message

skyscrapers.cpp:8:0: warning: ignoring #pragma warning  [-Wunknown-pragmas]
 #pragma warning (disable: 4996)
 
skyscrapers.cpp: In function 'void solve_subtask1()':
skyscrapers.cpp:45:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for (int i = 0; i < I.size(); i++) printf("%d\n", I[i]);
                   ~~^~~~~~~~~~
skyscrapers.cpp: In function 'void solve_subtask2()':
skyscrapers.cpp:159:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for (int i = 0; i < I.size(); i++) printf("%d\n", I[i]);
                   ~~^~~~~~~~~~
skyscrapers.cpp: In function 'int main()':
skyscrapers.cpp:164:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d%d", &N, &T);
  ~~~~~^~~~~~~~~~~~~~~~
skyscrapers.cpp:165:36: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  for (int i = 1; i <= N; i++) scanf("%d%d", &X[i], &Y[i]);
                               ~~~~~^~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 6520 KB ans=YES N=1
2 Correct 8 ms 6520 KB ans=YES N=4
3 Correct 8 ms 6520 KB ans=NO N=4
4 Correct 9 ms 6520 KB ans=YES N=5
5 Correct 9 ms 6520 KB ans=YES N=9
6 Correct 9 ms 6520 KB ans=YES N=5
7 Correct 9 ms 6520 KB ans=NO N=9
8 Correct 9 ms 6520 KB ans=NO N=10
9 Correct 8 ms 6520 KB ans=YES N=10
10 Correct 9 ms 6520 KB ans=YES N=10
11 Correct 9 ms 6572 KB ans=YES N=10
12 Correct 9 ms 6572 KB ans=YES N=9
13 Correct 9 ms 6520 KB ans=YES N=9
14 Correct 9 ms 6520 KB ans=YES N=8
15 Correct 9 ms 6520 KB ans=YES N=8
16 Correct 10 ms 6520 KB ans=NO N=2
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 6520 KB ans=YES N=1
2 Correct 8 ms 6520 KB ans=YES N=4
3 Correct 8 ms 6520 KB ans=NO N=4
4 Correct 9 ms 6520 KB ans=YES N=5
5 Correct 9 ms 6520 KB ans=YES N=9
6 Correct 9 ms 6520 KB ans=YES N=5
7 Correct 9 ms 6520 KB ans=NO N=9
8 Correct 9 ms 6520 KB ans=NO N=10
9 Correct 8 ms 6520 KB ans=YES N=10
10 Correct 9 ms 6520 KB ans=YES N=10
11 Correct 9 ms 6572 KB ans=YES N=10
12 Correct 9 ms 6572 KB ans=YES N=9
13 Correct 9 ms 6520 KB ans=YES N=9
14 Correct 9 ms 6520 KB ans=YES N=8
15 Correct 9 ms 6520 KB ans=YES N=8
16 Correct 10 ms 6520 KB ans=NO N=2
17 Correct 9 ms 6520 KB ans=YES N=17
18 Correct 9 ms 6524 KB ans=YES N=25
19 Correct 9 ms 6520 KB ans=YES N=100
20 Correct 9 ms 6520 KB ans=YES N=185
21 Correct 9 ms 6520 KB ans=NO N=174
22 Correct 10 ms 6520 KB ans=YES N=90
23 Correct 10 ms 6520 KB ans=YES N=63
24 Correct 12 ms 6520 KB ans=YES N=87
25 Correct 9 ms 6524 KB ans=YES N=183
26 Correct 9 ms 6520 KB ans=YES N=188
27 Correct 10 ms 6520 KB ans=YES N=183
28 Correct 9 ms 6520 KB ans=YES N=189
29 Correct 10 ms 6520 KB ans=YES N=200
30 Correct 9 ms 6520 KB ans=YES N=190
31 Correct 11 ms 6520 KB ans=YES N=187
32 Correct 11 ms 6520 KB ans=YES N=187
33 Correct 9 ms 6520 KB ans=YES N=182
34 Correct 9 ms 6520 KB ans=YES N=184
35 Correct 9 ms 6520 KB ans=YES N=188
36 Correct 9 ms 6520 KB ans=YES N=181
37 Correct 9 ms 6520 KB ans=YES N=188
38 Correct 9 ms 6520 KB ans=YES N=191
39 Correct 9 ms 6520 KB ans=YES N=196
40 Correct 8 ms 6524 KB ans=YES N=196
41 Correct 9 ms 6520 KB ans=YES N=196
42 Correct 9 ms 6520 KB ans=YES N=196
43 Correct 11 ms 6520 KB ans=YES N=195
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 6520 KB ans=YES N=1
2 Correct 8 ms 6520 KB ans=YES N=4
3 Correct 8 ms 6520 KB ans=NO N=4
4 Correct 9 ms 6520 KB ans=YES N=5
5 Correct 9 ms 6520 KB ans=YES N=9
6 Correct 9 ms 6520 KB ans=YES N=5
7 Correct 9 ms 6520 KB ans=NO N=9
8 Correct 9 ms 6520 KB ans=NO N=10
9 Correct 8 ms 6520 KB ans=YES N=10
10 Correct 9 ms 6520 KB ans=YES N=10
11 Correct 9 ms 6572 KB ans=YES N=10
12 Correct 9 ms 6572 KB ans=YES N=9
13 Correct 9 ms 6520 KB ans=YES N=9
14 Correct 9 ms 6520 KB ans=YES N=8
15 Correct 9 ms 6520 KB ans=YES N=8
16 Correct 10 ms 6520 KB ans=NO N=2
17 Correct 9 ms 6520 KB ans=YES N=17
18 Correct 9 ms 6524 KB ans=YES N=25
19 Correct 9 ms 6520 KB ans=YES N=100
20 Correct 9 ms 6520 KB ans=YES N=185
21 Correct 9 ms 6520 KB ans=NO N=174
22 Correct 10 ms 6520 KB ans=YES N=90
23 Correct 10 ms 6520 KB ans=YES N=63
24 Correct 12 ms 6520 KB ans=YES N=87
25 Correct 9 ms 6524 KB ans=YES N=183
26 Correct 9 ms 6520 KB ans=YES N=188
27 Correct 10 ms 6520 KB ans=YES N=183
28 Correct 9 ms 6520 KB ans=YES N=189
29 Correct 10 ms 6520 KB ans=YES N=200
30 Correct 9 ms 6520 KB ans=YES N=190
31 Correct 11 ms 6520 KB ans=YES N=187
32 Correct 11 ms 6520 KB ans=YES N=187
33 Correct 9 ms 6520 KB ans=YES N=182
34 Correct 9 ms 6520 KB ans=YES N=184
35 Correct 9 ms 6520 KB ans=YES N=188
36 Correct 9 ms 6520 KB ans=YES N=181
37 Correct 9 ms 6520 KB ans=YES N=188
38 Correct 9 ms 6520 KB ans=YES N=191
39 Correct 9 ms 6520 KB ans=YES N=196
40 Correct 8 ms 6524 KB ans=YES N=196
41 Correct 9 ms 6520 KB ans=YES N=196
42 Correct 9 ms 6520 KB ans=YES N=196
43 Correct 11 ms 6520 KB ans=YES N=195
44 Correct 13 ms 7672 KB ans=NO N=1934
45 Correct 13 ms 6904 KB ans=NO N=1965
46 Correct 14 ms 6780 KB ans=YES N=1824
47 Correct 16 ms 6776 KB ans=YES N=1981
48 Correct 16 ms 6776 KB ans=YES N=1814
49 Correct 14 ms 6776 KB ans=YES N=1854
50 Correct 17 ms 6776 KB ans=YES N=1831
51 Correct 15 ms 6776 KB ans=YES N=2000
52 Correct 14 ms 6776 KB ans=YES N=1847
53 Correct 13 ms 6776 KB ans=YES N=1819
54 Correct 15 ms 6776 KB ans=YES N=1986
55 Correct 14 ms 6904 KB ans=YES N=2000
56 Correct 16 ms 6904 KB ans=YES N=1834
57 Correct 14 ms 6904 KB ans=YES N=1860
58 Correct 14 ms 6904 KB ans=YES N=1898
59 Correct 14 ms 6904 KB ans=YES N=1832
60 Correct 15 ms 7036 KB ans=YES N=1929
61 Correct 15 ms 6776 KB ans=YES N=1919
62 Correct 16 ms 6904 KB ans=YES N=1882
63 Correct 14 ms 7032 KB ans=YES N=1922
64 Correct 16 ms 6776 KB ans=YES N=1989
65 Correct 13 ms 6904 KB ans=YES N=1978
66 Correct 13 ms 6904 KB ans=YES N=1867
67 Correct 14 ms 6904 KB ans=YES N=1942
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 6520 KB ans=NO N=1934
2 Correct 82 ms 11000 KB ans=NO N=1965
3 Incorrect 374 ms 10744 KB Integer -2 violates the range [1, 1824]
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 6520 KB ans=YES N=1
2 Correct 8 ms 6520 KB ans=YES N=4
3 Correct 8 ms 6520 KB ans=NO N=4
4 Correct 9 ms 6520 KB ans=YES N=5
5 Correct 9 ms 6520 KB ans=YES N=9
6 Correct 9 ms 6520 KB ans=YES N=5
7 Correct 9 ms 6520 KB ans=NO N=9
8 Correct 9 ms 6520 KB ans=NO N=10
9 Correct 8 ms 6520 KB ans=YES N=10
10 Correct 9 ms 6520 KB ans=YES N=10
11 Correct 9 ms 6572 KB ans=YES N=10
12 Correct 9 ms 6572 KB ans=YES N=9
13 Correct 9 ms 6520 KB ans=YES N=9
14 Correct 9 ms 6520 KB ans=YES N=8
15 Correct 9 ms 6520 KB ans=YES N=8
16 Correct 10 ms 6520 KB ans=NO N=2
17 Correct 9 ms 6520 KB ans=YES N=17
18 Correct 9 ms 6524 KB ans=YES N=25
19 Correct 9 ms 6520 KB ans=YES N=100
20 Correct 9 ms 6520 KB ans=YES N=185
21 Correct 9 ms 6520 KB ans=NO N=174
22 Correct 10 ms 6520 KB ans=YES N=90
23 Correct 10 ms 6520 KB ans=YES N=63
24 Correct 12 ms 6520 KB ans=YES N=87
25 Correct 9 ms 6524 KB ans=YES N=183
26 Correct 9 ms 6520 KB ans=YES N=188
27 Correct 10 ms 6520 KB ans=YES N=183
28 Correct 9 ms 6520 KB ans=YES N=189
29 Correct 10 ms 6520 KB ans=YES N=200
30 Correct 9 ms 6520 KB ans=YES N=190
31 Correct 11 ms 6520 KB ans=YES N=187
32 Correct 11 ms 6520 KB ans=YES N=187
33 Correct 9 ms 6520 KB ans=YES N=182
34 Correct 9 ms 6520 KB ans=YES N=184
35 Correct 9 ms 6520 KB ans=YES N=188
36 Correct 9 ms 6520 KB ans=YES N=181
37 Correct 9 ms 6520 KB ans=YES N=188
38 Correct 9 ms 6520 KB ans=YES N=191
39 Correct 9 ms 6520 KB ans=YES N=196
40 Correct 8 ms 6524 KB ans=YES N=196
41 Correct 9 ms 6520 KB ans=YES N=196
42 Correct 9 ms 6520 KB ans=YES N=196
43 Correct 11 ms 6520 KB ans=YES N=195
44 Correct 13 ms 7672 KB ans=NO N=1934
45 Correct 13 ms 6904 KB ans=NO N=1965
46 Correct 14 ms 6780 KB ans=YES N=1824
47 Correct 16 ms 6776 KB ans=YES N=1981
48 Correct 16 ms 6776 KB ans=YES N=1814
49 Correct 14 ms 6776 KB ans=YES N=1854
50 Correct 17 ms 6776 KB ans=YES N=1831
51 Correct 15 ms 6776 KB ans=YES N=2000
52 Correct 14 ms 6776 KB ans=YES N=1847
53 Correct 13 ms 6776 KB ans=YES N=1819
54 Correct 15 ms 6776 KB ans=YES N=1986
55 Correct 14 ms 6904 KB ans=YES N=2000
56 Correct 16 ms 6904 KB ans=YES N=1834
57 Correct 14 ms 6904 KB ans=YES N=1860
58 Correct 14 ms 6904 KB ans=YES N=1898
59 Correct 14 ms 6904 KB ans=YES N=1832
60 Correct 15 ms 7036 KB ans=YES N=1929
61 Correct 15 ms 6776 KB ans=YES N=1919
62 Correct 16 ms 6904 KB ans=YES N=1882
63 Correct 14 ms 7032 KB ans=YES N=1922
64 Correct 16 ms 6776 KB ans=YES N=1989
65 Correct 13 ms 6904 KB ans=YES N=1978
66 Correct 13 ms 6904 KB ans=YES N=1867
67 Correct 14 ms 6904 KB ans=YES N=1942
68 Correct 317 ms 17012 KB ans=NO N=66151
69 Correct 337 ms 36088 KB ans=NO N=64333
70 Correct 340 ms 15744 KB ans=YES N=69316
71 Correct 311 ms 15344 KB ans=YES N=66695
72 Correct 338 ms 15600 KB ans=YES N=68436
73 Correct 349 ms 15876 KB ans=YES N=70000
74 Correct 344 ms 15724 KB ans=YES N=68501
75 Correct 366 ms 16100 KB ans=YES N=70000
76 Correct 304 ms 16252 KB ans=YES N=65009
77 Correct 343 ms 19952 KB ans=YES N=67007
78 Correct 324 ms 21812 KB ans=YES N=66357
79 Correct 321 ms 23156 KB ans=YES N=65430
80 Correct 339 ms 22660 KB ans=YES N=65790
81 Correct 342 ms 21628 KB ans=YES N=66020
82 Correct 330 ms 20732 KB ans=YES N=65809
83 Correct 315 ms 17260 KB ans=YES N=65651
84 Correct 352 ms 27004 KB ans=YES N=68040
85 Correct 377 ms 24844 KB ans=YES N=66570
86 Correct 335 ms 16148 KB ans=YES N=65421
87 Correct 345 ms 16784 KB ans=YES N=68351
88 Correct 349 ms 15344 KB ans=YES N=67027
89 Correct 308 ms 18804 KB ans=YES N=68879
90 Correct 309 ms 16496 KB ans=YES N=67256
91 Correct 897 ms 26632 KB ans=YES N=148315
92 Correct 838 ms 78840 KB ans=NO N=142745
93 Correct 619 ms 91384 KB ans=NO N=148443
94 Correct 900 ms 26060 KB ans=YES N=148328
95 Correct 946 ms 25944 KB ans=YES N=147855
96 Correct 942 ms 26176 KB ans=YES N=150000
97 Correct 892 ms 25488 KB ans=YES N=144725
98 Correct 913 ms 26080 KB ans=YES N=149445
99 Correct 886 ms 25836 KB ans=YES N=144455
100 Correct 849 ms 25448 KB ans=YES N=143487
101 Correct 908 ms 26600 KB ans=YES N=149688
102 Correct 881 ms 35708 KB ans=YES N=141481
103 Correct 870 ms 46832 KB ans=YES N=147430
104 Correct 820 ms 30456 KB ans=YES N=142247
105 Correct 912 ms 35052 KB ans=YES N=149941
106 Correct 870 ms 44540 KB ans=YES N=141635
107 Correct 931 ms 41320 KB ans=YES N=142896
108 Correct 876 ms 45168 KB ans=YES N=142069
109 Correct 851 ms 27904 KB ans=YES N=142378
110 Correct 991 ms 39276 KB ans=YES N=150000
111 Correct 879 ms 50816 KB ans=YES N=141452
112 Correct 773 ms 48620 KB ans=YES N=134453
113 Correct 699 ms 50796 KB ans=YES N=144172
# 결과 실행 시간 메모리 Grader output
1 Runtime error 42 ms 19960 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 6520 KB ans=NO N=1934
2 Correct 82 ms 11000 KB ans=NO N=1965
3 Incorrect 374 ms 10744 KB Integer -2 violates the range [1, 1824]
4 Halted 0 ms 0 KB -