HDU 4011 Working in Beijing 在北京工作(解析 简单模拟 择优)

HDU 4011 Working in Beijing 在北京工作(简单模拟 择优)
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=4011

Working in Beijing
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others)
Total Submission(s): 2650 Accepted Submission(s): 1038

Problem Description
Mr. M is an undergraduate student of FDU. He finds an intern position in Beijing, so that he cannot attend all the college activities. But in some conditions, he must come back to Shanghai on certain date. We can assume the important activities that Mr. M must attend are occupy a whole day. Mr. M must take flight to Shanghai before that day and leave after that day. On the other hand, Mr. M is absent in Beijing and he will lose his salary for his absent.
Sometimes the cost of flight is much higher than the loss of salary, so to save the cost on the travel, Mr. M can stay in Shanghai to wait for another important date before he back to Beijing.
Now, Mr. M knows all of the important date in the next year. Help him schedule his travel to optimize the cost.

Input
The input contains several test cases. The first line of single integer indicates the number of test cases.
For each test case, the first line contains three integers: n, a and b, denoting the number of important events, the cost of a single flight from Beijing to Shanghai or Shanghai to Beijing and the salary for a single day stay in Beijing. (1 <= n <= 100000, 1 <= a <= 1000000000, 1 <= b <=100)
Next line contains n integers ti, denoting the time of the important events. You can assume the ti are in increasing order and they are different from each other. (0 <= ti <= 10000000)

Output
For each test case, output a single integer indicating the minimum cost for this year.

Sample Input
2
1 10 10
5
5 10 2
5 10 15 65 70

Sample Output
Case #1: 30
Case #2: 74

Source
The 36th ACM/ICPC Asia Regional Shanghai Site —— Warmup

题目解析:题意就是M先生在北京工作,每年需要去上海参加一些会议,有两种方案,一种是两次会议之间的时间待在上海;另一种是参加完一次会议返回北京,到时候再去上海参加第二场会议。分析两种中的最低成本并输出。
无论如何,开会的天数都会因旷工而扣工资,还有第一次前往和最后一次离开的机票钱是固定的。分析内部两间隔的情况即可。

AC代码:

#include

using namespace std;

const int X = 100008;
long long int al[X];
long long  money;              //需要分配足够数值范围

int main()
{
	int n,k=1;
	cin >> n;
	while (n--)
	{
		money = 0;
		memset(al, 0, sizeof(al));
		int a, b, c, i, j;
		scanf("%d%d%d", &a, &b, &c);
		for (i = 0; i < a; i++)
		{
			scanf("%d", &al[i]);
		}
		if (a == 1)
		{
			cout << "Case #"<<k++<<": "<<(2 * b + c) << endl; continue;
		}
		for (i = 0, j = 1; j < a; i++, j++)
		{
			if ((al[j] - al[i]-1) * c < 2 * b)    //选出两者中费用较少的
			{
				money =money+ (al[j] - al[i]-1) * c;
			}
			else
			{
				money = money + 2 * b;
			}
		}
		money = money + a * c + 2 * b;   //加上固定的花费
		cout << "Case #" << k++ << ": "<< money << endl;
	}
	return 0;
}

你可能感兴趣的:(HDU,模拟,择优)