回旋加速器(2021 CoE-II C)
metaphysis · · 题解
朴素的做法就是从每一个加速腔开始,遍历整个环形管道,然后找出最后剩余动能最大的加速腔,这是
如果认真加以思考,本题与求最大连续子数组和相似。在任何一个加速腔,我们只关心动能的损耗,定义
那么这道题目包含两个问题:(1)能否在环上绕一圈?(2)如果能,这个加速腔在那里?
第一个问题很简单,对
对于第二个问题,起始加速腔在哪里?假设我们从环上取一个区间
如果
参考代码:
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e6 + 10;
int n, ei[MAXN], di[MAXN], diff[MAXN];
int main(int argc, char *argv[])
{
cin.tie(0), cout.tie(0), ios::sync_with_stdio(false);
int cases;
cin >> cases;
for (int cs = 1; cs <= cases; cs++)
{
cin >> n;
for (int i = 0; i < n; i++) cin >> ei[i];
for (int i = 0; i < n; i++) cin >> di[i];
for (int i = 0; i < n; i++) diff[i] = ei[i] - di[i];
int energy = 0, sum = 0, idx = 0;
for (int i = 0; i < n; i++)
{
energy += diff[i];
sum += diff[i];
if (sum < 0)
{
idx = i + 1;
sum = 0;
}
}
if (energy < 0) cout << "Failed!\n";
else cout << (idx + 1) << '\n';
}
return 0;
}