QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#372023#3313. Emergency EvacuationPetroTarnavskyi#AC ✓76ms19712kbC++203.0kb2024-03-30 19:35:582024-03-30 19:36:08

Judging History

你现在查看的是最新测评结果

  • [2024-03-30 19:36:08]
  • 评测
  • 测评结果:AC
  • 用时:76ms
  • 内存:19712kb
  • [2024-03-30 19:35:58]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define RFOR(i, a, b) for(int i = (a) - 1; i >= (b); i--)
#define SZ(a) int(a.size())
#define ALL(a) a.begin(), a.end()
#define PB push_back
#define MP make_pair
#define F first
#define S second

typedef long long LL;
typedef vector<int> VI;
typedef pair<int, int> PII;
typedef double db;

const int N = 1047;

int a[N][N];
int heads[2][N * N];
int sz[2];
int p[N * N];
PII c[N * N];
int n;
int mid;
int r;

void add(int x, int t)
{
	heads[t][sz[t]++] = x;
}

void erase(int pos, int t)
{
	sz[t]--;
	swap(heads[t][pos], heads[t][sz[t]]);
}

PII next(int i)
{
	auto [x, y] = c[i];
	if (x == mid)
		return {x, y + 1};
	if (x < mid)
		return {x + 1, y};
	else
		return {x - 1, y};
}

int findAss(int i)
{
	auto [x, y] = c[i];
	auto [nx, ny] = next(i);
	if (nx == mid && x != mid)
		return -1;
	return a[nx][ny];
}

bool move(int i)
{
	bool ok = false;
	auto [x, y] = c[i];
	auto [nx, ny] = next(i);
	if (a[nx][ny] != -1)
		return false;
	if (ny == r)
		ok = true;
	if (!ok)
		a[nx][ny] = i;
	a[x][y] = -1;
	c[i] = {nx, ny};
	int idx = p[i];
	if (nx == mid && x != mid)
	{
		p[i] = -1;
		if (idx != -1)
			add(idx, 1);
	}
	if (ok && idx != -1)
		add(idx, 1);
	while (idx != -1)
	{
		tie(nx, ny) = next(idx);
		assert(nx == x && ny == y);
		tie(x, y) = c[idx];
		assert(a[nx][ny] == -1);
		a[nx][ny] = idx;
		c[idx] = {nx, ny};
		a[x][y] = -1;
		idx = p[idx];
	}
	return ok;
}

int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	
	
	FOR (i, 0, N) FOR (j, 0, N) a[i][j] = -1;
	fill(p, p + N * N, -1);
	
	int s;
	cin >> r >> s >> n;
		
	mid = s;
	FOR (i, 0, n)
	{
		int x, y;
		cin >> y >> x;
		y--;
		if (x <= mid)
			x--;
		c[i] = {x, y};
		a[x][y] = i;
		add(i, 0);
	}
	int ans = 0;
	int er = -1;
	while (true)
	{
		//if (ans > 7)
		//	break;
		//cerr << "Iter: " << ans << '\n';
		//FOR (i, 0, sz[0])
		//	cerr << heads[0][i] << ' ';
		//cerr << '\n';
		//FOR (i, 0, n)
		//{
		//	cerr << i << ' ' << c[i].F << ' ' << c[i].S << '\n';
		//}
		//cerr << "------------\n";
		FOR (i, 0, sz[0])
		{
			int h = heads[0][i];
			if (h == er)
			{
				n--;
				erase(i, 0);
				i--;
				continue;
			}
			int x = findAss(h);
			if (x != -1)
			{
				assert(p[x] == -1);
				p[x] = h;
				erase(i, 0);
				i--;
			}
		}
		if (sz[0] == 0)
			break;
		er = -1;
		int cnt = 0;
		FOR (i, 0, sz[0])
		{
			int h = heads[0][i];
			auto [x, y] = c[h];
			if (x == mid)
			{
				cnt++;
				if (move(h))
					er = h;
			}
		}
		bool br = cnt == 1 && er != -1;
		FOR (i, 0, sz[0])
		{
			int h = heads[0][i];
			auto [x, y] = c[h];
			if (x != mid)
			{
				move(h);
				br &= (c[h].F == x && c[h].S == y);
			}
		}
		if (br)
			break;
		FOR (i, 0, sz[1])
			add(heads[1][i], 0);
		sz[1] = 0;
		ans++;
	}
	cout << ans + n << '\n';
	cerr << db(clock()) / CLOCKS_PER_SEC << '\n';
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 61ms
memory: 18660kb

input:

500 500 475000
471 382
363 533
10 249
432 511
453 185
338 745
369 837
78 8
141 531
23 175
209 819
458 506
114 909
123 347
442 193
473 300
92 652
194 153
377 372
141 608
308 540
458 90
181 522
257 743
115 752
90 58
55 803
394 478
425 588
248 231
170 326
470 463
92 881
137 48
454 599
96 413
31 1000
29...

output:

475026

result:

ok single line: '475026'

Test #2:

score: 0
Accepted
time: 76ms
memory: 19712kb

input:

500 500 500000
200 367
499 822
451 305
102 514
337 592
383 600
314 158
287 989
325 827
447 266
133 698
96 851
229 284
259 954
174 110
431 15
51 45
187 770
485 793
17 478
121 668
8 675
60 500
288 732
180 627
260 666
222 640
128 14
154 824
92 522
37 748
297 47
122 952
436 24
414 527
385 339
290 52
275...

output:

500001

result:

ok single line: '500001'

Test #3:

score: 0
Accepted
time: 0ms
memory: 12984kb

input:

10 2 5
10 1
10 2
10 3
10 4
1 1

output:

12

result:

ok single line: '12'

Test #4:

score: 0
Accepted
time: 0ms
memory: 13068kb

input:

10 2 5
10 1
10 2
10 3
10 4
1 4

output:

12

result:

ok single line: '12'

Test #5:

score: 0
Accepted
time: 0ms
memory: 13172kb

input:

10 2 6
10 1
10 2
10 3
10 4
1 1
1 4

output:

13

result:

ok single line: '13'

Test #6:

score: 0
Accepted
time: 3ms
memory: 14660kb

input:

100 100 10000
39 51
21 136
45 179
36 129
3 155
20 127
49 135
7 140
3 127
3 139
21 102
8 196
37 191
27 89
16 78
15 14
3 140
18 149
33 84
17 33
19 120
29 74
47 17
2 20
34 26
27 84
40 114
3 8
12 43
39 28
8 142
12 171
39 147
44 46
36 46
44 73
15 23
1 190
32 8
18 145
16 93
22 74
21 47
21 73
3 157
1 114
2...

output:

10051

result:

ok single line: '10051'

Test #7:

score: 0
Accepted
time: 0ms
memory: 12896kb

input:

110 101 11332
47 179
25 17
48 73
31 96
4 69
10 10
24 17
44 160
55 27
19 60
44 78
22 27
28 185
32 47
2 58
3 175
1 84
47 117
17 44
12 93
33 132
2 115
53 135
9 19
49 130
50 201
45 24
25 179
29 43
38 20
7 68
14 135
10 169
46 68
54 48
5 153
35 95
37 10
26 102
11 145
42 103
52 199
5 129
11 112
45 57
6 190...

output:

11387

result:

ok single line: '11387'

Test #8:

score: 0
Accepted
time: 0ms
memory: 13696kb

input:

120 102 12729
2 68
13 194
6 33
14 56
53 77
12 9
7 134
36 172
40 98
14 143
61 175
56 6
20 132
37 10
61 166
34 186
60 18
17 45
20 90
7 63
17 22
1 108
27 115
29 181
13 162
45 12
28 136
39 77
46 149
35 177
42 200
9 157
35 140
17 95
50 55
41 149
61 128
1 163
15 175
29 183
16 96
47 55
34 108
31 11
23 202
...

output:

12788

result:

ok single line: '12788'

Test #9:

score: 0
Accepted
time: 2ms
memory: 14100kb

input:

130 103 14193
39 106
23 78
32 179
57 124
67 161
15 81
12 199
33 52
50 103
51 199
31 173
35 150
31 81
60 8
29 205
38 4
50 9
62 127
9 41
29 27
60 19
52 150
4 128
69 21
64 42
54 78
2 182
54 121
29 127
32 87
15 199
63 145
21 169
30 25
28 117
37 161
10 116
48 1
64 155
54 47
55 55
13 135
20 43
27 55
45 5
...

output:

14255

result:

ok single line: '14255'

Test #10:

score: 0
Accepted
time: 3ms
memory: 14152kb

input:

140 104 15724
59 191
12 75
23 89
15 197
69 91
3 93
48 9
58 149
9 179
58 34
11 78
41 166
62 151
61 63
10 104
21 199
15 184
30 85
50 152
21 72
42 52
54 8
19 73
18 143
19 86
3 161
73 10
16 152
66 171
30 106
72 112
59 102
8 139
34 152
19 152
22 30
11 107
11 135
66 196
35 13
26 185
14 89
54 167
55 164
45...

output:

15789

result:

ok single line: '15789'

Test #11:

score: 0
Accepted
time: 3ms
memory: 14228kb

input:

150 105 17325
8 56
57 162
72 32
56 116
79 109
41 197
7 62
77 60
31 59
50 101
28 37
35 130
65 129
21 122
14 210
82 144
77 145
21 105
48 108
68 149
38 120
45 132
4 181
9 73
80 105
26 34
2 32
38 65
22 35
14 119
51 182
60 99
1 37
6 29
23 10
47 193
31 143
76 85
44 108
10 33
81 168
51 111
6 45
73 63
68 13...

output:

17393

result:

ok single line: '17393'

Test #12:

score: 0
Accepted
time: 3ms
memory: 14040kb

input:

160 106 18995
68 122
54 84
20 184
13 16
40 29
50 139
44 189
21 108
40 109
36 55
65 184
89 76
10 31
62 76
82 197
37 92
62 128
41 129
44 7
83 3
66 50
69 178
13 212
11 133
20 17
81 84
51 125
2 131
80 56
1 175
72 20
34 117
73 1
5 211
64 106
45 165
18 105
79 45
13 200
85 101
80 120
86 181
74 41
67 211
53...

output:

19066

result:

ok single line: '19066'

Test #13:

score: 0
Accepted
time: 3ms
memory: 13640kb

input:

170 107 20736
88 89
78 209
35 49
92 193
97 191
30 38
46 101
70 200
4 133
2 133
58 142
42 183
13 27
87 85
83 148
40 87
5 196
30 101
55 132
72 179
74 154
92 20
88 119
67 132
58 84
10 1
15 28
73 185
70 173
33 123
66 60
80 151
75 23
54 138
82 184
71 59
94 185
90 214
60 148
59 203
14 55
71 171
43 51
72 4...

output:

20810

result:

ok single line: '20810'

Test #14:

score: 0
Accepted
time: 4ms
memory: 13292kb

input:

180 108 22550
27 35
52 133
99 159
2 28
83 109
2 6
105 55
93 62
44 157
47 116
100 208
62 144
86 41
24 196
76 206
62 68
8 210
12 23
94 142
79 133
26 90
35 199
57 45
55 12
13 102
6 154
75 48
93 179
1 43
59 118
44 104
74 189
11 55
100 122
9 100
88 181
90 45
83 60
47 200
66 182
74 5
40 39
1 21
80 120
23 ...

output:

22627

result:

ok single line: '22627'

Test #15:

score: 0
Accepted
time: 0ms
memory: 14544kb

input:

190 109 24437
67 165
27 190
57 194
54 170
37 155
39 41
41 78
100 94
2 47
92 117
24 52
29 136
73 13
41 2
27 205
111 116
42 29
17 21
79 129
28 39
105 77
109 108
15 57
110 199
61 26
13 12
18 215
59 93
22 77
2 23
6 8
51 81
92 134
85 67
101 162
105 159
84 74
82 169
56 69
89 107
35 75
53 199
99 213
2 53
1...

output:

24516

result:

ok single line: '24516'

Test #16:

score: 0
Accepted
time: 0ms
memory: 14284kb

input:

5 2 7
1 1
1 2
1 3
2 3
2 4
4 4
5 2

output:

9

result:

ok single line: '9'

Test #17:

score: 0
Accepted
time: 4ms
memory: 14016kb

input:

500 500 16
1 1
1 2
1 999
1 1000
2 1
2 2
2 999
2 1000
3 1
3 2
3 999
3 1000
499 500
499 501
499 999
499 1000

output:

1008

result:

ok single line: '1008'

Test #18:

score: 0
Accepted
time: 0ms
memory: 13192kb

input:

300 300 1
300 100

output:

202

result:

ok single line: '202'

Test #19:

score: 0
Accepted
time: 4ms
memory: 12612kb

input:

300 300 1
300 400

output:

101

result:

ok single line: '101'

Test #20:

score: 0
Accepted
time: 0ms
memory: 13076kb

input:

1 1 1
1 1

output:

2

result:

ok single line: '2'

Test #21:

score: 0
Accepted
time: 0ms
memory: 13000kb

input:

1 1 1
1 2

output:

2

result:

ok single line: '2'

Test #22:

score: 0
Accepted
time: 0ms
memory: 13736kb

input:

1 1 2
1 1
1 2

output:

3

result:

ok single line: '3'

Test #23:

score: 0
Accepted
time: 0ms
memory: 14244kb

input:

300 300 1800
3 351
1 173
3 100
3 518
3 34
2 428
1 247
3 92
3 436
1 188
1 515
2 442
2 453
1 341
2 370
2 378
3 290
3 225
2 107
2 69
2 195
2 79
3 542
2 200
3 11
3 299
1 308
1 443
3 82
2 422
3 549
2 461
3 235
1 196
1 157
2 8
2 333
1 288
2 198
2 599
1 383
2 247
2 322
2 592
1 6
2 253
1 174
1 113
2 293
3 4...

output:

2098

result:

ok single line: '2098'

Test #24:

score: 0
Accepted
time: 0ms
memory: 14560kb

input:

300 300 1
99 584

output:

486

result:

ok single line: '486'

Test #25:

score: 0
Accepted
time: 0ms
memory: 13544kb

input:

300 300 1
143 91

output:

368

result:

ok single line: '368'

Test #26:

score: 0
Accepted
time: 4ms
memory: 13176kb

input:

300 300 1
8 547

output:

540

result:

ok single line: '540'

Test #27:

score: 0
Accepted
time: 2ms
memory: 14180kb

input:

300 300 1
81 302

output:

222

result:

ok single line: '222'

Test #28:

score: 0
Accepted
time: 0ms
memory: 13504kb

input:

300 300 1
103 476

output:

374

result:

ok single line: '374'

Test #29:

score: 0
Accepted
time: 0ms
memory: 13860kb

input:

300 300 1
105 142

output:

355

result:

ok single line: '355'

Test #30:

score: 0
Accepted
time: 0ms
memory: 13028kb

input:

300 300 1
148 478

output:

331

result:

ok single line: '331'

Test #31:

score: 0
Accepted
time: 4ms
memory: 12932kb

input:

300 300 1
30 497

output:

468

result:

ok single line: '468'

Test #32:

score: 0
Accepted
time: 0ms
memory: 13300kb

input:

300 300 1
59 116

output:

427

result:

ok single line: '427'