杭电OJ第4011题,Working in Beijing(题目链接)。
Working in Beijing
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 70Sample Output
Case #1: 30
Case #2: 74Source
The 36th ACM/ICPC Asia Regional Shanghai Site —— Warmup
解题思路:逐一比较往返机票费用与扣除的工资,以少的为准。一定要注意的是,数据超过了32位整型的范围,所以要用64位整型来存放。C语言源代码如下:
#include <stdio.h> #include <stdlib.h> typedef long long COUNT; #define MAX_DAYS 100000 int main (void) { COUNT i, j; long long cases; long long days, ticket, salary; static long long important_days[MAX_DAYS]; long long min_cost; #ifdef _WIN32 scanf( "%I64d", &cases ); #else scanf( "%lld", &cases ); #endif for ( i = 1 ; i <= cases ; i ++ ) { #ifdef _WIN32 scanf( "%I64d%I64d%I64d", &days, &ticket, &salary ); #else scanf( "%lld%lld%lld", &days, &ticket, &salary ); #endif for ( j = 0 ; j < days ; j ++ ) { #ifdef _WIN32 scanf( "%I64d", &important_days[j] ); #else scanf( "%lld", &important_days[j] ); #endif } if ( days == 1 ) { min_cost = ( ticket << 1 ) + salary; } else { min_cost = ticket + salary; for( j = 1 ; j < days ; j ++ ) { if ( ( salary * ( important_days[j] - important_days[j-1] - 1 ) ) <= (ticket << 1) ) { min_cost += salary * ( important_days[j] - important_days[j-1] ); } else min_cost += ( ticket << 1 ) + salary; } min_cost += ticket; } #ifdef _WIN32 printf( "Case #%I64d: %I64d\n", i, min_cost ); #else printf( "Case #%lld: %lld\n", i, min_cost ); #endif } return EXIT_SUCCESS; }