1422 环形dp

就是将序列拓展之后dp,来达到环形的效果

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#define MAX 100007

using namespace std;

int n;
int a,b;
int v[MAX<<1];

int main ( )
{
    while ( ~scanf ( "%d" , &n ) )
    {
       int sum = 0;
       int temp = 0;
       int ans = 0;
       for ( int i = 1 ; i <= n ; i++ )
       {
            scanf ( "%d%d" ,&a , &b );
            v[i] = v[i+n] = a - b;
           // cout << v[i] << " ";
       } 
       for ( int i = 1 ; i <= 2*n ; i++ )
       {
           sum += v[i];
           if ( sum >= 0 ) temp++;
           ans = max ( ans , temp );
           if ( sum < 0 )
                temp = sum = 0;
       }
       printf ( "%d\n" , min ( ans , n ) );
    }
}


你可能感兴趣的:(C++,动态规划,环形dp)