跳到正文

题解

CF1983C 题解

Vitamin_B约 3 分钟
原文
文章信息与版本

文章信息

发布
2024年7月8日
收录
2025年12月12日
更新
2026年11月21日
篇幅
约 3 分钟
获赞
2
题目
CF1983C

版本

  1. v1当前

思路

先简化一下问题,保证 aa 先,bb 中间段,cc 最后,看看怎么做。因为所有数都是正数,所以我们不会浪费任何一个数,la=1,lb=ra+1,lc=rb+1,rc=nl_a=1,l_b=r_a+1,l_c=r_b+1,r_c=n。因为这一段数是连续的,所以我们可以用前缀和优化。rar_a 贪心取,可以用二分求出最小的 rar_a 使得 sara≥tot3sa_{r_a}\ge\frac{tot}3。再用二分法求出最小的 rbr_b 使得 sbrb≥tot3−sbrasb_{r_b}\ge\frac{tot}3-sb_{r_a}。最后看看是否合法即可。

但是,这题不一定是 a,b,ca,b,c 的顺序,怎么办?枚举大法,启动!

代码

C++
# include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair <int, int> pii;
int t, n, l[5], r[5], id[5];
ll a[200005], b[200005], c[200005], tot;
bool f (ll a[], ll b[], ll c[], int id1, int id2, int id3) {
	l[1] = 1, r[1] = lower_bound (a + 1, a + 1 + n, tot) - a;
	if (r[1] >= n) //第一段就用光了?
		return 0;
	l[2] = r[1] + 1, r[2] = lower_bound (b + l[2], b + 1 + n, tot + b[r[1]]) - b;
	if (r[2] >= n) //第三段没位置辣
		return 0;
	if (c[n] - c[r[2]] < tot) //最后一段不达标
		return 0;
	l[3] = r[2] + 1, r[3] = n;
	id[id1] = 1, id[id2] = 2, id[id3] = 3; //按原顺序存数
	cout << l[id[1]] << ' ' << r[id[1]] << ' ' << l[id[2]] << ' ' << r[id[2]] << ' ' << l[id[3]] << ' ' << r[id[3]] << '\n'; //输出
	return 1;
}
int main () {
	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], a[i] += a[i - 1];
		for (int i = 1; i <= n; ++ i)
			cin >> b[i], b[i] += b[i - 1];
		for (int i = 1; i <= n; ++ i)
			cin >> c[i], c[i] += c[i - 1];
		tot = (a[n] + 2) / 3;
		if (f (a, b, c, 1, 2, 3) || f (a, c, b, 1, 3, 2) || f (b, a, c, 2, 1, 3) || f (b, c, a, 2, 3, 1) || f (c, a, b, 3, 1, 2) || f (c, b, a, 3, 2, 1)) //逻辑或会自动“短路”,不用担心会输出多种情况。
			continue ;
		cout << "-1\n";
	}
	return 0;
}
原文
存档评论1 条抓取于