(挑戰編程_4_5)Shoemaker's Problem


http://www.programming-challenges.com/pg.php?page=downloadproblem&probid=110405&format=html
#include <iostream>
#include <cstdlib>
#include <vector>
#include <set>
#include <string>
#include <fstream>
#include <algorithm>
#include <sstream>
#include <string.h>
#include <cassert>
#include <iomanip>

using namespace std;

//#define _MY_DEBUG_

// 訂單
struct Order
{
	int id;			// 訂單號
	int time;		// 完成訂單所需時間
	int punish;		// 罰款
	bool isServed;	// 是否被處理:似乎用不到
	Order() : id(0), time(0), punish(0), isServed(false)
	{}
	Order(int _id, int _time, int _punish, bool _isServed)
		: id(_id), time(_time), punish(_punish), isServed(_isServed)
	{
	}
};

// 存放訂單
Order orders[1010];


/*
	MainStructure: 主結構
*/
void MainStructure();

/*
	Process:處理每一組數據
*/
void Process();

/*
	排序函數:按照punish/time
*/
bool OrderCompare(Order o1, Order o2);


int main()
{
	MainStructure();
	return 0;
}


void MainStructure()
{
	
#ifdef _MY_DEBUG_
	string inputStr = "C:/Users/cao/Desktop/算法/要看的書/算法/2挑戰編程/input/1.inp";
	//string inputStr = "D:/2挑戰編程/input/110205.inp";
	string outputStr = "C:/Users/cao/Desktop/算法/要看的書/算法/2挑戰編程/input/0000001.outp";
	ifstream fin;
	fin.open(inputStr);	

	cin.rdbuf(fin.rdbuf());	

	ofstream fout;
	fout.open(outputStr);
	//cout.rdbuf(fout.rdbuf());

#endif

	Process();
		
}

void Process()
{
	int count = 0;
	cin >> count;
	while (count--)
	{
		int num = 0;
		cin >> num;
		int id = 0;
		int time = 0;
		int punish = 0;
		while (num--)
		{
			cin >> time >> punish;
			Order o(id + 1, time, punish, false);
			orders[id] = o;
			++id;
		}

		sort(orders, orders + id, OrderCompare);

		//stable_sort(orders, orders + id, OrderCompare);

		for (int i = 0; i < id; ++i)
		{
			if (i != 0)
			{
				cout << ' ';
			}
			cout << orders[i].id;
		}
		cout << endl;


		if (count)
		{
			cout << endl;
		}
	}

}

bool OrderCompare(Order o1, Order o2)
{
	double d1 = (double)o1.punish / o1.time;
	double d2 = (double)o2.punish / o2.time;
	return (d1 > d2);
}


注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
粤ICP备14056181号  © 2014-2021 ITdaan.com