解法:不断找起点,由于如果1 2 3这些点是一次遍历的,然后到了第4个点他们的和小于0而退出,那么从2开始再次走肯定没有第一次优,所以可以采用尺取的办法,建立一个环,走一遍即可。
代码如下:
#include<iostream>
#include<cstdio>
#include<vector>
#include<queue>
#include<utility>
#include<stack>
#include<algorithm>
#include<cstring>
#include<string>
#include<cmath>
#include<set>
#include<map>
using namespace std;
const int maxn = 1e6 + 5;
int n;
int a[maxn<<1], b[maxn<<1], c[maxn<<1];
#define FI(n) FastIO::read(n)
namespace FastIO {
const int SIZE = 1 << 16;
char buf[SIZE], obuf[SIZE], str[60];
int bi = SIZE, bn = SIZE, opt;
int read(char *s) {
while (bn) {
for (; bi < bn && buf[bi] <= ' '; bi++);
if (bi < bn) break;
bn = fread(buf, 1, SIZE, stdin);
bi = 0;
}
int sn = 0;
while (bn) {
for (; bi < bn && buf[bi] > ' '; bi++) s[sn++] = buf[bi];
if (bi < bn) break;
bn = fread(buf, 1, SIZE, stdin);
bi = 0;
}
s[sn] = 0;
return sn;
}
bool read(int& x) {
int n = read(str), bf;
if (!n) return 0;
int i = 0; if (str[i] == '-') bf = -1, i++; else bf = 1;
for (x = 0; i < n; i++) x = x * 10 + str[i] - '0';
if (bf < 0) x = -x;
return 1;
}
};
int main() {
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
#endif
int n;
while(FI(n)) {
int len = n + n;
for(int i = 1; i <= n; i++)
FI(a[i]);
for(int i = 1; i <= n; i++) {
FI(b[i]);
c[i] = a[i] - b[i];
}
for(int i = n + 1; i < len; i++) {
a[i] = a[i - n];
c[i] = c[i - n];
}
int Max = 0, ans = 0;
for(int i = 1, j; i <= n; i++) {
if(c[i] < 0)
continue;
int tmp = c[i], cnt = a[i];
len = i + n;
for(j = i + 1; j < len; j++) {
if(tmp + c[j] < 0)
break;
tmp += c[j];
cnt += a[j];
}
if(cnt > Max) {
Max = cnt;
ans = i - 1;
}
i = j - 1;
}
printf("%d\n", ans);
}
return 0;
}