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. Pair Project: Elevator Scheduler [电梯调度算法的实现和测试]:思考题——谢勤政11061197

    第一题: 大楼里面的电梯一般分区域,或考虑思考题第四题的情况,运行楼层不一样的电梯属于不同的区域.然后在接口IRequest和IPassenger还有IElevator里面都加上int area这个属 ...

  2. poj 3261 Milk Patterns(后缀数组)(k次的最长重复子串)

    Milk Patterns Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 7938   Accepted: 3598 Cas ...

  3. Codeforces Round #329 div2

    Problem_A(593A): 题意: 给n个单词, 每个单词由小写字母组成, 且长度<=1000. 组成一篇文章的要求是: 所有单词所用字母 <= 2 即最多只能有两个不同的字母. 求 ...

  4. HTML中的转义字符

    HTML常用符号: 显示一个空格    < 小于 < <> 大于 > >& &符号 & &" 双引号 " &qu ...

  5. What's this?(js)

    What's this? 由于运行期绑定的特性,JavaScript 中的 this 含义非常多,它可以是全局对象.当前对象或者任意对象,这完全取决于函数的调用方式 随着函数使用场合的不同,this的 ...

  6. [Unity菜鸟] Mecanim 系统遇到的问题

    1. 给角色添加一个Animator组件和New State,运行后,摆出这种奇怪的姿势 这是因为没有把动画片段赋给New State,可以看到此时的New State为空,把Idle片段拖进去就好了 ...

  7. SPRING IN ACTION 第4版笔记-第三章ADVANCING WIRING-007-给BEAN运行时注入值placeholder、@Value

    一.用placeholder给bean运行时注入值的步骤 Spring取得placeholder的值是用${...} 1.声明placeholder bean (1)java方式 In order t ...

  8. Qt中如何写一个model(自定义一个RowNode,我没有碰到过)

    在qt中,用到最多就是model/view的结构来表示数据层及表示层的关系.model用于给view提供数据.那如何来实现一个简单的树形model呢. 实现一个自己的model需要重载以下的方法: Q ...

  9. QT4项目升级到QT5遇到的问题和解决方法

    QT4升级到QT5改动: PC部分: [改QTDIR变量] 在工程根目录下找到.user文件, 如InnoTabPlugin.vcxproj.user 修改指向你的QT5根目录: <Proper ...

  10. android viewpager change adapter ---在使用viewpager设置新的adapter的时候发现页面还是显示旧的adapter中的值

    有一个需求是当用户选择navigationview中的某一项时,右边的viewpager需要动态切换不同的adapter 发现直接setAdapter没有任何反应,加载的数据还是旧的数据 折腾了半天只 ...