POJ 1907 Work Reduction

 

Work Reduction
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 1292   Accepted: 438

Description

Paperwork is beginning to pile up on your desk, and tensions at the workplace are starting to mount. Your boss has threatened to fire you if you don't make any progress by the end of the day. You currently have N units of paperwork on your desk, and your boss demands that you have exactly M units of paperwork left by the end of the day. 
The only hope for you now is to hire help. There are various agencies which offer paperwork reduction plans: 

For $A they will reduce your paperwork by one unit. 
For $B they will reduce your entire paperwork by half (rounding down when necessary). 


Note that work can never be reduced to less than 0. 

Your task now is to produce a sorted table of agency names and their respective minimum costs to solve your workload problem. 

Input

The first line of input consists of a single positive integer representing the number of cases to follow. Each case begins with three positive integers separated by spaces: N - your starting workload, M - your target workload, and L - the number of work reduction agencies available to you, (1 <= M <= N <= 100000, 1 <= L <= 100). The next L lines have the format "[agency name]:A,B", where A and B are the rates as described above for the given agency. (0 <= A,B <= 10000) The length of the agency name will be between 1 and 16, and will consist only of capital letters. Agency names will be unique.

Output

For each test case, print "Case X", with X being the case number, on a single line, followed by the table of agency names and their respective minimum costs, sorted in non-decreasing order of minimum costs. Sort job agencies with identical minimum costs in alphabetical order by agency name. For each line of the table, print out the agency name, followed by a space, followed by the minimum required cost for that agency to solve your problem.

Sample Input

2
100 5 3
A:1,10
B:2,5
C:3,1
1123 1122 5
B:50,300
A:1,1000
C:10,10
D:1,50
E:0,0

Sample Output

Case 1
C 7
B 22
A 37
Case 2
E 0
A 1
D 1
C 10
B 50

Source

Waterloo local 2004.06.12

 

 /*典型的贪心问题,很好证明:因为每次总的工作量的一半采用什么策略,不会影响
剩下一半工作量的最优值
假设当前要处理的工作量是CurWork,当前的花费是total那么贪心策略的选取是:
比较B和(CurWork - int(CurWork / 2))×A的大小,哪个小就将total加上哪个
然后CurWork = int(CurWork / 2)
!注意当(CurWork / 2) < M时只能采用一个一个取的策略,否则如果采取取半的策略,
得到的剩余工作量将小于M,不符合题意

*/
#include <iostream> #include <algorithm> #include <cstring> #define MAX_A 105 using namespace std; struct agent { char name[20]; int cost; }agents[MAX_A + 1]; int N, M, L; char temp[50]; bool compare(const agent &a1, const agent &a2) { return a1.cost == a2.cost ? strcmp(a1.name, a2.name) <= 0 : a1.cost < a2.cost; } int getMinCost(int A, int B) { int cur = N, total = 0; while(cur > M) { int half = cur / 2; if(half < M) { total += (cur - M) * A; cur = M; } else { if(B <= (cur - half) * A) total += B; else total += (cur - half) * A; cur = half; } } return total; } int main() { int caseN, c, i, j; scanf("%d", &caseN); for(c = 1; c <= caseN; c++) { int A, B; scanf("%d%d%d", &N, &M, &L); for(i = 0; i < L; i++) { scanf("%s", temp); j = 0; int len = strlen(temp); while(temp[j] != ':' && j < len) { agents[i].name[j] = temp[j]; j++; } agents[i].name[j] = '/0'; j++; A = 0; while(temp[j] != ',' && j < len) { A = A * 10 + int(temp[j] - '0'); j++; } j++; B = 0; while(j < len) { B = B * 10 + int(temp[j] - '0'); j++; } agents[i].cost = getMinCost(A, B); } sort(agents, agents + L, compare); printf("Case %d/n", c); for(i = 0; i < L; i++) printf("%s %d/n", agents[i].name, agents[i].cost); } return 0; } 

你可能感兴趣的:(c,工作,table,Integer,input,each)