刚开始没注意访问顺序 用的贪心 WA了 后来仔细读题
环形序列的最长非负连续子序列。显然当前剩余为 非负时dp[i]=dp[i-1]+1;否则,dp[i]=0;动态地记录已经获得的最优解。
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <stack>
#include <queue>
using namespace std;
int a[200200];
int dp[200200];
int main ()
{
int n;
int p,pp;
while (scanf ("%d",&n)!=EOF)
{
for (int i =1 ;i<=n;i++)
{
scanf ("%d %d",&p,&pp);
a[i]=a[i+n]=p-pp;//预处理
}
int sum=0;//当前剩余钱
int s = 0 ;//出发的起点
int ans = -1;//记录最优解
for (int i =1 ;i<=n*2;i++)
{
if (sum + a[i] >= 0)
{
dp[i]=dp[i-1]+1;
sum += a[i];
if (i - s == n)
{
if (ans < dp[i])
ans = dp[i];
break;
}
}
else
{
s=i//更新起点和当前剩余钱数
sum=0;
dp[i]=0;
ans = max (ans ,dp[i-1]);//更新记录最大值
}
}
printf("%d\n",ans);
}
return 0;
}