[HDU1422] Relive the World Cup (simple dynamic programming)

Relive the World Cup

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)

Problem Description

The World Cup is over, and the Italians even recovered the debt owed to them by the French 6 years ago, won the Hercules Cup and achieved 4-star Italy.
Although the World Cup is over, this World Cup still leaves us with Many things worth remembering. For example, we heard Huang Mingzui’s 3-minute passionate commentary. We learned that we can show 3 yellow cards to the same person. We also saw that Zidane’s head can not only top the ball but also top people... ………
Because there are so many wonderful things, xhd decided to revisit the World Cup in Germany, of course, just to go to the cities hosting the World Cup. But this requires a lot of money, fortunately xhd’s love for the World Cup Impressed by the German World Cup Organizing Committee, they will provide xhd with round-trip air tickets to Hangzhou, China and any World Cup hosting city in Germany, and persuaded these cities to provide him with a living allowance when xhd arrives in the city so that he can use it when visiting there. , When the tour is finished, the remaining money will also be reserved for xhd, but when the living expenses are not enough, they will forcibly end the xhd trip to Germany, in addition to this, they have one condition, xhd can only visit according to the route they gave For example, there are 3 cities a, b, and c, and they have given the route of abc, then xhd has only 3 visit orders abc, bca, cab. Since the living expenses provided by each city and the cost there are different, this makes Xhd is a headache. Fortunately, we knew the living expenses and expenses in advance. How many cities can Xhd visit smoothly?

Input

Each set of input data is divided into two lines. The first line is a positive integer n (1<=n<=100000), indicating that there are n cities. The next line outputs the living expenses of these n cities according to the given route order And the cost, w1, l1, w2, l2,..., wn, ln, where wi, li represent the living expenses and expenses of the i-th city, and they are all positive integers.

Output

The maximum number of cities that can be visited corresponding to each set of data output.

Sample Input

3
3 2 3 4 2 2
3
3 2 3 4 2 3

Sample Output

3
2

When I did this question at the beginning, I didn't look at the meaning of the question carefully, so I started TIE. I learned later that it’s a loop-like thing and just follow the method we did before.

#include "iostream"
#include "cstdio"
#include "cstring"
#include "algorithm"

using namespace std;

const int maxn = 2e5+5;

int n;
int w,l;
int dp[maxn];
int sum;

int main(){
    while(~scanf("%d",&n)){
        for( int i=0 ; i<n ; i++ ){
            scanf("%d%d",&w,&l);
            dp[i]=dp[i+n]=w-l;
        }
        sum = 0;
        int k = 0;
        int max = 0;
        for( int i=0 ; i<2*n ; i++ ){
            sum += dp[i];
            k++;
            if(sum < 0){
                sum = 0;
                k = 0;
            }
            if( k > max ) max = k;
            if( k > n ) break;
        }
        if(max > n) max = n;
        cout<<max<<endl;
    }
    return 0;
}

Guess you like

Origin blog.csdn.net/thesprit/article/details/52026937