感谢 http://blog.csdn.net/txgang/article/details/77568491
以下供参考
getchar读入法 2683MS
FastIO法 MX=1e2 561MS
MX=1e3 436MS
MX=1e4 499MS
MX=1e5 452MS
MX=1e6 483MS
MX=1e7 499MS
#include<bits/stdc++.h> using namespace std; typedef long long LL; namespace FastIO { //使用时直接调用 read(x)即可, const static int MX=1e6; bool IOerror=0; char nc() { static char buf[MX],*p1=buf+MX,*pend=buf+MX; if(p1==pend) { p1=buf; pend=buf+fread(buf,1,MX,stdin); if(pend==p1) { IOerror=1; return -1; } } return *p1++; } inline bool blank(char ch) { return ch==' '||ch=='\n'||ch =='\r'||ch =='\t'; } inline int read(int& x) { char ch; int sign=1; while(blank(ch=nc())); if(IOerror) return 0; if(ch=='-') sign=-1,ch=nc(); for(x=ch-'0'; (ch=nc())>='0'&&ch<='9'; x=x*10+ch-'0'); x*=sign; return 1; } inline int read(LL& x) { char ch; int sign=1; while(blank(ch=nc())); if(IOerror) return 0; if(ch=='-') sign=-1,ch=nc(); for(x=ch-'0'; (ch=nc())>='0'&&ch<='9'; x=x*10+ch-'0'); x*=sign; return 1; } } using namespace FastIO; const int N=2e6+9; int a[N],b[N]; int solve(int n) { for(int i=0;i<n;i++) read(a[i]),a[i+n]=a[i]; for(int i=0;i<n;i++) read(b[i]),b[i+n]=b[i]; int ret=0,fuck=0; int sum1=0,sum2=0,be=0; for(int i=0;i<2*n&&be<n;i++) { sum1+=a[i]; sum2+=a[i]-b[i]; if(sum2<0||i-be+1==n) { if(sum1>fuck) ret=be,fuck=sum1; sum1=sum2=0; be=i+1; } } return ret; } int main() { // freopen("test.in","r",stdin); int n; while(read(n)) printf("%d\n",solve(n)); }