HDOJ-1422 重温世界杯

环状线性dp

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <vector>
#include <cstdio>
#include <queue>
#define INF 1e9
#define maxn 200005 

using namespace std;

int num[maxn];
int dp[maxn];
int main(){

// freopen("in.txt", "r", stdin);
    int n;

    while(cin >> n){

        int a, b;
        for(int i = 0; i < n; i++){

            scanf("%d%d", &a, &b);
            num[i] = a - b;
            num[i+n] = a - b;
        }
        int maxs = 0, sign = 0;
        for(int i = 0; i < 2 * n; i++){
            if(sign && (num[i] += num[i-1]) >= 0){
                dp[i] = dp[i-1] + 1;
                maxs = max(maxs, dp[i]); 
            }
            else{
                if(num[i] >= 0){
                  dp[i] = 1;
                  sign = 1;
                  maxs = max(maxs, dp[i]);
                }
                else{
                  sign = 0;
                }
            }
            if(maxs >= n)
              break;
        }
        cout << maxs << endl;
    }
    return 0;
}

你可能感兴趣的:(HDOJ-1422 重温世界杯)