Uva--10670(贪心)

2014-07-25 13:42:10

 Problem C: Work Reduction

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.

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.

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

思路:简单的贪心,注意一下rounding down是四舍五入的意思,((number + 1) / 2)可以达到这个效果QAQ。
 1 /*************************************************************************
 2     > File Name: f.cpp
 3     > Author: Nature
 4     > Mail: 564374850@qq.com
 5     > Created Time: Thu 24 Jul 2014 11:05:39 PM CST
 6 ************************************************************************/
 7 
 8 #include <cstdio>
 9 #include <cstring>
10 #include <cstdlib>
11 #include <cmath>
12 #include <iostream>
13 #include <algorithm>
14 using namespace std;
15 
16 struct node{
17     char name[20];
18     int a,b,tmin;
19 }no[105];
20 
21 bool cmp(node a,node b){
22     if(a.tmin == b.tmin)
23         return strcmp(a.name,b.name) < 0;
24     return a.tmin < b.tmin;
25 }
26 
27 int Case,N,M,L;
28 
29 void Work(int k){
30     int tem,f1,f2;
31     int st = N;
32     int ed = M;
33     int sum = 0;
34     while(st > ed){
35         tem = (st + 1) / 2;
36         if(st - tem < ed) f1 = no[k].a * (st - ed);
37         else f1 = no[k].a * tem;
38         if(tem != 0 && st - tem >= ed) f2 = no[k].b;
39         else f2 = 1000000000;
40         sum += min(f1,f2);
41         st -= tem;
42     }
43     no[k].tmin = sum;
44 }
45 
46 int main(){
47     scanf("%d",&Case);
48     for(int t = 1; t <= Case; ++t){
49         scanf("%d%d%d",&N,&M,&L);
50         getchar();
51         for(int i = 0; i < L; ++i){
52             int j = 0;
53             while(scanf("%c",&no[i].name[j]) && no[i].name[j] != ':') ++j;
54             no[i].name[j] = '\0';
55             scanf("%d,%d",&no[i].a,&no[i].b);
56             getchar();
57             Work(i);
58         }
59         sort(no,no + L,cmp);
60         printf("Case %d\n",t);
61         for(int i = 0; i < L; ++i)
62             printf("%s %d\n",no[i].name,no[i].tmin);
63     }
64     return 0;
65 }

 


posted @ 2014-07-25 13:44  Naturain  阅读(148)  评论(0编辑  收藏  举报