hdu 1422(环上最长连续子序列)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1422

 1 #include<iostream>

 2 #include<cstdio>

 3 #include<cstring>

 4 #include<algorithm>

 5 using namespace std;

 6 #define MAXN 222222

 7 int level[MAXN];

 8 int dp[MAXN];

 9 int n,w,l;

10 

11 int main(){

12     while(~scanf("%d",&n)){

13         int st=0,ans=0,now=0;

14         memset(dp,0,sizeof(dp));

15         for(int i=1;i<=n;i++){

16             scanf("%d%d",&w,&l);

17             level[i]=level[i+n]=w-l;

18         }

19         for(int i=1;i<=2*n;i++){

20             if(now+level[i]>=0){

21                 now+=level[i];

22                 dp[i]=dp[i-1]+1;

23                 //最多n个城市

24                 if(i-st==n){

25                     ans=max(ans,dp[i]);

26                     break;

27                 }

28             }else {

29                 now=0,st=i;//注意这里st应该改为i.

30                 dp[i]=0;

31                 ans=max(ans,dp[i-1]);

32             }

33         }

34         printf("%d\n",ans);

35     }

36     return 0;

37 }
View Code

 

你可能感兴趣的:(HDU)