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
题意:有n个任务,要求完成到m个。。现在有l家公司,每家公司有一个名字有A代表完成一项工作花A元,B代表完成一半的工作花B元。。。对于这个完成一半。题目中是说rounding down。。我开始以为是向下取整。结果答案跑出来都跟样例不一样。。后来才明白其实是四舍五入。。。还有题目中公司的名字是字符串。我一开始看样例想当然以为是一个字符。结果WA了几次。。。。。哎
还有一个注意点。输出要按花费少的排序。就是如果花费相同,就按名字的字典序排序。
思路:贪心。。每次比较一件件完成和完成一半谁花得少。就用哪个方案。直到n / 2 >= m 为止。。剩下还没完成的部分。只能一项项完成(因为最后剩下的任务一定要是m)。。
代码:
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std; int t, n, m, l; struct Work {
char name[20];
int a, b;
int value;
} w[105]; int cmp(Work aa, Work bb) {
if (aa.value != bb.value)
return aa.value < bb.value;
if (strcmp(aa.name, bb.name) < 0)
return 1;
else
return 0;
} void solve(int nn) {
int num = n;
while (num / 2 >= m) {
if (w[nn].b < (num -num / 2) * w[nn].a)
w[nn].value += w[nn].b;
else
w[nn].value += w[nn].a * (num - num / 2);
num /= 2;
}
w[nn].value += (num - m) * w[nn].a;
}
int main() {
int Case = 1;
scanf("%d", &t);
while (t --) {
memset(w, 0, sizeof(w));
scanf("%d%d%d%*c", &n, &m, &l);
for (int i = 0; i < l; i ++) {
char sb; int j = 0;
while ((sb = getchar()) != EOF && sb != ':') {
w[i].name[j ++] = sb;
}
w[i].name[j] = '\0';
scanf("%d%*c%d%*c", &w[i].a, &w[i].b);
}
for (int i = 0; i < l; i ++) {
solve(i);
}
sort(w, w + l, cmp);
printf("Case %d\n", Case ++);
for (int i = 0; i < l; i ++)
printf("%s %d\n", w[i].name, w[i].value);
}
return 0;
}

10670 Work Reduction (贪心 + 被题意坑了- -)y的更多相关文章

  1. uva 10670 Work Reduction(贪心)

    题目连接:10670 - Work Reduction 题目大意:有tol的工作量,和要求达到的工作剩余量sur,然后是公司总数,对应每个公司提供两种服务,1.完成一个工作量,2.完成当前未完成工作量 ...

  2. UVa 10670 - Work Reduction

    题目大意:对n份文件进行处理使其减少到m份,有l个机构可供选择.每个机构提供两种方案:每减少一份收费a元,或者减少到文件数量的一半收费b元.根据各个机构收取费用进行排序. 很直接的题目,直接进行模拟就 ...

  3. hdu 5038 水题 可是题意坑

    http://acm.hdu.edu.cn/showproblem.php?pid=5038 就是求个众数  这个范围小 所以一个数组存是否存在的状态即可了 可是这句话真恶心  If not all ...

  4. hdu 6034 贪心模拟 好坑

    关键在排序!!! 数组间的排序会超时,所以需要把一个数组映射成一个数字,就可以了 #include <bits/stdc++.h> using namespace std; typedef ...

  5. 枚举+贪心 HDOJ 4932 Miaomiao's Geometry

    题目传送门 /* 题意:有n个点,用相同的线段去覆盖,当点在线段的端点才行,还有线段之间不相交 枚举+贪心:有坑点是两个点在同时一条线段的两个端点上,枚举两点之间的距离或者距离一半,尽量往左边放,否则 ...

  6. codeforces 1020 C Elections(枚举+贪心)

    题意: 有 n个人,m个党派,第i个人开始想把票投给党派pi,而如果想让他改变他的想法需要花费ci元.你现在是党派1,问你最少花多少钱使得你的党派得票数大于其它任意党派. n,m<3000 思路 ...

  7. AC_Dream 1224 Robbers(贪心)

    题意:n个抢劫犯分别抢到的金钱是k1, k2, k3,...,一共得到的金钱是m, 但是在分钱的时候是按照x1/y, x2/y, x3/y,....的比例进行分配的!这样的话 一些抢劫犯就会觉得不公平 ...

  8. 贪心 POJ 1328 Radar Installation

    题目地址:http://poj.org/problem?id=1328 /* 贪心 (转载)题意:有一条海岸线,在海岸线上方是大海,海中有一些岛屿, 这些岛的位置已知,海岸线上有雷达,雷达的覆盖半径知 ...

  9. HDU4647+贪心

    /* 贪心. 题意:给定一些点 一些边 点和边都有价值.现在A B 选点.求A-B的maxVal 思路:分割边.边的1/2分给两个端点. 如果这两个点被同一个人取,则ok:否则 做减法也行,对题意无影 ...

随机推荐

  1. IOS开发—IOS 8 中设置applicationIconBadgeNumber和消息推送

    摘要 在IOS7中设置applicationIconBadgeNumber不会有什么问题,但是直接在IOS8中设置applicationIconBadgeNumber会报错 因为在IOS8中要想设置a ...

  2. 利用百度地图API,在浏览器中找到自己的位置

    首先你得有个百度地图的秘钥,http://lbsyun.baidu.com/apiconsole/key 剩下的就是编码了 这里面会用到一个javascript里的一个函数,getMyLocation ...

  3. [转载]C# 中对html 标签过滤

    private string FilterHTML(string html) { System.Text.RegularExpressions.Regex regex1 = new System.Te ...

  4. Linux系统调用列表

    转自Linux系统调用列表 一.进程控制: fork 创建一个新进程 clone 按指定条件创建子进程 execve 运行可执行文件 exit 中止进程 _exit 立即中止当前进程 getdtabl ...

  5. Cygwin环境编译/usr/include/sys/_types.h:72:20: 致命错误:stddef.h:can not found

    环境介绍: win7_x64 +Cygwin64 gcc :4.8.2 g++:4.8.1 编译 c++的helloworld.cpp 一直失败! 代码如下: #include <iostrea ...

  6. nginx Location配置总结(转)

    本文部分转自:http://cssor.com/nginx-location-configuration.html 一. 开头 语法规则: location [=|~|~*|^~] /uri/ { … ...

  7. [状压dp]POJ2686 Traveling by Stagecoach

    题意: m个城市, n张车票, 每张车票$t_i$匹马, 每张车票可以沿某条道路到相邻城市, 花费是路的长度除以马的数量. 求a到b的最小花费, 不能到达输出Impossible $1\le n\le ...

  8. C内存管理

    一般而言,分配给进程的内存有四个概念上不同的区域,分别为:代码段.数据段.堆和栈,其中数据段又可以细分为初始化为非零的数据和初始化为零的数据.如下图所示: 1.栈区(stack)— 由编译器自动分配释 ...

  9. 自定义NavigationView's item 的高度

    http://stackoverflow.com/questions/31204320/how-can-i-change-the-navigationviews-item-text-size 自定义s ...

  10. virsh console使用方法

    How to use virsh console virsh 是libvirt 开源函数库中的一个命令行工具,用来在命令行下通过libvirt发布管理虚拟机的各种命令,现在介绍一下其中一个命令的使用方 ...