思维
把数组扩大一倍,然后找连续的大于0的最长的一段即可,注意结尾特判一下。
#include#define INF 0x3f3f3f3f#define full(a, b) memset(a, b, sizeof a)#define FAST_IO ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)using namespace std;typedef long long ll;inline int lowbit(int x){ return x & (-x); }inline int read(){ int ret = 0, w = 0; char ch = 0; while(!isdigit(ch)) { w |= ch == '-'; ch = getchar(); } while(isdigit(ch)) ret = (ret << 3) + (ret << 1) + (ch ^ 48), ch = getchar(); return w ? -ret : ret;}inline int gcd(int a, int b){ return b ? gcd(b, a % b) : a; }inline int lcm(int a, int b){ return a / gcd(a, b) * b; }template inline T max(T x, T y, T z){ return max(max(x, y), z); }template inline T min(T x, T y, T z){ return min(min(x, y), z); }template inline A fpow(A x, B p, C lyd){ A ans = 1; for(; p; p >>= 1, x = 1LL * x * x % lyd)if(p & 1)ans = 1LL * x * ans % lyd; return ans;}const int N = 2000005;int a[N], b[N], c[N], n;int main(){ while(~scanf("%d", &n)){ for(int i = 1; i <= n; i ++) a[i] = read(); for(int i = 1; i <= n; i ++) b[i] = read(); for(int i = 1; i <= n; i ++) c[i] = a[i] - b[i]; for(int i = n + 1; i <= 2 * n; i ++) c[i] = a[i - n] - b[i - n]; int sum = 0, index = -1, maxi = -INF, ans = -1, len = 0; for(int i = 1; i <= 2 * n; i ++){ if(index == -1 && c[i] < 0) continue; else if(index == -1) sum += c[i], index = i, len ++; else if(sum + c[i] >= 0){ sum += c[i], len ++; if(i == 2 * n){ if(len > maxi) maxi = len, ans = index; } } else if(sum + c[i] < 0){ if(len > maxi) maxi = len, ans = index; sum = 0, len = 0, index = -1; } if(maxi == n) break; } printf("%d\n", ans - 1); } return 0;}