UVa 10670 - Work Reduction

【链接】

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

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



题目大意】

公司要你要完成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 102
using 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  (转载请标明)


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值