UVA - 10670 Work Reduction 贪心

题目大意:有N个单元的文件,boss你只能剩下M个单元的文件,有L家抄写机构,每个机构都能提供两种服务,一种是抄1个单元,另一种是抄一半,两个服务各有价格,要求列出一张表单,表单上是每个机构的名称和聘请该机构完成任务所需的最小钱数,要求按钱数的升序排列,如果钱数相同,就按机构名称的升序排列,注意,机构名称是一个字符串

解题思路:贪心,如果帮忙完成一半的服务比完成一个单元*要完成的单元的价格还要少的话,肯定先选要完成的单元。

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 100 + 5;
int N,M,L;

char name[maxn][20];
int cost[maxn],a[maxn],b[maxn],temp[maxn];
char str[maxn];
int cmp(const void *p1,const void *p2) {
	int *p = (int *)p1;
	int *q = (int *)p2;
	if(cost[*p] == cost[*q])
		return strcmp(name[*p],name[*q]);
	else
		return cost[*p] > cost[*q] ? 1:-1;
}
int main() {
	int test;
	int mark = 1;
	scanf("%d",&test);
	while(test--) {
		scanf("%d%d%d\n", &M,&N,&L);

		for(int i = 0; i < L; i++) {
			scanf("%s",str);
			for(int j = 0; str[j];j++)
				if(str[j] == ':' || str[j] == ',')
					str[j] = ' ';

			sscanf(str,"%s%d%d",name[i],&a[i],&b[i]);
		}

		printf("Case %d\n", mark++);
		memset(cost,0,sizeof(cost));
		int s;
		for(int i = 0; i < L; i++) {
			s = M;
			while( s-(s + 1)/2 >= N && b[i] <  (s+1) / 2 * a[i]) {
				s = s -  (s + 1) / 2;
				cost[i] = cost[i] + b[i];	
			}
			cost[i] = cost[i] + a[i] * (s - N);
		}

		for(int i = 0; i < L; i++)
			temp[i] = i;	

		qsort(temp,L,sizeof(temp[0]),cmp);
		for(int i = 0; i < L; i++)
			printf("%s %d\n",name[temp[i]],cost[temp[i]]);	
	}
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值