110405 Shoemaker's Problem


#include <iostream>

using namespace std;

class ShoeMaker
{
public:
	void Init(int index, int days, int punishment)
	{
		m_index = index;
		m_days = days;
		m_punishment = punishment;
	}

	class Comparer
	{
	public:
		bool operator()(const ShoeMaker& a, const ShoeMaker& b)
		{
			int x = a.m_days * b.m_punishment;
			int y = a.m_punishment * b.m_days;
			if (x == y)
				return a.m_index > b.m_index;
			return x > y;
		}
	};

	int m_index;

private:
	int m_days;
	int m_punishment;
};

template <typename T>
static void Swap(T* data, int index1, int index2)
{
	T temp = *(data + index1);
	*(data + index1) = *(data + index2);
	*(data + index2) = temp;
}

template <typename T, typename GreaterThanFunc>
static int Partition(T* data, int left, int right, GreaterThanFunc greaterThanFunc)
{
	T& ref = *(data + right);
	int lastSmallerThanRefIndex = left - 1;
	for (int i = left; i < right; ++i)
	{
		if (greaterThanFunc(ref, *(data + i)))
		{
			++lastSmallerThanRefIndex;
			Swap(data, lastSmallerThanRefIndex, i);
		}
	}

	++lastSmallerThanRefIndex;
	Swap(data, lastSmallerThanRefIndex, right);

	return lastSmallerThanRefIndex;
}

template <typename T, typename GreaterThanFunc>
static void QuickSort(T* data, int left, int right, GreaterThanFunc greaterThanFunc)
{
	if (left >= right)
		return;

	if ((right - left) == 1)
	{
		if (greaterThanFunc(*(data + left), *(data + right)))
			Swap(data, left, right);
		return;
	}

	int mid = Partition(data, left, right, greaterThanFunc);
	QuickSort(data, left, mid - 1, greaterThanFunc);
	QuickSort(data, mid + 1, right, greaterThanFunc);
}

static void RunTest()
{
	int cnt;
	cin >> cnt;
	if (cnt <= 0)
		return;
	ShoeMaker* shoeMakers = new ShoeMaker[cnt];
	int days, punishment;
	for (int i = 0; i < cnt; ++i)
	{
		cin >> days >> punishment;
		shoeMakers[i].Init(i + 1, days, punishment);
	}

	QuickSort(shoeMakers, 0, cnt - 1, ShoeMaker::Comparer());
	for (int i = 0; i < cnt; ++i)
	{
		if (i != 0)
			cout << ' ';
		cout << shoeMakers[i].m_index;
	}
	cout << endl;
	delete[] shoeMakers;
}

static void Test()
{
	int cnt;
	cin >> cnt;
	for (int i = 0; i < cnt; ++i)
	{
		if (i != 0)
			cout << endl;
		RunTest();
	}
}

int main(int argc, char* argv[])
{
	Test();
	return 0;
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值