UVa Problem 10026 Shoemaker’s Problem (鞋匠的烦恼)

// Shoemaker’s Problem (鞋匠的烦恼)
// PC/UVa IDs: 110405/10026, Popularity: C, Success rate: average Level: 2
// Verdict: Accepted
// Submission Date: 2011-05-23
// UVa Run Time: 0.008s
//
// 版权所有(C)2011,邱秋。metaphysis # yeah dot net
//
// 算法:假设有 n 个订单 T1 ~ Tn,罚金分别为 S1 ~ Sn,为了让罚金尽可能少,则一个订单必须被优先处
// 理的充要条件是:设订单 Tx(Sx) 为 T1 ~ Tn中 的任意一个订单,只要满足 Tx * (未处理订单罚金总
// 和) < Sx * (未处理订单需时总和),则订单 Tx 应该被优先处理。那么比较任意两个订单 Tx 和 Ty,
// 只要 Tx * Sy < Ty * Sx,则订单 Tx 应该优先处理,将订单按照以上规则排序,如果订单 Tx 与其
// 它订单都满足以上关系,则订单 Tx 必须优先被处理。
	
#include <iostream>
#include <algorithm>
	
using namespace std;
	
#define MAXSIZE 1000
	
struct order
{
	int days;
	int fine;
	int index;
};
	
bool cmp(order x, order y)
{
	return x.days * y.fine < y.days * x.fine;
}
	
int main(int ac, char *av[])
{
	order orders[MAXSIZE];
	int capacity;
	int cases;
	
	cin >> cases;
	while (cases--)
	{
		cin >> capacity;
		int counter = 0;
		while (counter < capacity)
		{
			cin >> orders[counter].days >> orders[counter].fine;
			orders[counter].index = (counter + 1);
			counter++;
		}
		
		sort(orders, orders + capacity, cmp);
		
		for (int i = 0; i < capacity; i++)
		{
			cout << orders[i].index;
			if (i < (capacity - 1))
				cout << " ";
		}
		
		cout << endl;
		
		if (cases)
			cout << endl;
	}
	
	return 0;
}


你可能感兴趣的:(c,Date,算法,struct)