UOJ Logo

NOI.AC

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#197490#3450. 左脑右脑FAT1001581ms448544kbC++113.5kb2023-11-12 12:07:522023-11-12 13:20:10

answer

#include <bits/stdc++.h>
#define PB push_back
#define EB emplace_back
using namespace std;
typedef long long i64;
const int maxn = 30;
int n, m, L, R;
int a[maxn + 5][maxn + 5], b[maxn + 5][maxn + 5];
i64 f1[maxn + 1][maxn + 1][maxn + 1][maxn + 1][maxn + 1], f2[maxn + 1][maxn + 1][maxn + 1][maxn + 1][maxn + 1];
inline void upd(i64& x, i64 y) { x = min(x, y); }
void solve(i64 f[][maxn + 1][maxn + 1][maxn + 1][maxn + 1], int v[][maxn + 5]) {
	f[0][0][0][0][0] = 0;
	for (int i = 1; i <= n; i++)
		for (int j = i - 1; ~j; j--)
			for (int k = i - j - 1; ~k; k--)
				for (int x = j; x <= m; x++)
					for (int y = k; x + y <= m; y++) {
						i64 nw = f[i - 1][j][k][x][y];
						if (nw >= 1E18) continue;
						f[i][j][k][x][y] = nw + v[i][0];
						for (int z = 1; x + y + z <= m; z++) {
							upd(f[i][j + 1][k][x + z][y], nw + v[i][z]);
							upd(f[i][j][k + 1][x][y + z], nw + v[i][z]);
						}
					}
}
void plan(i64 f[][maxn + 1][maxn + 1][maxn + 1][maxn + 1], int v[][maxn + 5], int j, int k, int x, int y, int bl[], int deg[]) {
	for (int i = n; i; i--) {
		if (f[i][j][k][x][y] == f[i - 1][j][k][x][y] + v[i][0]) { bl[i] = 1, deg[i] = 0; goto ed; }
		if (j) {
			for (int z = 1; z <= x; z++)
				if (f[i][j][k][x][y] == f[i - 1][j - 1][k][x - z][y] + v[i][z]) {
					bl[i] = 0, deg[i] = z;
					j--, x -= z;
					goto ed;
				}
		}
		if (k) {
			for (int z = 1; z <= y; z++)
				if (f[i][j][k][x][y] == f[i - 1][j][k - 1][x][y - z] + v[i][z]) {
					bl[i] = 1, deg[i] = z;
					k--, y -= z;
					goto ed;
				}
		}
		ed:; 
	}
}
int bl1[maxn + 5], bl2[maxn + 5];
int deg1[maxn + 5], deg2[maxn + 5];
int main() {
	scanf("%d%d%d%d", &n, &m, &L, &R);
	if (m < L || !R) return puts("NO"), 0;
	for (int i = 1; i <= n; i++)
		for (int j = 0; j <= m; j++) scanf("%d", &a[i][j]);
	for (int i = 1; i <= n; i++)
		for (int j = 0; j <= m; j++) scanf("%d", &b[i][j]);
	memset(f1, 63, sizeof(f1)), memset(f2, 63, sizeof(f2));
	solve(f1, a), solve(f2, b);
	i64 ans = 1E18;
	int _1, _2, _3, _4, _5, _6, _7, _8;
	for (int i = 0; i <= R; i++)
		for (int j = 0; i + j <= n; j++)
			for (int k = max(L - i, 0); k <= min(R - i, j); k++)
				for (int l = i; k + l <= n; l++)
					for (int x = i; x <= m - j; x++) {
						i64 nw = f1[n][i][j][x][m - x];
						if (nw >= 1E18) continue;
						for (int y = max(k, m - x); y <= m - l; y++)
							if (nw + f2[n][k][l][y][m - y] < ans) {
								ans = nw + f2[n][k][l][y][m - y];
								_1 = i, _2 = j, _3 = x, _4 = m - x, _5 = k, _6 = l, _7 = y, _8 = m - y; 
							}
					}
	printf("YES\n%lld\n", ans);
	plan(f1, a, _1, _2, _3, _4, bl1, deg1);
	plan(f2, b, _5, _6, _7, _8, bl2, deg2);
	for (int i = 1, j = 1; i <= n; i++) {
		if (bl1[i]) continue;
		while (!bl2[j] || !deg2[j]) j++;
		printf("%d %d\n", i, j + n);
		deg1[i]--, deg2[j++]--;
	}
	for (int i = 1, j = 1; i <= n; i++) {
		if (bl2[i]) continue;
		while (!bl1[j] || !deg1[j]) j++;
		printf("%d %d\n", j, i + n);
		deg2[i]--, deg1[j++]--;
	}
	for (int i = 1, j = 1; i <= n; i++) {
		if (!bl1[i]) continue;
		while (deg1[i]) {
			while (bl2[j] || !deg2[j]) j++;
			printf("%d %d\n", i, j + n);
			deg1[i]--, deg2[j]--;
		}
	}
	for (int i = 1, j = 1; i <= n; i++) {
		if (!bl2[i]) continue;
		while (deg2[i]) {
			while (bl1[j] || !deg1[j]) j++;
			printf("%d %d\n", j, i + n);
			deg2[i]--, deg1[j]--;
		}
	}
	for (int i = 1, j = 1; i <= n; i++) {
		if (bl1[i]) continue;
		while (deg1[i]) {
			while (!deg2[j]) j++;
			printf("%d %d\n", i, j + n);
			deg1[i]--, deg2[j]--;
		}
	}
}

详细

小提示:点击横条可展开更详细的信息

Subtask #1:

score: 15
Accepted

Test #1:

score: 15
Accepted
time: 8ms
memory: 448532kb

input:

4 2 1 2
881932528 -768380602 216151177
198101493 -806366376 -445911009
-627799651 219211477 -8323008...

output:

YES
-3329276836
1 7
2 7

result:

ok OK. Correct Answer.

Test #2:

score: 0
Accepted
time: 35ms
memory: 448536kb

input:

4 4 2 3
-320416683 789659083 470095245 -613173378 766099160
516827294 -747501628 106722350 -71812850...

output:

YES
-4724159226
2 5
3 7
4 8
4 8

result:

ok OK. Correct Answer.

Test #3:

score: 0
Accepted
time: 44ms
memory: 448536kb

input:

4 4 3 3
-563376938 595886929 7892282 719645529 702105639
873063789 148913741 -879215160 -205270458 -...

output:

YES
-2972719785
2 5
3 6
4 7
2 7

result:

ok OK. Correct Answer.

Test #4:

score: 0
Accepted
time: 35ms
memory: 448532kb

input:

4 2 1 3
-896762217 -160700484 762787149
688862725 127673851 -589344726
-640132305 -407401441 2113555...

output:

YES
-3211938166
2 7
2 7

result:

ok OK. Correct Answer.

Test #5:

score: 0
Accepted
time: 44ms
memory: 448532kb

input:

4 2 1 3
-883355964 955863408 126351121
-591412814 612633262 -755218069
995754184 -775608127 -6171285...

output:

YES
-4791060883
3 5
3 6

result:

ok OK. Correct Answer.

Subtask #2:

score: 15
Accepted

Test #6:

score: 15
Accepted
time: 62ms
memory: 448536kb

input:

7 4 2 6
-96768558 120695853 -660162883 512927333 881065570
685314888 -743093703 299192918 69181691 3...

output:

YES
-6389642919
2 9
3 12
4 9
4 9

result:

ok OK. Correct Answer.

Test #7:

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

input:

7 2 3 7
-529562883 165116371 119468463
727831512 665226165 967930770
-793190645 214992668 694047746
...

output:

NO

result:

ok OK, correct answer.

Test #8:

score: 0
Accepted
time: 23ms
memory: 448540kb

input:

7 3 1 1
36307792 -445146766 330278252 -182174288
476717774 702688190 982294145 197524224
-372135390 ...

output:

YES
-3190936000
1 14
4 14
7 14

result:

ok OK. Correct Answer.

Test #9:

score: 0
Accepted
time: 57ms
memory: 448540kb

input:

7 6 6 7
821026907 -37395250 842711843 107532104 836520470 -595081231 430960037
893674542 384353172 -...

output:

YES
-2431116822
1 8
2 10
3 11
4 12
5 13
7 14

result:

ok OK. Correct Answer.

Test #10:

score: 0
Accepted
time: 31ms
memory: 448536kb

input:

7 5 1 6
-117951698 -44726312 89071183 800740173 953253241 784170276
417428233 679752659 -354527735 -...

output:

YES
-5965619681
3 8
2 10
7 14
2 14
3 9

result:

ok OK. Correct Answer.

Test #11:

score: 0
Accepted
time: 28ms
memory: 448536kb

input:

7 3 3 4
48 56 149 242
118 309 453 628
284 423 661 801
177 359 513 659
121 272 511 968
567 1092 1379 ...

output:

YES
6077
1 9
3 11
7 12

result:

ok OK. Correct Answer.

Test #12:

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

input:

7 4 6 6
51 83 85 176 265
162 264 318 503 642
109 281 448 746 904
103 400 782 1072 1151
73 175 630 64...

output:

NO

result:

ok OK, correct answer.

Test #13:

score: 0
Accepted
time: 23ms
memory: 448532kb

input:

7 6 1 3
77 147 173 270 360 367 391
15 93 177 357 439 490 682
30 287 367 447 533 658 775
332 395 541 ...

output:

YES
5853
1 8
1 9
1 9
1 9
1 9
1 10

result:

ok OK. Correct Answer.

Test #14:

score: 0
Accepted
time: 31ms
memory: 448532kb

input:

7 3 3 4
66 93 104 149
193 375 437 438
272 537 585 879
44 215 244 280
145 518 905 1084
411 644 1063 1...

output:

YES
6745
1 10
2 11
4 14

result:

ok OK. Correct Answer.

Test #15:

score: 0
Accepted
time: 59ms
memory: 448536kb

input:

7 6 2 4
42 132 218 278 334 338 340
93 241 346 418 583 640 710
13 145 219 443 555 802 917
357 563 710...

output:

YES
5939
1 8
7 13
1 8
1 8
1 9
1 11

result:

ok OK. Correct Answer.

Test #16:

score: 0
Accepted
time: 62ms
memory: 448536kb

input:

7 5 2 4
101 136 228 328 498 791
101 157 267 437 681 861
106 164 266 384 474 697
101 169 216 310 455 ...

output:

YES
1932
4 8
1 10
2 11
7 13
4 9

result:

ok OK. Correct Answer.

Test #17:

score: 0
Accepted
time: 44ms
memory: 448532kb

input:

7 7 3 7
107 145 279 401 536 829 1187 1487
109 168 312 492 757 915 1294 1632
109 168 308 488 696 817 ...

output:

YES
2249
1 8
2 11
3 12
5 13
6 14
7 12
7 13

result:

ok OK. Correct Answer.

Test #18:

score: 0
Accepted
time: 36ms
memory: 448536kb

input:

7 5 4 4
102 130 264 366 532 798
103 133 272 344 432 772
107 154 201 333 507 638
101 142 275 369 486 ...

output:

YES
1838
1 10
2 12
3 13
4 14
5 13

result:

ok OK. Correct Answer.

Test #19:

score: 0
Accepted
time: 24ms
memory: 448536kb

input:

7 6 4 6
107 166 260 444 706 887 1045
101 178 267 379 622 730 1125
108 154 241 361 528 666 917
101 16...

output:

YES
1993
4 8
1 10
3 12
6 13
7 14
4 9

result:

ok OK. Correct Answer.

Test #20:

score: 0
Accepted
time: 32ms
memory: 448532kb

input:

7 6 5 6
109 151 274 446 563 720 1018
104 143 237 311 505 708 884
107 158 263 435 673 834 1047
101 17...

output:

YES
2066
1 8
2 10
3 11
5 12
7 13
7 13

result:

ok OK. Correct Answer.

Subtask #3:

score: 30
Accepted

Test #21:

score: 30
Accepted
time: 28ms
memory: 448536kb

input:

2 20 2 2
-3 5 -1 -4 -9 3 -8 7 -6 9 -8 -4 -3 10 -8 -4 7 4 10 6 -9
-8 4 -7 -10 -6 1 -2 -3 9 4 9 6 6 8 ...

output:

YES
-26
1 3
2 4
1 3
1 3
1 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 4
2 4
2 4
2 4
2 4
2 4
2 4
2 4

result:

ok OK. Correct Answer.

Test #22:

score: 0
Accepted
time: 31ms
memory: 448536kb

input:

2 20 0 2
140 185 76 63 183 34 -397 35 47 177 139 48 106 142 122 88 115 59 184 164 194
150 174 7 94 1...

output:

YES
-1683
1 3
2 4
1 3
1 3
1 3
1 3
1 3
2 3
2 3
2 3
2 3
2 4
2 4
2 4
2 4
2 4
2 4
2 4
2 4
2 4

result:

ok OK. Correct Answer.

Test #23:

score: 0
Accepted
time: 80ms
memory: 448540kb

input:

5 12 1 1
0 12 12 93 177 252 288 477 693 855 945 1209 1209
3 3 51 60 60 75 75 75 291 399 519 717 1041...

output:

YES
878
1 8
1 8
2 8
2 8
2 8
2 8
2 8
2 8
2 8
4 8
4 8
4 8

result:

ok OK. Correct Answer.

Test #24:

score: 0
Accepted
time: 43ms
memory: 448532kb

input:

5 3 1 3
-356 63 42 197
50 -745 125 170
-939 68 24 31
83 -295 99 116
2 -281 7 94
168 199 52 188
-7 15...

output:

YES
-4059
2 6
4 8
5 6

result:

ok OK. Correct Answer.

Test #25:

score: 0
Accepted
time: 19ms
memory: 448532kb

input:

20 11 6 14
108 145 259 416 549 675 978 1365 1625 2216 2485 2966
101 127 265 477 631 966 1111 1328 18...

output:

YES
4896
1 21
2 22
4 23
7 24
12 25
13 27
15 28
16 31
18 33
19 37
20 39

result:

ok OK. Correct Answer.

Test #26:

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

input:

20 13 17 20
104 147 269 366 531 774 943 1121 1323 1763 2339 2702 3198 3538
100 132 181 267 507 838 1...

output:

NO

result:

ok OK, correct answer.

Test #27:

score: 0
Accepted
time: 40ms
memory: 448536kb

input:

20 7 6 9
109 176 225 392 613 960 1274 1519
105 168 225 386 525 768 910 1141
102 145 218 331 453 633 ...

output:

YES
4639
4 24
6 25
7 26
11 29
18 30
19 33
20 36

result:

ok OK. Correct Answer.

Test #28:

score: 0
Accepted
time: 19ms
memory: 448536kb

input:

20 6 4 11
106 183 301 457 608 928 1130
107 182 234 302 445 641 874
106 143 192 378 488 776 1132
105 ...

output:

YES
4564
3 22
5 23
6 32
7 35
10 36
19 37

result:

ok OK. Correct Answer.

Subtask #4:

score: 10
Accepted

Test #29:

score: 10
Accepted
time: 56ms
memory: 448536kb

input:

30 14 14 17
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...

output:

YES
60
1 31
2 32
3 33
4 34
5 35
6 36
7 37
8 38
9 39
10 40
11 41
12 42
13 43
14 44

result:

ok OK. Correct Answer.

Test #30:

score: 0
Accepted
time: 51ms
memory: 448536kb

input:

30 27 5 7
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...

output:

YES
60
1 31
2 32
3 33
4 34
5 35
1 31
1 31
1 31
1 31
1 31
1 31
1 31
1 31
1 31
1 31
1 31
1 31
1 31
1 3...

result:

ok OK. Correct Answer.

Subtask #5:

score: 30
Accepted

Test #31:

score: 30
Accepted
time: 48ms
memory: 448536kb

input:

30 21 15 19
-254491436 -483856050 206213527 -41748829 436780835 -456533898 -566210793 411625129 3765...

output:

YES
-25064372072
1 31
2 33
3 34
6 39
10 45
11 46
12 47
13 48
15 50
17 51
19 52
20 53
24 56
29 57
30 ...

result:

ok OK. Correct Answer.

Test #32:

score: 0
Accepted
time: 48ms
memory: 448544kb

input:

30 29 13 23
105 136 100 178 125 156 320 262 338 540 412 747 1109 1091 833 687 789 607 1129 1439 1772...

output:

YES
4973
4 31
7 33
10 35
14 38
15 41
16 42
17 46
23 47
24 48
26 53
27 54
29 55
30 58
24 35
29 41
29 ...

result:

ok OK. Correct Answer.

Test #33:

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

input:

30 25 26 28
106 105 161 104 192 116 98 10 -70 -70 214 513 814 776 1129 1150 923 750 1012 939 1417 11...

output:

NO

result:

ok OK, correct answer.

Test #34:

score: 0
Accepted
time: 52ms
memory: 448540kb

input:

30 21 5 19
106 96 88 142 121 137 29 175 314 504 673 905 1031 1225 1302 1243 1418 1266 926 1224 918 1...

output:

YES
4834
1 35
3 37
7 45
12 49
30 59
7 35
7 35
7 37
7 37
7 37
7 37
7 37
7 37
7 37
7 37
7 37
7 37
7 49...

result:

ok OK. Correct Answer.

Test #35:

score: 0
Accepted
time: 44ms
memory: 448532kb

input:

30 27 9 29
107 147 264 336 419 617 988 1147 1505 2005 2590 3008 3768 4543 5330 6292 6665 7166 7996 9...

output:

YES
8479
1 31
2 32
4 34
5 35
7 36
8 37
9 40
10 41
12 42
13 43
15 44
16 45
17 46
18 47
19 48
21 49
22...

result:

ok OK. Correct Answer.

Test #36:

score: 0
Accepted
time: 35ms
memory: 448532kb

input:

30 21 6 26
106 142 250 442 542 824 1035 1346 1634 2266 2489 2992 3428 4177 4900 5828 6441 7328 7937 ...

output:

YES
8125
1 31
2 32
4 35
5 37
6 38
9 39
10 40
11 41
12 42
14 43
15 44
18 45
19 49
20 51
23 55
24 56
2...

result:

ok OK. Correct Answer.

Test #37:

score: 0
Accepted
time: 89ms
memory: 448536kb

input:

30 29 19 29
105 155 215 394 499 780 1141 1346 1816 2182 2391 3063 3642 4164 4456 5307 5956 7044 7592...

output:

YES
8991
1 31
2 32
4 33
5 34
7 38
8 39
9 40
10 41
11 42
12 43
13 44
14 45
15 46
17 48
18 49
19 51
21...

result:

ok OK. Correct Answer.

Test #38:

score: 0
Accepted
time: 87ms
memory: 448540kb

input:

30 28 16 27
-88874271 -839508192 60937837 -815839639 844392859 -701378073 -564840845 -430075583 7543...

output:

YES
-31070003899
6 32
1 31
2 34
5 35
8 38
13 39
15 40
16 41
17 44
18 47
22 49
23 51
24 54
25 55
28 5...

result:

ok OK. Correct Answer.

Test #39:

score: 0
Accepted
time: 62ms
memory: 448540kb

input:

30 21 2 23
-595636977 42194597 -743201027 677936386 -320905193 28103221 -359376648 503790430 -229527...

output:

YES
-25338652882
19 33
2 41
10 42
12 43
14 47
17 48
18 49
20 50
22 53
23 54
26 55
29 59
10 41
12 42
...

result:

ok OK. Correct Answer.

Test #40:

score: 0
Accepted
time: 71ms
memory: 448540kb

input:

30 30 6 18
-649127874 680925978 719860653 -138058399 -606169291 134602490 -808791136 -413952763 -353...

output:

YES
-34863056520
4 31
6 37
8 38
10 39
11 40
12 43
13 47
14 49
15 51
17 57
19 58
21 59
24 60
8 37
8 3...

result:

ok OK. Correct Answer.

Extra Test:

score: 0
Extra Test Passed