首页 > 其他 > 详细

[贪心&&排序]UVA10670 Work Reduction

时间:2014-04-06 15:48:23      阅读:539      评论:0      收藏:0      [点我收藏+]

Problem C: Work Reduction

bubuko.com,布布扣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", whereA 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

Gilbert Lee

题意:现在有N个任务要做,而且要留M个到明天做,你自己做不完,只能请团队帮你做,每个团队有两种方案,一个一个任务做或者把你当前的任务的一半做掉,花费各不相同,求出对每一个团队的最小花费。

思路:典型的贪心题目,每次都选择最优方案,并且保证当前的任务数量大于等于M。

代码:

#include<iostream>
#include<algorithm>
#include<string>

using namespace std;

class Node
{
public:
	string str;
	int cost;
}node[10005];

bool cmp(Node s1,Node s2)
{
	if(s1.cost!=s2.cost) return s1.cost<s2.cost;
	else return s1.str<s2.str;
}

int main()
{
	int num,pos;
	cin>>num;
	for(pos=1;pos<=num;pos++)
		{
			int x,y,z,r,t;
			cin>>x>>y>>z;
			char ch;
			for(int i=0;i<z;i++)
				{
					int work=x;
					string st="";
					while(cin>>ch&&ch!=‘:‘)
						{
							st=st+ch;
						}
					node[i].str=st;
					cin>>r>>ch>>t;
					int val=0;
					while(t<((work+1)/2*r)&&(work-(work+1)/2)>=y)
						{
							val=val+t;
							work=work-(work+1)/2;
						}
					val=val+(work-y)*r;
					node[i].cost=val;
				}
			sort(node,node+z,cmp);
			cout<<"Case "<<pos<<endl;
			for(int i=0;i<z;i++)
				{
					cout<<node[i].str<<" "<<node[i].cost<<endl;
				}
		}
	return 0;
}



[贪心&&排序]UVA10670 Work Reduction,布布扣,bubuko.com

[贪心&&排序]UVA10670 Work Reduction

原文:http://blog.csdn.net/zju_ziqin/article/details/23022869

(0)
(1)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!