UOJ Logo

NOI.AC

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#203341#2780. Carrytkswls10049ms1724kbC++112.5kb2024-02-23 08:25:582024-02-23 12:07:57

answer

#include <bits/stdc++.h>
#pragma GCC optimize(2)
#define int long long
using namespace std;
int n, m;
int t, a[100005], b[100005], c[5], d[100005];
inline bool check(int k) {
	int p = 0, q = 0, ansa = 0, ansb = 0, cnt = 0;
	c[1] = c[2] = c[3] = 0;
	for (int i = 1; i <= n; i++) {
		c[a[i]] += k / n + ((k % n) >= i);
	}
	for (int i = 1; i <= m; i++) {
		d[i] = b[i];
	}
	int op;
	//	for (int i = 1; i <= n; i++) {
	//		op = min(c[3] / 2, d[i] / 6);
	//		d[i] -= op * 6, c[3] -= op * 2;
	//		q += op * 2;
	//	}
	//	for (int i = 1; i <= n; i++) {
	//		if (d[i] >= 3 && c[3]) {
	//			d[i] -= 3, c[3]--;
	//			p++;
	//		}
	//	}
	//	for (int i = 1; i <= n; i++) {
	//		if (d[i] >= 2 && c[3]) {
	//			d[i] -= 2, c[3]--, p++;
	//		}
	//	}
	//	for (int i = 1; i <= n; i++) {
	//		if (d[i] >= 1 && c[3]) {
	//			d[i] -= 1, c[3]--, p++;
	//		}
	//	}
	//	for (int i = 1; i <= n; i++) {
	//		if (d[i]) {
	//			if (d[i] % 2 == 0) {
	//				ansb += d[i] / 2;
	//			} else {
	//				ansb += d[i] / 2, ansa++;
	//			}
	//		}
	//	}
	//	c[2] -= ansb, c[1] -= ansa;
	//	if (c[2] * 2 + c[1] < 0) return false;
	//	if (c[2] < 0) return true;
	//	if (c[1] < 0) {
	//		if (c[2] >= c[1]) return true;
	//
	//	}
//	cout << c[1] << " " << c[2] << ' ' << c[3] << "\n";
	for (int i = 1; i <= m; i++) {
		if (d[i] & 1) {
			if (c[3] && d[i] >= 3) {
				c[3]--, d[i] -= 3;
			} else if (c[1]) {
				c[1]--, d[i] -= 1;
			}
		}
//		cout << d[i] << "-";
	}
//	cout << "\n" << c[1] << " " << c[2] << ' ' << c[3] << "\n";
	for (int i = 1; i <= m; i++) {
		op = min(c[3] / 2, d[i] / 6);
		d[i] -= op * 6, c[3] -= op * 2;
	}
	for (int i = 1; i <= m; i++) {
		if (d[i] & 1) {
			c[2] -= (d[i] + 1) / 2;
			d[i] = 0;
		} else {
			int op = min((min(c[1], c[3])), d[i] / 4);
			c[1] -= op, c[3] -= op;
			d[i] -= 4 * op;
			c[2] -= d[i] / 2, d[i] = 0;
		}
	}
	for (int i = 1; i <= m; i++) {
		if (d[i]) return false;
	}
	c[2] += c[3] + c[1] / 2;
	return (c[2] >= 0);
}
signed main() {
//	freopen("ex_farmer2.in", "r", stdin);
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin >> t;
	while (t--) {
		cin >> n;
		for (int i = 1; i <= n; i++) {
			cin >> a[i];
		}
		cin >> m;
		for (int i = 1; i <= m; i++) {
			cin >> b[i];
		}
//		check(3);
		int l = 0, r = 100000000000000, mid;
		while (l < r) {
			mid = (l + r) >> 1;
//			cout << mid << '!' << check(mid) << "[]\n";
			if (check(mid)) r = mid;
			else l = mid + 1;
		}
		cout << l << "\n";
	}


}

Details

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

Test #1:

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

input:

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

output:

4
3

result:

ok 2 lines

Test #2:

score: 10
Accepted
time: 0ms
memory: 1264kb

input:

100
21
1 3 3 3 2 3 3 2 2 1 3 2 1 3 2 1 1 1 3 3 3
3
3 3 1
19
1 3 1 1 3 3 1 3 2 3 2 2 3 3 1 1 2 2 2
10...

output:

3
15
3
7
19
12
3
8
7
20
5
10
6
10
3
10
16
1
5
6
10
14
13
8
8
5
13
15
5
10
16
14
10
1
10
4
3
16
5
4
7...

result:

ok 100 lines

Test #3:

score: 20
Accepted
time: 9ms
memory: 1268kb

input:

100
1000
2 3 3 3 1 3 3 1 2 1 1 2 2 2 1 1 1 2 1 3 2 3 1 3 2 1 1 3 2 2 3 2 1 3 3 2 3 3 3 1 1 1 2 3 2 1...

output:

308619748
915114183
400399521
396849898
34408909
355254042
762251542
10231210
308559696
416902736
31...

result:

ok 100 lines

Test #4:

score: 20
Accepted
time: 28ms
memory: 1284kb

input:

100
773
2 3 3 2 1 1 1 3 1 3 2 3 2 2 2 2 1 3 3 3 2 3 3 2 2 3 2 1 1 1 3 2 1 1 2 1 2 3 3 3 1 1 3 2 1 3 ...

output:

141465623985
146640177725
193302027436
185725363449
27377351959
26962096046
122965020242
16457554942...

result:

ok 100 lines

Test #5:

score: 20
Accepted
time: 5ms
memory: 1612kb

input:

1
45105
3 1 2 1 1 1 1 2 1 3 3 2 1 1 1 3 3 2 2 1 2 1 2 1 3 1 1 3 3 1 3 1 3 3 2 2 1 1 1 1 1 2 2 2 3 3 ...

output:

52702719862

result:

ok single line: '52702719862'

Test #6:

score: 20
Accepted
time: 7ms
memory: 1724kb

input:

1
58785
3 2 2 2 2 2 1 1 1 2 1 2 1 1 3 2 1 2 1 2 2 1 1 3 1 3 2 2 2 2 2 2 2 1 1 2 2 3 3 3 2 1 2 3 3 1 ...

output:

203100926

result:

ok single line: '203100926'

Test #7:

score: 10
Accepted
time: 0ms
memory: 1256kb

input:

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

output:

12
7
12
15
5
3
17
12
8
2

result:

ok 10 lines

Extra Test:

score: 0
Extra Test Passed