欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

HDU-5477-A Sweet Journey(简单模拟)

程序员文章站 2022-06-09 17:42:56
...

题目链接---HDU-A Sweet Journry


A Sweet Journey

                                                          Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
                                                          Total Submission(s): 117    Accepted Submission(s): 60
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) 

HDU-5477-A Sweet Journey(简单模拟)
 
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
题意:Di骑自行车旅行,经过平地和沼泽,经过沼泽时要消耗A的体力,经过平地时可以恢复B的体力,求出发时Di最少体力多少才能经过整段路程。
题解:该题可以直接进行简单模拟,先假设Di出发时的体力为0,求他所消耗的最大体力

代码实现如下:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define INF 0x3f3f3f 
using namespace std;
int main()
{
	int t;
	cin>>t;
	int k=1;
	while(t--)
	{
		int n,a,b,l,R,L;
		cin>>n>>a>>b>>l;
		int sum=0,p=0,minx=INF;
		for(int i=0;i<n;i++)
		{
			cin>>L>>R;
			sum=sum+b*(L-p)-a*(R-L);
			minx=min(minx,sum);//求消耗体力的最大值 
			p=R;
		} 
		printf("Case #%d: ",k++);
		if(minx<0)
		cout<<-minx<<endl;
		else
		cout<<0<<endl; 
	}
return 0;	
}