relive the World Cup
problem Descriptionat the end of the World Cup, the Italians brought back the French debt owed to them 6 years ago and took up the cup of the Hercules, making 4-star Italy.
The World Cup is over, but the World Cup has left us with a lot to remember. For example, we heard the yellow name of the mouth of the 3-minute passionate commentary, we learned that the original can be presented to the same person 3 yellow cards, we also saw Zidane's head can not only ball can also top people ...
There are so many wonderful, Xhd decided to revisit the German World Cup, of course, just go to the host World Cup tournament to take a look at the city. But it takes a lot of money, and fortunately Xhd's love for the World Cup has moved the World Cup organizing Committee, They will provide a round-trip ticket to the city of Xhd in China, Hangzhou and Germany, and persuade the cities to provide him with a living allowance when Xhd arrives in the city, so that when he visits there, the rest of the money will be left to XHD, But when the cost of living is not enough, they will force the end of XHD's trip to Germany, in addition to this, they have a condition, XHD only according to their route to visit. For example, there are 3 cities a,b,c, they given a-b-c-a route, then XHD only 3 kinds of tour order ABC,BCA, Cab. Because the cost of living and spending there are different in each city, it makes XHD very headache, fortunately we know the cost of living and expenses in advance. How many cities can xhd visit?
Inputeach set of input data is divided into two lines, and the first line is a positive integer n (1<=n<=100000), indicating that there are N cities. The next line is the output of the N cities in accordance with the given route order. The cost of living and spending, W1,l1,w2,l2,......, Wn,ln, of which WI, Li represents the cost of living and spending for the first city, and they are all positive integers.
OutputThe maximum number of cities that can be visited for each set of data output.
Sample Input2 3 4 2 233 2 3 4 2 3
Sample Output3 2for every city, v[i]= living expenses-costThis problem becomes, give a sequence, find a sub-sequence, V[l]......v[r]The >=0 of the v[l]......v[r of any subsequence that begins with V[l] .and R-l+1 MaxThis sequence can also be returned to the beginning of the 1th at the end.It's like a ring, so how do you deal with it? one way to do this is to take the first n-1 elements of the sequence to the back,For Example: 12345, it translates to: 123451234that would be equivalent.Set Dp[i] indicates the maximum value of r-l+1 in the subsequence of the last element, i.e. the number of cities that can travel up to then simply sweep through the past, then Sum+=v[i]if Sum<0 then sum=0,dp[i]=0, continueElse dp[i]=dp[i-1]+1 then find the maximum value of dp[i],Note: Because we have a chain of rings, so if Max_dp>n is Max_dp=n
1#include <cstdio>2#include <cstring>3#include <algorithm>4 5 using namespacestd;6 7 Const intmaxn=100005;8 9 intdp[2*MAXN];Ten intv[2*MAXN]; One A intMain () - { - intN; the - while(SCANF ("%d", &n)! =EOF) - { - intVal,cost; + - for(intI=1; i<=n;i++) + { Ascanf"%d%d",&val,&Cost ); atv[i]=val-Cost ; - } - - for(inti=n+1;i<2*n;i++) -v[i]=v[i-n]; - indp[0]=0; - to intsum=0; + - for(intI=1;i<2*n;i++) the { *sum+=V[i]; $ if(sum<0)Panax Notoginseng { -sum=0; thedp[i]=0; + Continue; A } thedp[i]=dp[i-1]+1; + } - $ intans=0; $ - for(intI=1;i<2*n;i++) - if(dp[i]>ans) theans=Dp[i]; - Wuyi if(ans>N) theans=N; - Wuprintf"%d\n", ans); - } About $ return 0; - -}View Code
HDU 1422 relive the World Cup foundation DP Good question