您的位置:首页 > 其它

【HDU】5477---A Sweet Journey(二分)

2017-09-12 09:30 190 查看
                                                                                          A Sweet Journey HDU - 5477 

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 ($1 \leq t \leq 50$), 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: $L_i, R_i$, which represents the interval $[L_i, R_i]$ is swamp. 

$1 \leq n \leq 100, 1 \leq L \leq 10^5,1 \leq A \leq 10,1 \leq B \leq 10,1 \leq L_i < R_i \leq L$. 

Make sure intervals are not overlapped which means $R_i < L_{i + 1}$ for each i ($1 \leq i < 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

1

2 2 2 5

1 2

3 4

Sample Output

Case #1: 0

思路分析:

            (1)题意:一段路程,由沼泽地和平地交错排列,在沼泽地每米要消耗A能量,在平地每米会重新获得B能量,求要顺利走完全程,在最开始至少应该已经有的能量。

            (2)分析:走完每一段沼泽地所要消耗的能量都要小于等于在走之前已经具备的能量,已经具备的能量包括本段沼泽地之前一段平地获得的能量和平地之前就已经累积            的能量。每走完一段平地和沼泽地所剩余的能量找最小者,最后找到的最小者即为一开始就需要的能量,这样才可以顺利走完全程。如果最小者为正数或0,一开始需要的就是0,因为每一段都可以走完。如果最小者为负数,一开始最少就是它的相反数,最少的那一段都可以走完了,那么任何一段都可以走完了哦。

代码如下:

#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
struct node
{
int l,r;
}da[111];
int main()
{
int t,k=0;
scanf("%d",&t);
while(t--)
{
int n,A,B,L;
scanf("%d%d%d%d",&n,&A,&B,&L);
int sum=0,ans=0,st=0;
for(int i=0;i<n;i++)
{
scanf("%d%d",&da[i].l,&da[i].r); //某一段沼泽地的区间
sum+=B*(da[i].l-st)-A*(da[i].r-da[i].l);//走完前面一块平地和这一块沼泽地之后剩余的能量,还需要叠加上原来留下来的能量
ans=min(ans,sum); //找到走完每一段平地和沼泽地后所剩余能量的最小者,
st=da[i].r;
}
printf("Case #%d: ",++k);
if(ans>=0) //如果最后所剩余能量为正,说明最开始需要具备的能量为0
printf("0\n");
else //如果最后为负,说明一开始需要它的相反数那样的能量
printf("%d\n",-1*ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: