답안 #787786

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
787786 2023-07-19T12:41:13 Z onjo0127 함박 스테이크 (JOI20_hamburg) C++17
100 / 100
1104 ms 157364 KB
#include <bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;
using vi = vector<int>;
using vpi = vector<pii>;
using vvi = vector<vi>;
const vector<pii> EMP = {};
 
int f(vi &X, int x) { return lower_bound(X.begin(), X.end(), x) - X.begin() + 1; }
pii its(pii l, pii r) { return {max(l.first, r.first), min(l.second, r.second)}; }

vi X, Y; int XS, YS;
 
vvi rem(vvi &A, int x, int y) {
	vvi ret;
	for(auto& it: A) if(it[2] < x || x < it[0] || it[3] < y || y < it[1]) ret.push_back(it);
	return ret;
}
 
vvi intersect_line(vvi &A, int x, int y) {
	vvi ret;
	if(x) for(auto& it: A) if(it[0] <= x || x <= it[2]) ret.push_back(it);
	if(y) for(auto& it: A) if(it[1] <= y || y <= it[3]) ret.push_back(it);
	return ret;
}
 
vpi sol4(vvi A) {
	int mnr = XS, mxl = 1, mnu = YS, mxd = 1;
	for(auto& it: A) {
		mnr = min(mnr, it[2]);
		mxl = max(mxl, it[0]);
		mnu = min(mnu, it[3]);
		mxd = max(mxd, it[1]);
	}
	vvi B;
	for(auto& it: A) {
		int cnt = 0;
		for(auto& x: {mnr, mxl}) for(auto& y: {mnu, mxd}) if(it[0] <= x && x <= it[2] && it[1] <= y && y <= it[3]) ++cnt;
		if(cnt <= 1) B.push_back(it);
	}
	A = B;
	for(auto& it: A) if(mnr < it[0] && it[2] < mxl && mnu < it[1] && it[3] < mxd) return EMP;
 
	vvi L = intersect_line(A, mnr, 0);
	int only_L_mxd = mnu + 1, only_L_mnu = mxd - 1;
	for(auto& it: L) if(it[2] < mxl && mnu < it[1] && it[3] < mxd) {
		only_L_mxd = max(only_L_mxd, it[1]);
		only_L_mnu = min(only_L_mnu, it[3]);
	}
 
	vi D_mnr(YS + 1, mxl - 1);
	for(auto& it: L) D_mnr[it[3]] = min(D_mnr[it[3]], it[2]);
	for(int i=1; i<=YS; i++) D_mnr[i] = min(D_mnr[i-1], D_mnr[i]);
 
	vi U_mnr(YS + 2, mxl - 1);
	for(auto& it: L) U_mnr[it[1]] = min(U_mnr[it[1]], it[2]);
	for(int i=YS; i>=1; i--) U_mnr[i] = min(U_mnr[i+1], U_mnr[i]);
 
 	vvi U = intersect_line(A, 0, mxd);
	int new_mnr = mxl - 1;
	for(auto& it: U) if(mnr < it[0] && it[2] < mxl) new_mnr = min(new_mnr, it[2]);
 
	int only_D_mnr = mxl - 1;
	vvi D = intersect_line(A, 0, mnu);
	for(auto& it: D) if(mnr < it[0] && it[2] < mxl && it[3] < mxd) only_D_mnr = min(only_D_mnr, it[2]);

	vvi UD = intersect_line(D, 0, mxd);
	vi UD_mnr(XS + 2, mxl - 1);
	for(auto& it: UD) UD_mnr[it[0]] = min(UD_mnr[it[0]], it[2]);
	for(int i=XS; i>=1; i--) UD_mnr[i] = min(UD_mnr[i+1], UD_mnr[i]);
 
	vvi R = intersect_line(A, mxl, 0);
	vvi RU = intersect_line(R, 0, mxd);
	vvi RD = intersect_line(R, 0, mnu);
 
	vi RU_mxd(XS + 2, mnu + 1);
	for(auto& it: RU) if(it[1] > mnu) RU_mxd[it[0]] = max(RU_mxd[it[0]], it[1]);
	for(int i=XS; i>=1; i--) RU_mxd[i] = max(RU_mxd[i+1], RU_mxd[i]);
 
	vi RD_mnu(XS + 2, mxd - 1);
	for(auto& it: RD) if(it[3] < mxd) RD_mnu[it[0]] = min(RD_mnu[it[0]], it[3]);
	for(int i=XS; i>=1; i--) RD_mnu[i] = min(RD_mnu[i+1], RD_mnu[i]);
 
	int only_R_mxd = mnu + 1, only_R_mnu = mxd - 1;
	for(auto& it: R) if(it[1] > mnu && it[3] < mxd && mnr < it[0]) {
		only_R_mxd = max(only_R_mxd, it[1]);
		only_R_mnu = min(only_R_mnu, it[3]);
	}

	vvi LR = intersect_line(R, mnr, 0);
	vpi LRU(YS + 1, {mnu + 1, mxd - 1}), LRD(YS + 2, {mnu + 1, mxd - 1});
	for(auto& it: LR) if(it[1] > mnu && it[3] < mxd) {
		LRU[it[3]] = its(LRU[it[3]], {it[1], it[3]});
		LRD[it[1]] = its(LRD[it[1]], {it[1], it[3]});
	}
	for(int i=1; i<=YS; i++) LRU[i] = its(LRU[i-1], LRU[i]);
	for(int i=YS; i>=1; i--) LRD[i] = its(LRD[i+1], LRD[i]);
 
	for(int ly=only_L_mxd; ly<=only_L_mnu; ly++) {
		int ux = min(U_mnr[ly + 1], new_mnr);
		int dx = min({D_mnr[ly - 1], only_D_mnr, UD_mnr[ux + 1]});
		int r_mxd = max({RU_mxd[ux + 1], only_R_mxd, LRU[ly - 1].first, LRD[ly + 1].first});
		int r_mnu = min({RD_mnu[dx + 1], only_R_mnu, LRU[ly - 1].second, LRD[ly + 1].second});
		//printf("ly: %d, ux: %d, dx: %d, r_mxd: %d, r_mnu: %d\n", ly, ux, dx, r_mxd, r_mnu);
		if(ux <= dx && r_mxd <= r_mnu) return {{mnr, ly}, {ux, mxd}, {dx, mnu}, {mxl, r_mxd}};
	}
 
	return EMP;
}
 
vpi sol(vvi &A, int K) {
	int mnr = XS, mxl = 1, mnu = YS, mxd = 1;
	for(auto& it: A) {
		mnr = min(mnr, it[2]);
		mxl = max(mxl, it[0]);
		mnu = min(mnu, it[3]);
		mxd = max(mxd, it[1]);
	}
	if(K == 1) {	
		if(mxl <= mnr && mxd <= mnu) return {{mxl, mxd}};
		return EMP;
	}
	if(K == 2) {
		vvi B; vpi S;
		for(auto y: {mxd, mnu}) {
			B = rem(A, mxl, y);
			S = sol(B, 1);
			if(S.size()) return {{mxl, y}, S[0]};
		}
		return EMP;
	}
	if(K == 3) {
		vvi B; vpi S;
		for(auto x: {mxl, mnr}) for(auto y: {mxd, mnu}) {
			B = rem(A, x, y);
			S = sol(B, 2);
			if(S.size()) return {{x, y}, S[0], S[1]};
		}
		return EMP;
	}
	if(K == 4) {
		vvi B; vpi S;
		for(auto x: {mxl, mnr}) for(auto y: {mxd, mnu}) {
			B = rem(A, x, y);
			S = sol(B, 3);
			if(S.size()) return {{x, y}, S[0], S[1], S[2]};
		}
		S = sol4(A);
		if(S.size()) return S;
		for(auto& it: A) it = {XS - it[2] + 1, it[1], XS - it[0] + 1, it[3]};
		S = sol4(A);
		for(auto& it: A) it = {XS - it[2] + 1, it[1], XS - it[0] + 1, it[3]};
		if(S.size()) {
			for(auto& [x, y]: S) x = XS - x + 1;
			return S;
		}
		return EMP;
	}
}

int rnd(int l, int r) {
	return l + rand() % (r-l+1);
}

int main() {
	vvi A; 
	int N, K; scanf("%d%d", &N, &K);
	for(int i=0; i<N; i++) {
		vi H(4);
		for(int j=0; j<4; j++) scanf("%d", &H[j]);
		A.push_back(H);
		X.push_back(H[0]);
		X.push_back(H[2]);
		Y.push_back(H[1]);
		Y.push_back(H[3]);
	}
	sort(X.begin(), X.end()); X.resize(unique(X.begin(), X.end()) - X.begin()); XS = X.size();
	sort(Y.begin(), Y.end()); Y.resize(unique(Y.begin(), Y.end()) - Y.begin()); YS = Y.size();
	for(auto& it: A) {
		it[0] = f(X, it[0]);
		it[1] = f(Y, it[1]);
		it[2] = f(X, it[2]);
		it[3] = f(Y, it[3]);
	}
	vpi ans;
	for(int k=1; k<=K && ans.empty(); k++) ans = sol(A, k);
	assert(!ans.empty());
	while((int)ans.size() < K) ans.push_back({1, 1});
	for(auto& [x, y]: ans) printf("%d %d\n", X[x-1], Y[y-1]);
	/*
	int N = 5, K = 4;
	srand(1235);
	while(1) {
		vvi A;
		X.clear();
		Y.clear();
		for(int i=0; i<N; i++) {
			vi H = {rnd(1, 2*N), rnd(1, 2*N), rnd(1, 2*N), rnd(1, 2*N)};
			if(H[0] > H[2]) swap(H[0], H[2]);
			if(H[1] > H[3]) swap(H[1], H[3]);
			A.push_back(H);
			X.push_back(H[0]);
			X.push_back(H[2]);
			Y.push_back(H[1]);
			Y.push_back(H[3]);
		}
		sort(X.begin(), X.end()); X.resize(unique(X.begin(), X.end()) - X.begin()); XS = X.size();
		sort(Y.begin(), Y.end()); Y.resize(unique(Y.begin(), Y.end()) - Y.begin()); YS = Y.size();
		for(auto& it: A) {
			it[0] = f(X, it[0]);
			it[1] = f(Y, it[1]);
			it[2] = f(X, it[2]);
			it[3] = f(Y, it[3]);
		}
		vpi ans;
		for(int k=1; k<=K && ans.empty(); k++) ans = sol(A, k);
		if(ans.empty()) {
			for(auto& it: A) printf("%d %d %d %d\n", it[0], it[1], it[2], it[3]);
			return 0;
		}
	}
	*/
	return 0;
}

Compilation message

hamburg.cpp: In function 'vpi sol(vvi&, int)':
hamburg.cpp:159:1: warning: control reaches end of non-void function [-Wreturn-type]
  159 | }
      | ^
hamburg.cpp: In function 'int main()':
hamburg.cpp:167:17: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  167 |  int N, K; scanf("%d%d", &N, &K);
      |            ~~~~~^~~~~~~~~~~~~~~~
hamburg.cpp:170:31: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  170 |   for(int j=0; j<4; j++) scanf("%d", &H[j]);
      |                          ~~~~~^~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 340 KB Output is correct
2 Correct 2 ms 340 KB Output is correct
3 Correct 2 ms 340 KB Output is correct
4 Correct 3 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 468 KB Output is correct
2 Correct 2 ms 468 KB Output is correct
3 Correct 2 ms 468 KB Output is correct
4 Correct 4 ms 468 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 596 KB Output is correct
2 Correct 3 ms 596 KB Output is correct
3 Correct 3 ms 596 KB Output is correct
4 Correct 2 ms 468 KB Output is correct
5 Correct 2 ms 468 KB Output is correct
6 Correct 3 ms 468 KB Output is correct
7 Correct 2 ms 596 KB Output is correct
8 Correct 3 ms 596 KB Output is correct
9 Correct 4 ms 572 KB Output is correct
10 Correct 3 ms 596 KB Output is correct
11 Correct 3 ms 572 KB Output is correct
12 Correct 2 ms 468 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 576 KB Output is correct
2 Correct 3 ms 468 KB Output is correct
3 Correct 3 ms 596 KB Output is correct
4 Correct 3 ms 596 KB Output is correct
5 Correct 4 ms 596 KB Output is correct
6 Correct 3 ms 468 KB Output is correct
7 Correct 4 ms 740 KB Output is correct
8 Correct 5 ms 800 KB Output is correct
9 Correct 3 ms 652 KB Output is correct
10 Correct 4 ms 708 KB Output is correct
11 Correct 5 ms 724 KB Output is correct
12 Correct 3 ms 588 KB Output is correct
13 Correct 3 ms 592 KB Output is correct
14 Correct 9 ms 1072 KB Output is correct
15 Correct 3 ms 572 KB Output is correct
16 Correct 5 ms 704 KB Output is correct
17 Correct 6 ms 1320 KB Output is correct
18 Correct 4 ms 692 KB Output is correct
19 Correct 4 ms 724 KB Output is correct
20 Correct 8 ms 1576 KB Output is correct
21 Correct 4 ms 724 KB Output is correct
22 Correct 4 ms 768 KB Output is correct
23 Correct 7 ms 1676 KB Output is correct
24 Correct 4 ms 860 KB Output is correct
25 Correct 5 ms 884 KB Output is correct
26 Correct 6 ms 868 KB Output is correct
27 Correct 4 ms 852 KB Output is correct
28 Correct 4 ms 844 KB Output is correct
29 Correct 4 ms 852 KB Output is correct
30 Correct 4 ms 868 KB Output is correct
31 Correct 7 ms 1280 KB Output is correct
32 Correct 7 ms 1084 KB Output is correct
33 Correct 6 ms 1180 KB Output is correct
34 Correct 6 ms 1232 KB Output is correct
35 Correct 7 ms 1576 KB Output is correct
36 Correct 6 ms 1232 KB Output is correct
37 Correct 8 ms 1704 KB Output is correct
38 Correct 10 ms 1768 KB Output is correct
39 Correct 7 ms 1452 KB Output is correct
40 Correct 8 ms 1196 KB Output is correct
41 Correct 6 ms 1448 KB Output is correct
42 Correct 7 ms 1488 KB Output is correct
43 Correct 7 ms 1452 KB Output is correct
44 Correct 7 ms 1520 KB Output is correct
45 Correct 5 ms 896 KB Output is correct
46 Correct 8 ms 1584 KB Output is correct
47 Correct 6 ms 1492 KB Output is correct
48 Correct 10 ms 1780 KB Output is correct
49 Correct 7 ms 1496 KB Output is correct
50 Correct 9 ms 1420 KB Output is correct
51 Correct 8 ms 1704 KB Output is correct
52 Correct 6 ms 1152 KB Output is correct
53 Correct 7 ms 1548 KB Output is correct
54 Correct 9 ms 1728 KB Output is correct
55 Correct 6 ms 1480 KB Output is correct
56 Correct 6 ms 1460 KB Output is correct
57 Correct 6 ms 1436 KB Output is correct
58 Correct 6 ms 1484 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 340 KB Output is correct
2 Correct 2 ms 340 KB Output is correct
3 Correct 2 ms 340 KB Output is correct
4 Correct 3 ms 340 KB Output is correct
5 Correct 255 ms 14396 KB Output is correct
6 Correct 254 ms 14328 KB Output is correct
7 Correct 277 ms 14288 KB Output is correct
8 Correct 271 ms 14288 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 468 KB Output is correct
2 Correct 2 ms 468 KB Output is correct
3 Correct 2 ms 468 KB Output is correct
4 Correct 4 ms 468 KB Output is correct
5 Correct 264 ms 20248 KB Output is correct
6 Correct 278 ms 23740 KB Output is correct
7 Correct 275 ms 20032 KB Output is correct
8 Correct 272 ms 28756 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 596 KB Output is correct
2 Correct 3 ms 596 KB Output is correct
3 Correct 3 ms 596 KB Output is correct
4 Correct 2 ms 468 KB Output is correct
5 Correct 2 ms 468 KB Output is correct
6 Correct 3 ms 468 KB Output is correct
7 Correct 2 ms 596 KB Output is correct
8 Correct 3 ms 596 KB Output is correct
9 Correct 4 ms 572 KB Output is correct
10 Correct 3 ms 596 KB Output is correct
11 Correct 3 ms 572 KB Output is correct
12 Correct 2 ms 468 KB Output is correct
13 Correct 298 ms 26052 KB Output is correct
14 Correct 282 ms 26508 KB Output is correct
15 Correct 295 ms 27888 KB Output is correct
16 Correct 269 ms 21688 KB Output is correct
17 Correct 318 ms 27592 KB Output is correct
18 Correct 273 ms 20732 KB Output is correct
19 Correct 288 ms 32792 KB Output is correct
20 Correct 377 ms 39956 KB Output is correct
21 Correct 297 ms 33880 KB Output is correct
22 Correct 313 ms 40436 KB Output is correct
23 Correct 342 ms 38740 KB Output is correct
24 Correct 326 ms 37000 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 576 KB Output is correct
2 Correct 3 ms 468 KB Output is correct
3 Correct 3 ms 596 KB Output is correct
4 Correct 3 ms 596 KB Output is correct
5 Correct 4 ms 596 KB Output is correct
6 Correct 3 ms 468 KB Output is correct
7 Correct 4 ms 740 KB Output is correct
8 Correct 5 ms 800 KB Output is correct
9 Correct 3 ms 652 KB Output is correct
10 Correct 4 ms 708 KB Output is correct
11 Correct 5 ms 724 KB Output is correct
12 Correct 3 ms 588 KB Output is correct
13 Correct 3 ms 592 KB Output is correct
14 Correct 9 ms 1072 KB Output is correct
15 Correct 3 ms 572 KB Output is correct
16 Correct 5 ms 704 KB Output is correct
17 Correct 6 ms 1320 KB Output is correct
18 Correct 4 ms 692 KB Output is correct
19 Correct 4 ms 724 KB Output is correct
20 Correct 8 ms 1576 KB Output is correct
21 Correct 4 ms 724 KB Output is correct
22 Correct 4 ms 768 KB Output is correct
23 Correct 7 ms 1676 KB Output is correct
24 Correct 4 ms 860 KB Output is correct
25 Correct 5 ms 884 KB Output is correct
26 Correct 6 ms 868 KB Output is correct
27 Correct 4 ms 852 KB Output is correct
28 Correct 4 ms 844 KB Output is correct
29 Correct 4 ms 852 KB Output is correct
30 Correct 4 ms 868 KB Output is correct
31 Correct 7 ms 1280 KB Output is correct
32 Correct 7 ms 1084 KB Output is correct
33 Correct 6 ms 1180 KB Output is correct
34 Correct 6 ms 1232 KB Output is correct
35 Correct 7 ms 1576 KB Output is correct
36 Correct 6 ms 1232 KB Output is correct
37 Correct 8 ms 1704 KB Output is correct
38 Correct 10 ms 1768 KB Output is correct
39 Correct 7 ms 1452 KB Output is correct
40 Correct 8 ms 1196 KB Output is correct
41 Correct 6 ms 1448 KB Output is correct
42 Correct 7 ms 1488 KB Output is correct
43 Correct 7 ms 1452 KB Output is correct
44 Correct 7 ms 1520 KB Output is correct
45 Correct 5 ms 896 KB Output is correct
46 Correct 8 ms 1584 KB Output is correct
47 Correct 6 ms 1492 KB Output is correct
48 Correct 10 ms 1780 KB Output is correct
49 Correct 7 ms 1496 KB Output is correct
50 Correct 9 ms 1420 KB Output is correct
51 Correct 8 ms 1704 KB Output is correct
52 Correct 6 ms 1152 KB Output is correct
53 Correct 7 ms 1548 KB Output is correct
54 Correct 9 ms 1728 KB Output is correct
55 Correct 6 ms 1480 KB Output is correct
56 Correct 6 ms 1460 KB Output is correct
57 Correct 6 ms 1436 KB Output is correct
58 Correct 6 ms 1484 KB Output is correct
59 Correct 360 ms 38240 KB Output is correct
60 Correct 347 ms 31496 KB Output is correct
61 Correct 338 ms 35368 KB Output is correct
62 Correct 322 ms 29440 KB Output is correct
63 Correct 341 ms 35600 KB Output is correct
64 Correct 303 ms 24192 KB Output is correct
65 Correct 387 ms 41960 KB Output is correct
66 Correct 541 ms 45320 KB Output is correct
67 Correct 426 ms 48292 KB Output is correct
68 Correct 669 ms 55524 KB Output is correct
69 Correct 791 ms 61524 KB Output is correct
70 Correct 595 ms 50640 KB Output is correct
71 Correct 399 ms 48756 KB Output is correct
72 Correct 882 ms 132584 KB Output is correct
73 Correct 405 ms 49876 KB Output is correct
74 Correct 531 ms 58720 KB Output is correct
75 Correct 618 ms 93524 KB Output is correct
76 Correct 534 ms 57116 KB Output is correct
77 Correct 375 ms 50664 KB Output is correct
78 Correct 1091 ms 146132 KB Output is correct
79 Correct 396 ms 47768 KB Output is correct
80 Correct 443 ms 47916 KB Output is correct
81 Correct 860 ms 132912 KB Output is correct
82 Correct 507 ms 54540 KB Output is correct
83 Correct 519 ms 53732 KB Output is correct
84 Correct 624 ms 58248 KB Output is correct
85 Correct 666 ms 68320 KB Output is correct
86 Correct 422 ms 48180 KB Output is correct
87 Correct 674 ms 67296 KB Output is correct
88 Correct 528 ms 64196 KB Output is correct
89 Correct 649 ms 111524 KB Output is correct
90 Correct 1020 ms 141884 KB Output is correct
91 Correct 670 ms 104732 KB Output is correct
92 Correct 1052 ms 157364 KB Output is correct
93 Correct 759 ms 133928 KB Output is correct
94 Correct 933 ms 133732 KB Output is correct
95 Correct 854 ms 141168 KB Output is correct
96 Correct 902 ms 122980 KB Output is correct
97 Correct 842 ms 133424 KB Output is correct
98 Correct 934 ms 131464 KB Output is correct
99 Correct 661 ms 107624 KB Output is correct
100 Correct 1051 ms 142484 KB Output is correct
101 Correct 945 ms 138768 KB Output is correct
102 Correct 676 ms 90116 KB Output is correct
103 Correct 1047 ms 146140 KB Output is correct
104 Correct 803 ms 99700 KB Output is correct
105 Correct 991 ms 149836 KB Output is correct
106 Correct 1018 ms 143432 KB Output is correct
107 Correct 831 ms 124784 KB Output is correct
108 Correct 1104 ms 145760 KB Output is correct
109 Correct 927 ms 142816 KB Output is correct
110 Correct 956 ms 137728 KB Output is correct
111 Correct 772 ms 129040 KB Output is correct
112 Correct 1035 ms 148688 KB Output is correct
113 Correct 667 ms 118488 KB Output is correct
114 Correct 665 ms 118372 KB Output is correct
115 Correct 680 ms 118332 KB Output is correct
116 Correct 673 ms 118492 KB Output is correct
117 Correct 892 ms 145256 KB Output is correct
118 Correct 801 ms 145236 KB Output is correct
119 Correct 851 ms 145232 KB Output is correct
120 Correct 801 ms 145236 KB Output is correct
121 Correct 794 ms 145236 KB Output is correct
122 Correct 802 ms 145240 KB Output is correct
123 Correct 802 ms 145260 KB Output is correct
124 Correct 820 ms 145276 KB Output is correct
125 Correct 792 ms 145236 KB Output is correct
126 Correct 802 ms 145248 KB Output is correct