UVa 10670 - Work Reduction

来源:互联网 发布:大秀直播程序源码 编辑:程序博客网 时间:2024/05/21 06:31

【链接】

UVA:  http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=1611

poj:     http://poj.org/problem?id=1907


【原题】

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

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

Sample Output

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



题目大意】

公司要你要完成N份任务,但是你是不可能全部完成的,所以需要雇佣别人来做,做到剩下M份时,自己再亲自出马。现在有个机构,有两种付费方式,第一种是每付A元帮你完成1份,第二种是每付B元帮你完成剩下任务的一半(rounding down)。


【思路与总结】

很明显的贪心题,每次选择性价比最高的付费方式。

但是还是遇到了点麻烦,由于英文比较烂,做这题时对rounding down这个意思有很大的疑问,查了字典的解释是“向下取整”, 所以就直接除2。但是样例却一直出不来。然后网上查了下,说应该是四舍五入,改了下,就过了。


【代码】

/* * UVa: 10670 - Work Reduction * Time: 0.012s(UVa),  0MS(poj) * Author: D_Double * */#include<iostream>#include<algorithm>#include<cstring>#include<cstdio>#define MAXN 102using namespace std;int M,N,L;struct Node{    char name[20];    int A, B;    int cost;    friend bool operator < (const Node&a, const Node&b){        if(a.cost!=b.cost)            return a.cost < b.cost;        return strcmp(a.name,b.name) < 0;    }}arr[MAXN];inline void input(){    char str[200];    scanf("%d%d%d",&N,&M,&L);    for(int i=0; i<L; ++i){        scanf("%s",str);        int j;        for(j=0; str[j]!=':'; ++j)            arr[i].name[j]=str[j];        arr[i].name[j]='\0';        sscanf(str+j+1, "%d,%d",&arr[i].A,&arr[i].B);        arr[i].cost=0;    }}inline void greedy(){    for(int i=0; i<L; ++i){        int left=N;        int A=arr[i].A, B=arr[i].B;        int half=(left+1)/2;        while(left-half>=M && B<=half*A){            arr[i].cost += B;            left -= half;            half=(left+1)/2;        }        if(left>M)arr[i].cost += (left-M)*A;    }}int main(){    int T, cas=1;    scanf("%d",&T);    while(T--){        input();        greedy();        sort(arr, arr+L);        printf("Case %d\n",cas++);        for(int i=0; i<L; ++i){             printf("%s %d\n",arr[i].name, arr[i].cost);        }    }    return 0;}

——  生命的意义,在于赋予它意义。

          
     原创 http://blog.csdn.net/shuangde800 , By   D_Double  (转载请标明)


原创粉丝点击