답안 #258850

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
258850 2020-08-06T15:49:40 Z amoo_safar 함박 스테이크 (JOI20_hamburg) C++17
21 / 100
1571 ms 203700 KB
// Zende bad Shoma nasime faghat !
#include <bits/stdc++.h>

#define pb push_back
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define debug(x) cerr << #x << " : " << x << '\n'

using namespace std;

typedef long long ll;
typedef long double ld;
typedef string str;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;

const ll Mod = 1000000007LL;
const int N = 2e5 + 10;
const ll Inf = 2242545357980376863LL;
const ll Log = 30;

int n, k;
int L[N], R[N], U[N], D[N];

vector<int> uX, uY;
vector<pii> points;

void Done(){
	while((int) points.size() < k) points.pb(pii(0, 0));
	for(auto x : points) printf("%d %d\n", uX[x.F], uY[x.S]);
	exit(0);
}

int mk[N];
bool In(pii P, int id){
	return L[id] <= P.F && P.F <= R[id] && D[id] <= P.S && P.S <= U[id];
}
void On(pii P, int col){
	for(int i = 0; i < n; i++){
		if(L[i] <= P.F && P.F <= R[i] && D[i] <= P.S && P.S <= U[i] && !mk[i]) mk[i] = col;
	}
	points.pb(P);
}
void Off(int col){
	for(int i = 0; i < n; i++)
		if(mk[i] == col)
			mk[i] = 0;
	points.pop_back();
}

void Solve(int y){
	if(y == 0){
		for(int i = 0; i < n; i++)
			if(!mk[i])
				return ;
	}
	int mnx = N, mxx = -1;
	for(int i = 0; i < n; i++)
		if(!mk[i])
			mnx = min(mnx, R[i]), mxx = max(mxx, L[i]);
	if(mxx == -1){
		for(int i = 0; i < y; i++) points.pb(pii(0, 0));
		Done();
	}


	int mny = N, mxy = -1;
	for(int i = 0; i < n; i++)
		if(!mk[i])
			mny = min(mny, U[i]), mxy = max(mxy, D[i]);
	
	for(int px : {mnx, mxx}){
		for(int py : {mny, mxy}){
			On({px, py}, y);
			Solve(y - 1);
			Off(y);
		}
	}
}

const int N2 = N * 4;
vector<int> O[N2], I[N2];
void AddE(int u, int v){
	O[u].pb(v);
	I[v].pb(u);
}
void AddC(int u, int v){
	//if(u == v);
	AddE(u ^ 1, v);
	AddE(v ^ 1, u);
}
void Ban(int u, int v){
	AddC(u ^ 1, v ^ 1);
}

int mark[N2];
vector<int> top;
void DFS(int u){
	mark[u] = 1;
	for(auto adj : O[u]) if(!mark[adj]) DFS(adj);
	top.pb(u);
}

void DFS(int u, int c){
	mark[u] = c;
	for(auto adj : I[u]) if(!mark[adj]) DFS(adj, c);
}

bool isst[N];

void Main(){

	int mnx = N, mxx = -1;
	for(int i = 0; i < n; i++)
		if(!mk[i])
			mnx = min(mnx, R[i]), mxx = max(mxx, L[i]);
	int mny = N, mxy = -1;
	for(int i = 0; i < n; i++)
		if(!mk[i])
			mny = min(mny, U[i]), mxy = max(mxy, D[i]);
	
	vector<pii> po;
	vector<int> ln;
	{ // Nodes
		int st = 0;
		for(int i = mnx + 1; i < mxx; i++)
			po.pb({i, mny});	
		
		isst[st] = true;
		for(int i = st; i + 1 < (int) po.size(); i++)
			Ban(i << 1 | 1, (i + 1) << 1);
		st = po.size();
		/////////////////////
		for(int i = mnx + 1; i < mxx; i++)
			po.pb({i, mxy});

		isst[st] = true;
		for(int i = st; i + 1 < (int) po.size(); i++)
			Ban(i << 1 | 1, (i + 1) << 1);
		st = po.size();
		/////////////////////
		for(int i = mny + 1; i < mxy; i++)
			po.pb({mnx, i});	
		
		isst[st] = true;
		for(int i = st; i + 1 < (int) po.size(); i++)
			Ban(i << 1 | 1, (i + 1) << 1);
		st = po.size();
		/////////////////////
		for(int i = mny + 1; i < mxy; i++)
			po.pb({mxx, i});

		isst[st] = true;
		for(int i = st; i + 1 < (int) po.size(); i++)
			Ban(i << 1 | 1, (i + 1) << 1);
		st = po.size();
	}
	int m = po.size();
	for(int i = 0; i < m; i++) if(isst[i]) ln.pb(i);
	ln.pb(m);
	assert(ln.size() == 5);

	int fst, la;
	vector<pii> seg;
	for(int i = 0; i < n; i++){
		seg.clear();
		for(int j = 0; j < 4; j++){
			fst = -1; la = -1;
			for(int l = ln[j]; l < ln[j + 1]; l++){
				if(In(po[l], i)){
					la = l;
					if(fst == -1) fst = l;
				}
			}

			if(fst == -1) continue;
			seg.pb({fst, la});
		}

		if(seg.empty() || seg.size() > 2) continue;
		if(seg.size() == 1){
			AddC(seg[0].S << 1 | 1, seg[0].S << 1 | 1);
			if(!isst[seg[0].F])
				AddC((seg[0].F - 1) << 1, (seg[0].F - 1) << 1);
		} else {
			vector<int> B[2];
			for(int j = 0; j < 2; j++){
				B[j].pb(seg[j].S << 1 | 0);
				if(!isst[seg[j].F])
					B[j].pb((seg[j].F - 1) << 1 | 1);
			}
			for(int b1 : B[0]) for(int b2 : B[1]) Ban(b1, b2);
		}
	}

	for(int i = 0; i < m + m; i++){
		if(!mark[i]) DFS(i);
	}
	reverse(all(top));
	memset(mark, 0, sizeof mark);
	int cnt = 1;
	for(auto x : top) if(!mark[x]){
		DFS(x, cnt);
		cnt ++;
	}
	vector<int> val(m, 0);
	for(int i = 0; i < m; i++)
		val[i] = (mark[i + i] < mark[i + i + 1] ? 1 : 0);

	for(int j = 0; j < 4; j++){
		for(int l = ln[j]; l < ln[j + 1]; l++){
			if(val[l]){
				points.pb(po[l]);
				break;
			}
		}
	}
	Done();
}

int main(){
	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	scanf("%d%d", &n, &k);
	for(int i = 0; i < n; i++) scanf("%d%d%d%d", L + i, D + i, R + i, U + i);

	for(int i = 0; i < n; i++) uX.pb(L[i]), uY.pb(D[i]), uX.pb(R[i]), uY.pb(U[i]);
	
	sort(all(uX)); uX.resize(unique(all(uX)) - uX.begin());
	sort(all(uY)); uY.resize(unique(all(uY)) - uY.begin());

	for(int i = 0; i < n; i++){
		L[i] = lower_bound(all(uX), L[i]) - uX.begin();
		R[i] = lower_bound(all(uX), R[i]) - uX.begin();
		U[i] = lower_bound(all(uY), U[i]) - uY.begin();
		D[i] = lower_bound(all(uY), D[i]) - uY.begin();
	}

	//assert(k <= 3);

	Solve(k);
	assert(k == 4);
	Main();

	return 0;
}

/*
4 4
1 4 1 4
2 1 2 1
3 0 3 0
4 3 4 3

*/

Compilation message

hamburg.cpp: In function 'int main()':
hamburg.cpp:224:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  224 |  scanf("%d%d", &n, &k);
      |  ~~~~~^~~~~~~~~~~~~~~~
hamburg.cpp:225:34: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  225 |  for(int i = 0; i < n; i++) scanf("%d%d%d%d", L + i, D + i, R + i, U + i);
      |                             ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 29 ms 38008 KB Output is correct
2 Correct 31 ms 38016 KB Output is correct
3 Correct 36 ms 38012 KB Output is correct
4 Correct 32 ms 38060 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 35 ms 38008 KB Output is correct
2 Correct 31 ms 38016 KB Output is correct
3 Correct 30 ms 38016 KB Output is correct
4 Correct 30 ms 38016 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 40 ms 38016 KB Output is correct
2 Correct 32 ms 38016 KB Output is correct
3 Correct 30 ms 38016 KB Output is correct
4 Correct 30 ms 38016 KB Output is correct
5 Correct 29 ms 38016 KB Output is correct
6 Correct 33 ms 38008 KB Output is correct
7 Correct 29 ms 38016 KB Output is correct
8 Correct 31 ms 38016 KB Output is correct
9 Correct 31 ms 38016 KB Output is correct
10 Correct 30 ms 38016 KB Output is correct
11 Correct 33 ms 38016 KB Output is correct
12 Correct 30 ms 38016 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 30 ms 38016 KB Output is correct
2 Correct 33 ms 38016 KB Output is correct
3 Correct 31 ms 38016 KB Output is correct
4 Correct 31 ms 38004 KB Output is correct
5 Correct 31 ms 38016 KB Output is correct
6 Correct 33 ms 38016 KB Output is correct
7 Correct 33 ms 38008 KB Output is correct
8 Correct 32 ms 38016 KB Output is correct
9 Correct 32 ms 38016 KB Output is correct
10 Correct 34 ms 38008 KB Output is correct
11 Correct 30 ms 38016 KB Output is correct
12 Correct 30 ms 38008 KB Output is correct
13 Correct 41 ms 38008 KB Output is correct
14 Correct 63 ms 42104 KB Output is correct
15 Correct 40 ms 38008 KB Output is correct
16 Correct 37 ms 38016 KB Output is correct
17 Correct 68 ms 42320 KB Output is correct
18 Correct 36 ms 38008 KB Output is correct
19 Correct 38 ms 38136 KB Output is correct
20 Correct 71 ms 42592 KB Output is correct
21 Correct 39 ms 38016 KB Output is correct
22 Correct 37 ms 38016 KB Output is correct
23 Correct 67 ms 42616 KB Output is correct
24 Correct 36 ms 37964 KB Output is correct
25 Correct 34 ms 38016 KB Output is correct
26 Correct 35 ms 38032 KB Output is correct
27 Correct 36 ms 38092 KB Output is correct
28 Correct 35 ms 38016 KB Output is correct
29 Correct 34 ms 38016 KB Output is correct
30 Correct 33 ms 38016 KB Output is correct
31 Correct 61 ms 42176 KB Output is correct
32 Correct 69 ms 41980 KB Output is correct
33 Correct 60 ms 42232 KB Output is correct
34 Correct 76 ms 42104 KB Output is correct
35 Correct 67 ms 42616 KB Output is correct
36 Correct 76 ms 42104 KB Output is correct
37 Correct 91 ms 42872 KB Output is correct
38 Correct 76 ms 43000 KB Output is correct
39 Correct 63 ms 42364 KB Output is correct
40 Correct 78 ms 42232 KB Output is correct
41 Correct 68 ms 42360 KB Output is correct
42 Correct 67 ms 42360 KB Output is correct
43 Correct 66 ms 42360 KB Output is correct
44 Correct 64 ms 42488 KB Output is correct
45 Correct 34 ms 38016 KB Output is correct
46 Correct 82 ms 42488 KB Output is correct
47 Correct 63 ms 42488 KB Output is correct
48 Correct 69 ms 42836 KB Output is correct
49 Correct 67 ms 42488 KB Output is correct
50 Correct 67 ms 42360 KB Output is correct
51 Correct 70 ms 42744 KB Output is correct
52 Correct 58 ms 42232 KB Output is correct
53 Correct 63 ms 42360 KB Output is correct
54 Correct 69 ms 42836 KB Output is correct
55 Correct 68 ms 42360 KB Output is correct
56 Correct 80 ms 42312 KB Output is correct
57 Correct 78 ms 42360 KB Output is correct
58 Correct 67 ms 42360 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 29 ms 38008 KB Output is correct
2 Correct 31 ms 38016 KB Output is correct
3 Correct 36 ms 38012 KB Output is correct
4 Correct 32 ms 38060 KB Output is correct
5 Correct 398 ms 45148 KB Output is correct
6 Correct 383 ms 45144 KB Output is correct
7 Correct 473 ms 45144 KB Output is correct
8 Correct 402 ms 45144 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 35 ms 38008 KB Output is correct
2 Correct 31 ms 38016 KB Output is correct
3 Correct 30 ms 38016 KB Output is correct
4 Correct 30 ms 38016 KB Output is correct
5 Correct 439 ms 45140 KB Output is correct
6 Correct 431 ms 45156 KB Output is correct
7 Correct 399 ms 45208 KB Output is correct
8 Correct 411 ms 45236 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 40 ms 38016 KB Output is correct
2 Correct 32 ms 38016 KB Output is correct
3 Correct 30 ms 38016 KB Output is correct
4 Correct 30 ms 38016 KB Output is correct
5 Correct 29 ms 38016 KB Output is correct
6 Correct 33 ms 38008 KB Output is correct
7 Correct 29 ms 38016 KB Output is correct
8 Correct 31 ms 38016 KB Output is correct
9 Correct 31 ms 38016 KB Output is correct
10 Correct 30 ms 38016 KB Output is correct
11 Correct 33 ms 38016 KB Output is correct
12 Correct 30 ms 38016 KB Output is correct
13 Correct 461 ms 45148 KB Output is correct
14 Correct 428 ms 45240 KB Output is correct
15 Correct 415 ms 45236 KB Output is correct
16 Correct 400 ms 45276 KB Output is correct
17 Correct 410 ms 45144 KB Output is correct
18 Correct 468 ms 45140 KB Output is correct
19 Correct 440 ms 45136 KB Output is correct
20 Correct 438 ms 45268 KB Output is correct
21 Correct 594 ms 45272 KB Output is correct
22 Correct 541 ms 45136 KB Output is correct
23 Correct 519 ms 45272 KB Output is correct
24 Correct 502 ms 45248 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 30 ms 38016 KB Output is correct
2 Correct 33 ms 38016 KB Output is correct
3 Correct 31 ms 38016 KB Output is correct
4 Correct 31 ms 38004 KB Output is correct
5 Correct 31 ms 38016 KB Output is correct
6 Correct 33 ms 38016 KB Output is correct
7 Correct 33 ms 38008 KB Output is correct
8 Correct 32 ms 38016 KB Output is correct
9 Correct 32 ms 38016 KB Output is correct
10 Correct 34 ms 38008 KB Output is correct
11 Correct 30 ms 38016 KB Output is correct
12 Correct 30 ms 38008 KB Output is correct
13 Correct 41 ms 38008 KB Output is correct
14 Correct 63 ms 42104 KB Output is correct
15 Correct 40 ms 38008 KB Output is correct
16 Correct 37 ms 38016 KB Output is correct
17 Correct 68 ms 42320 KB Output is correct
18 Correct 36 ms 38008 KB Output is correct
19 Correct 38 ms 38136 KB Output is correct
20 Correct 71 ms 42592 KB Output is correct
21 Correct 39 ms 38016 KB Output is correct
22 Correct 37 ms 38016 KB Output is correct
23 Correct 67 ms 42616 KB Output is correct
24 Correct 36 ms 37964 KB Output is correct
25 Correct 34 ms 38016 KB Output is correct
26 Correct 35 ms 38032 KB Output is correct
27 Correct 36 ms 38092 KB Output is correct
28 Correct 35 ms 38016 KB Output is correct
29 Correct 34 ms 38016 KB Output is correct
30 Correct 33 ms 38016 KB Output is correct
31 Correct 61 ms 42176 KB Output is correct
32 Correct 69 ms 41980 KB Output is correct
33 Correct 60 ms 42232 KB Output is correct
34 Correct 76 ms 42104 KB Output is correct
35 Correct 67 ms 42616 KB Output is correct
36 Correct 76 ms 42104 KB Output is correct
37 Correct 91 ms 42872 KB Output is correct
38 Correct 76 ms 43000 KB Output is correct
39 Correct 63 ms 42364 KB Output is correct
40 Correct 78 ms 42232 KB Output is correct
41 Correct 68 ms 42360 KB Output is correct
42 Correct 67 ms 42360 KB Output is correct
43 Correct 66 ms 42360 KB Output is correct
44 Correct 64 ms 42488 KB Output is correct
45 Correct 34 ms 38016 KB Output is correct
46 Correct 82 ms 42488 KB Output is correct
47 Correct 63 ms 42488 KB Output is correct
48 Correct 69 ms 42836 KB Output is correct
49 Correct 67 ms 42488 KB Output is correct
50 Correct 67 ms 42360 KB Output is correct
51 Correct 70 ms 42744 KB Output is correct
52 Correct 58 ms 42232 KB Output is correct
53 Correct 63 ms 42360 KB Output is correct
54 Correct 69 ms 42836 KB Output is correct
55 Correct 68 ms 42360 KB Output is correct
56 Correct 80 ms 42312 KB Output is correct
57 Correct 78 ms 42360 KB Output is correct
58 Correct 67 ms 42360 KB Output is correct
59 Correct 441 ms 45232 KB Output is correct
60 Correct 435 ms 45264 KB Output is correct
61 Correct 423 ms 45236 KB Output is correct
62 Correct 403 ms 45140 KB Output is correct
63 Correct 425 ms 45144 KB Output is correct
64 Correct 403 ms 45188 KB Output is correct
65 Correct 612 ms 45236 KB Output is correct
66 Runtime error 1571 ms 203700 KB Execution killed with signal 11 (could be triggered by violating memory limits)
67 Halted 0 ms 0 KB -