hdu 5477 A Sweet Journey 模拟

来源:互联网 发布:软件项目管理ppt 编辑:程序博客网 时间:2024/05/16 16:10

A Sweet Journey

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1070    Accepted Submission(s): 539


Problem Description
Master Di plans to take his girlfriend for a travel by bike. Their journey, which can be seen as a line segment of length L, is a road of swamps and flats. In the swamp, it takes A point strengths per meter for Master Di to ride; In the flats, Master Di will regain B point strengths per meter when riding. Master Di wonders:In the beginning, he needs to prepare how much minimum strengths. (Except riding all the time,Master Di has no other choice) 

 

Input
In the first line there is an integer t (1t50), indicating the number of test cases.
For each test case:
The first line contains four integers, n, A, B, L.
Next n lines, each line contains two integers: Li,Ri, which represents the interval [Li,Ri] is swamp.
1n100,1L105,1A10,1B101Li<RiL.
Make sure intervals are not overlapped which means Ri<Li+1 for each i (1i<n).
Others are all flats except the swamps.
 

Output
For each text case:
Please output “Case #k: answer”(without quotes) one line, where k means the case number counting from 1, and the answer is his minimum strengths in the beginning.
 

Sample Input
12 2 2 51 23 4
 

Sample Output
Case #1: 0
 

Source
2015 ACM/ICPC Asia Regional Shanghai Online
 

Recommend
hujie   |   We have carefully selected several similar problems for you:  5932 5931 5930 5929 5928 
 

Statistic | Submit | Discuss | Note


题意:一段长为L的路上有沼泽和平地,平地上每行驶单位路程恢复B点能量,沼泽上则减少A点能量。问一开始最少需要准备多少能量。


解法:关键是使得行驶到沼泽右端点时的能量大于等于0即可。于是假设一开始有0点能量,能量为负仍然可以前进。现在模拟,找到能量的最低值minE,如果>=0,那么一开始不需要准备能量,否则准备-minE点。


#include<cstdio>#include<iostream>#include<algorithm>using namespace std;const int INF =0x3f3f3f3f;int n,A,B,L;int main(){   std::ios::sync_with_stdio(false);   int T,le,ri,kase=0;cin>>T;   while(T--)   {      cin>>n>>A>>B>>L;      int p=0,e=0,mine=INF;      for(int i=1;i<=n;i++)      {          cin>>le>>ri;          e+= B*(le-p)-A*(ri-le  );          mine=min(mine,e);          p=ri;      }      int ans=mine<0?-mine:0;      printf("Case #%d: %d\n",++kase,ans);   }   return 0;}


0 0
原创粉丝点击