CodeForces 1983C Have Your Cake and Eat It Too
题目链接:CodeForces 1983C【Have Your Cake and Eat It Too】
思路
先向上取整计算出tot/3,然后依次枚举abc三个数组取区间的前后顺序,对于每个顺序依次从前往后枚举,直到取得的区间数字之和大于等于tot/3,就对下一个数组进行枚举,直到所有数组都满足取出的区间数字之和大于等于 tot/3。
代码
#include <bits/stdc++.h>
#include <vector>
using namespace std;
#define ll long long
const int N = 2e5 + 10;
int a[5][N];
void solve() {
ll n, sum = 0;
cin >> n;
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= n; j++) {
cin >> a[i][j];
}
}
for (int i = 1; i <= n; i++) {
sum += a[1][i];
}
ll need = (sum + 2) / 3;
vector<int> p{1, 2, 3};
do {
vector<int> l(4), r(4);
ll s = 0, i = 1;
bool ok = true;
for (auto t : p) {
int j = i;
while (j <= n && s < need) {
s += a[t][j++];
}
ok &= (s >= need);
l[t] = i;
r[t] = j - 1;
i = j;
s = 0;
}
if (!ok) {
continue;
}
for (int i = 1; i <= 3; i++) {
cout << l[i] << ' ' << r[i] << ' ';
}
cout << endl;
return;
} while (next_permutation(p.begin(), p.end()));
cout << "-1" << endl;
}
int main() {
int t;
cin >> t;
while (t--) {
solve();
}
return 0;
}

浙公网安备 33010602011771号