解题思路:
把环拆成2*n的序列,不断减去左边界,加上右边界更新即可。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<queue>
#include<vector>
#define ll long long
using namespace std;
int getint()
{
int i=0,f=1;char c;
for(c=getchar();(c<'0'||c>'9')&&c!='-';c=getchar());
if(c=='-')c=getchar(),f=-1;
for(;c>='0'&&c<='9';c=getchar())i=(i<<3)+(i<<1)+c-'0';
return i*f;
}
const int N=200005;
int T,n,ans;
int a[N],b[N];
ll sum;
int main()
{
//freopen("lx.in","r",stdin);
int i,j;
while(scanf("%d",&n)!=EOF)
{
ans=sum=0;
for(i=1;i<=n;i++)
a[i]=a[i+n]=getint(),b[i]=b[i+n]=getint();
j=1;
for(i=1;i<=n;i++)
{
sum-=(a[i-1]-b[i-1]);
while(sum+a[j]-b[j]>=0&&j<i+n)
sum+=a[j]-b[j],j++;
ans=max(ans,j-i);
}
cout<<ans<<'\n';
}
}