题意:给你一个序列,每一个有两个值,一个代表分数,一个代表惩罚值,你每次只能选择最前面的一个,如果你当前的分数小于惩罚值就停止选择,你能在游戏的开始前将最前面的移到最后面任意次,问你的到最大分数是移动的次数。
思路:这个题,emmmm其实一开始就写对了,但是因为数组开小了给我了tle结果我就懵逼了,写的O(n)时间复杂度的代码超时,后来评测机炸了也没再好好检查这个题目(还是太菜了),思路很简单就是一个和尺取差不多的思想。
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define MAXN 2000010
#define MAXE 5
#define INF 1000000000
#define MOD 998244353
#define LL long long
#define pi acos(-1.0)
using namespace std;
int arr[MAXN], brr[MAXN];
int read() {
int x = 0;
char ch = getchar();
while ('0' > ch || ch > '9')
ch = getchar();
while ('0' <= ch && ch <= '9') {
x = (x << 3) + (x << 1) + ch - '0';
ch = getchar();
}
return x;
}
int main() {
//std::ios::sync_with_stdio(false);
int n;
while (~scanf("%d", &n)) {
int max_sum = 0;
int sum = 0;
int x = 0;
int cnt = 0;
for (int i = 1; i <= n; ++i) {
arr[i] = read();
arr[i + n] = arr[i];
sum += arr[i];
}
for (int i = 1; i <= n; ++i) {
brr[i] = read();
brr[i + n] = brr[i];
}
int pos = 1;
while (true) {
int Num = 0;
int ans = 0;
bool flag = false;
for (int i = pos; i < pos + n; ++i) {
ans += arr[i];
Num += arr[i] - brr[i];
if (Num < 0) {
flag = true;
if (ans > max_sum || (sum == max_sum && x > pos - 1)) {
max_sum = ans;
x = pos - 1;
}
pos = i + 1;
break;
}
}
if (!flag) {
if (ans == max_sum && x > pos - 1) {
x = pos - 1;
} else if (ans > max_sum) {
max_sum = ans;
x = pos - 1;
}
break;
}
if (pos > n) {
break;
}
if (max_sum == sum) {
break;
}
}
printf("%d\n", x);
}
return 0;
}