uva 10670 Work Reduction (贪心)

解决UVA 10670比赛问题,你需要通过雇佣帮助来减少工作量。题目提供了两种减负方案,一种是按单位减少,一种是按半数减少。你需要计算出每个机构解决工作量问题的最低成本,并按成本和名称排序输出。
摘要由CSDN通过智能技术生成

                          uva 10670 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


题目大意:每组数据包括两个部分:

1)N总任务数,M目标任务数,L公司数目。

2)L行,每行3个有效数据:编号(长度1~16),完成一个任务所需的金额,完成一般任务所需的金额(一半为  (当前任务数 + 1) / 2)。

求每家公司完成N - M个任务所需的最小金额,按金额的升序输出,若金额相同,按字符序输出。

解题思路:贪心。先减半,减半到不能再减(再减小于M),或者减半的性价比不如减一时,开始减一。



#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
using namespace std;
int N, M, L;
char s[10000];
struct A{
	char id[20];
	int a, b, sum;
};
A f[10500];
int cmp(A p, A q) {
	if (p.sum != q.sum) {
		return p.sum < q.sum;
	}
	else return strcmp(p.id, q.id) < 0;
}
void cal(int x) {
	f[x].sum = 0;
	int temp = N;
	while (temp - (temp + 1) / 2 >= M && f[x].b < (temp + 1) / 2 * f[x].a) {
		temp -= (temp + 1) / 2;
		f[x].sum += f[x].b;
	}
	f[x].sum += f[x].a * (temp - M);
}
int main() {
	int T, Case = 1;
	scanf("%d", &T);
	while (T--) {
		printf("Case %d\n", Case++);
		scanf("%d %d %d\n", &N, &M, &L);
		for (int i = 0; i < L; i++) {
			scanf("%s", s);
			for (int j = 0; j < strlen(s); j++) {
				if (s[j] == ':') {
					s[j] = ' ';
					break;
				}
			}
			sscanf(s, "%s%d,%d\n", f[i].id, &f[i].a, &f[i].b);
			cal(i);
		}
		sort(f, f + L, cmp);
		for (int i = 0; i < L; i++) {
			printf("%s %d\n", f[i].id, f[i].sum);
		}
	}
	return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值