uva - 10670 - Work Reduction(贪心)

题意:N个单元的文件,Boss要求剩下M个,其余的要你完成。但是在deadline之前你完不成所以你要雇佣机构来帮你。L个机构,每个机构都可以分A、B两种方案,A是帮你完成一个单元,B是帮你完成剩下的一半。输出每个机构的最小花费。注意rounding down是四舍五入的意思。

方法:贪心计算就行,循环结束的条件是(1) temp_N - (temp_N+1)/2 >= M,(2) agen[i].B < (temp_N+1)/2 * agen[i].A。注意这两个就行,下边的WA代码也是这个思路但是wa.. 不知道原因。


AC代码:

#include <iostream>      
#include <iomanip>      
#include <string>      
#include <cstring>      
#include <cstdio>      
#include <queue>      
#include <stack>      
#include <algorithm>      
#include <cmath>      
#include <ctime>  

using namespace std;

const int maxn_f = 100000+10;
const int maxn_a = 100+10;

struct Agency
{
	char name[20];
	int A;
	int B;
	int cost;
};

Agency agen[maxn_a];

void Handle_Input(char temp[], int i)
{
	int j = 0, k = 0;
	char temp1[50];
	memset(temp1, '\0', sizeof(temp1));
	for (j = 0; temp[j] != ':'; j++)
		agen[i].name[k++] = temp[j];
	for (k = 0, j++; temp[j] != ','; j++)
		temp1[k++] = temp[j];
	agen[i].A = atoi(temp1);
	memset(temp1, '\0', sizeof(temp1));
	for (k = 0, j++; j < strlen(temp); j++)
		temp1[k++] = temp[j];
	agen[i].B = atoi(temp1);
}

int cmp(const void *a, const void *b)
{
	struct Agency *c = (Agency *)a;
	struct Agency *d = (Agency *)b;
	if ((*c).cost != (*d).cost)
		return (*c).cost - (*d).cost;
	else
		return strcmp((*c).name, (*d).name);
}

int main()
{
#ifdef Local
	freopen("a.txt", "r", stdin);
#endif
	int t = 0;
	cin >> t;
	for (int kases = 1; kases <= t; kases++)
	{
		memset(agen, 0, sizeof(agen));
		int N = 0, M = 0, L = 0;
		int i = 0;
		cin >> N >> M >> L;
		for (i = 0; i < L; i++)
		{
			char temp[50];
			cin >> temp;
			Handle_Input(temp, i);

			int temp_N = N;
			while (temp_N - (temp_N+1)/2 >= M && agen[i].B < (temp_N+1)/2 * agen[i].A)
			{
				agen[i].cost += agen[i].B;
				temp_N -= (temp_N+1) / 2;
			}
			agen[i].cost += (temp_N-M) * agen[i].A;
		}
		qsort(agen, L, sizeof(agen[0]), cmp);
		cout << "Case " << kases << endl;
		for (i = 0; i < L; i++)
			cout << agen[i].name << " " << agen[i].cost << endl;
	}
	return 0;
}

WA代码:

#include <iostream>      
#include <iomanip>      
#include <string>      
#include <cstring>      
#include <cstdio>      
#include <queue>      
#include <stack>      
#include <algorithm>      
#include <cmath>      
#include <ctime>  

using namespace std;

const int maxn_f = 100000+10;
const int maxn_a = 100+10;

struct Agency
{
	char name[20];
	int A;
	int B;
	int cost;
};

Agency agen[maxn_a];

void Handle_Input(char temp[], int i)
{
	int j = 0, k = 0;
	char temp1[50];
	memset(temp1, '\0', sizeof(temp1));
	for (j = 0; temp[j] != ':'; j++)
		agen[i].name[k++] = temp[j];
	for (k = 0, j++; temp[j] != ','; j++)
		temp1[k++] = temp[j];
	agen[i].A = atoi(temp1);
	memset(temp1, '\0', sizeof(temp1));
	for (k = 0, j++; j < strlen(temp); j++)
		temp1[k++] = temp[j];
	agen[i].B = atoi(temp1);
}

int cmp(const void *a, const void *b)
{
	struct Agency *c = (Agency *)a;
	struct Agency *d = (Agency *)b;
	if ((*c).cost != (*d).cost)
		return (*c).cost - (*d).cost;
	else
		return strcmp((*c).name, (*d).name);
}

int main()
{
#ifdef Local
	freopen("a.txt", "r", stdin);
#endif
	int t = 0;
	cin >> t;
	for (int kases = 1; kases <= t; kases++)
	{
		memset(agen, 0, sizeof(agen));
		double N = 0, M = 0, L = 0;
		int i = 0;
		cin >> N >> M >> L;
		for (i = 0; i < L; i++)
		{
			char temp[50];
			cin >> temp;
			Handle_Input(temp, i);

			double temp_N = N;
			while (true)
			{
				if (temp_N / 2 < M)
				{
					agen[i].cost += (temp_N-M) * agen[i].A;
					break;
				}
				else if ((agen[i].B / (int)(temp_N/2+0.5))  < agen[i].A)
				{
					agen[i].cost += agen[i].B;
					temp_N = temp_N - (int)(temp_N/2+0.5);
				}
				else
				{
					agen[i].cost += (temp_N-M) * agen[i].A;
					break;
				}
			}
		}
		qsort(agen, L, sizeof(agen[0]), cmp);
		cout << "Case " << kases << endl;
		for (i = 0; i < L; i++)
			cout << agen[i].name << " " << agen[i].cost << endl;
	}
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值