"Greedy strategy" Usaco Trail Run

Source: Internet
Author: User
Tags time limit

Hangzhou two water problems.

question H: Offroad running "greedy strategy"

"Surface"

In order to be able to play well in the next run, Bessie began her training on a mountain path. Bessie wanted to run as far as possible in every training, but she also knew of a rule in the farm: the cows would not be allowed to enter the mountain alone for more than M seconds (1< =m< =10,000,000).

The entire mountain road was divided by Bessie into a small segment of the same length (1< = t< = 100,000), and Bessie used the s_i to represent the road conditions of the sub-segment. S_i is one of the 3 letters of the U,F,D, each of which indicates that paragraph I is uphill, flat, or downhill.

Bessie takes U seconds (1< =u< = 100) to run through an uphill period, and the time spent on a flat level is F seconds (1 < = f < = 100), and a downhill run will take D seconds (1 < = d < = 100). Note that along the path back to the original road, the road was originally uphill to the downhill, originally the downhill road into the uphill.

Bessie wondered how far she could run on the mountain path on the premise that she could return to the farm on time.

Input

Line 1th: 5 integers separated by a space: m,t,u,f, and D

2nd.. T+1 Line: section i+1 behavior 1 Letter s_i, describes the road conditions of section I

Output

Output 1 integers to how far Bessie would go when she returned to the farm on time.

Sample input

5 3 2 1
u
f
u
d
F

Sample output

3

Tips

The maximum time for Bessie to run is 13 seconds (so short ...). ), her running mountain road was divided into 5 sections. Bessie ran an uphill journey of 3 seconds, a flat level of 2 seconds, and a descent of 1 seconds. The direction of each section of the mountain Road is as follows:

/_/\ _ (Let me change it, I really do not see the misleading novice.) )

Bessie runs the first 3 sections of the mountain road, and then returns, the total time spent is 3 + 2 + 3 + 1 + 2 + 1 = 12 seconds, only less than the time limit she can stay outside for 1 seconds. If she runs farther, she will not be able to return to the farm on time.

Exercises

Draw a sketch of the sample first, as shown in the figure:

With a picture, the quality is not good enough to explain the problem.

The whole picture has been clearly expressed,

We know that the cow's starting point must be on the ranch.

But the question did not say where the pasture was, a little pit,

Finally found that the ranch is numbered 1 points.

Easy to spot, cows at pasture time and for 0+0=0

Here the first addend represents the time to go,

The second addend represents the return time, and the following is true;

A brief note of i=1,time[1]=0+0=0;

The same can be asked I=2,time[2]=3+1=4 i=3,time[3]=5+3=8 i=4,time[4]=8+4=12

I=5,time[5]=9+7=16 i=6,time[6]=11+9=20

Discover that the first addend is accumulated at the time of the trip, and the second addend is accumulated at the time of return.

The algorithm is out.

by Tot1[i] (1<=i<=t+1) to indicate the time of the first point;

Tot2[i] (1<=i<=t+1) indicates the time of the first point back;

Time[i]=tot1[i]+tot2[i]; And sweep it out again.

But when comparing the output, we usually use

For i:=1 to T+1 do begin

Time:=tot1[i]+tot2[i];

If Time>m Then

The key is how the output statement in the box is filled in.

Fill Writeln (i-2) or Writeln (i-1) or writeln (i).

Notice, once we time>m is that the road is not allowed to go, need to reduce one, but the starting point to the beginning does not take a step, so minus one, altogether minus two, so the output is Writeln (i-2).

Program

var t1,t2,t3,t,m,i,time:longint;
    Tot1,tot2,a:array[1..100000]of Longint;
Ch:char;
 Begin READLN (M,T,T1,T2,T3);
  For i:=1 to T do begin READLN (CH);
   Case ch of ' U ': a[i]:=1;
   ' F ': a[i]:=2;
  ' d ': a[i]:=3;
 End
 End
 time:=0;
  For i:=1 to t+1 do begin tot1[i]:=time;
   Case A[i] of 1:time:=time+t1;
   2:time:=time+t2;
  3:TIME:=TIME+T3;
 End
 End
 For I:=1 to T+1 does if A[i]=1 then a[i]:=3 else if A[i]=3 then a[i]:=1;
 time:=0;
  For i:=1-T do begin tot2[i]:=time;
   Case A[i] of 1:time:=time+t1;
   2:time:=time+t2;
  3:TIME:=TIME+T3;
 End
 End
 time:=0;
 {for i:=1 to T do write (Tot1[i], ");
 Writeln;
 For I:=1 to T do write (Tot2[i], ");
 Writeln;}
  For i:=1 to T+1 do begin time:=tot1[i]+tot2[i]; If Time>m then begin Writeln (I-2); Halt
 End
 End
Writeln (t); End. 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.