[LINK]
Ultraviolet A: 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 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 varous agencies which offer paperwork limit ction plans:
For$They will reduce your paperwork by one unit.
For$ BThey will reduce your entire paperwork by half (rounding down when necessary ).
Note that work can never be forced CED 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 instances to follow. Each case begins with thposiree integers separated by spaces:N-Your starting
Workload,M-Your target workload, andL-The number of work functions ction agencies available to you, (1 <= m <= n <= 100000, 1 <= L <= 100). The nextLLines have the format "[Agency name]:,B",
WhereAAndBAre the rates as described abve 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
【Topic description]
The company requires you to complete n tasks, but you cannot do all of them, so you need to hire someone else to do it, so that when you have m copies left, you can start your own work. There is now an organization with two payment options. The first one is to help you complete one copy for every dollar, the second is to complete half of the remaining tasks (rounding down) for every dollar B ).
[Ideas and Summary]
Obviously greedy, you can choose the most cost-effective payment method each time.
However, I still encountered some trouble. Due to poor English, I had a lot of questions about the meaning of rounding down when I did this. I checked the dictionary and explained it as "downgrading ", therefore, Division 2 is performed directly. However, the sample is always failed. Then I checked it online and said it should have been rounded up. After I changed it, it would have been over.
[Code]
/* * 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;}
-- The meaning of life is to give it meaning.
Original
Http://blog.csdn.net/shuangde800
, By d_double (reprinted, please mark)