Link
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
"Original question"
Paperwork is beginning to pile up in your desk, and tensions at the workplace are to mount. Your boss has threatened to fire for you if don ' t make any progress by the "end of". Currently have N units of paperwork on your desk, and your boss demands this you have exactly M units of paperwork T by the "end of".
The only hope for you are to hire help. There are various agencies which offer paperwork reduction:
For $A they would reduce your paperwork by one. For $B they would reduce your entire paperwork by half (rounding down when necessary).
Note that work can never is reduced to less than 0.
Your task now to produce a sorted table of agency names and their respective minimum costs to solve Your workload probl Em.
The ' a single positive integer representing the number of the ' a ' of input consists of cases to follow. Each case begins and three positive integers separated by spaces:n-your starting workload, M-your target workload, a nd 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. (0 <= a,b <= 10000) The length of the agency name would be between 1 and would consist only for capital letters. Agency names would be unique.
For each test case, print ' Case X ', with X being the ' Case number, ' a single ', 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 the "table", print out the agency name, followed by a spaces, followed by the minimum required Agency to solve your problem.
Sample Input
2
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
A Panax Notoginseng case
2
E 0
a 1
D 1
C ten
B 50
"The main effect of the topic"
The company wants you to complete n tasks, but you are impossible to complete, so you need to hire someone else to do, to do the rest of the M, you personally. Now there is a body, there are two ways to pay, the first is to pay a dollar a to help you complete 1, the second is a B-dollar to help you complete the remaining tasks of half (rounding down).
"Thinking and Summarizing"
It is obvious that the greedy question, each time choose the most cost-effective way to pay.
This column more highlights: http://www.bianceng.cn/Programming/sjjg/
But still encountered a bit of trouble, due to poor English, do this when the rounding down this meaning has a lot of doubt, check the dictionary explanation is "down to the whole", so directly except 2. But the example never comes out. Then online check the next, said should be rounded, changed the next, it passed.
Code
* * 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 & Lt
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]= ' "; 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; }