programming-challenges Shoemaker's Problem (110405) 题解

原创 2015年07月07日 15:29:52

Greedy. 


证明:

Let's say we have job 1, 2, ..., n, and they have time and fine as t1, f1, t2, f2, ..., tn, fn

and they are in the order of t1/f1 <= t2/f2 <= t3/f3 <= ... <= tn/fn

So this is the objective schedule. Now we change 1 with m (1 < m <= n)

By the original order, we need pay fine as much as: F1 = t1 * (f2 + ... + fn) + t2 * (f3 + ... + fn) + ... + tm * (fm+1 + ... + fn) + R

By the new order, we need pay fine as much as: F2 = tm * (f1 + ... + fm-1 + fm+1 + ... + fn) + t1 * (f2 + ... + fm-1 + fm+1 + ... + fn) + ... + fm-1 * fm+1 + ... + fn) + R

F1 - F2 = (t1 + t2 + ... + tm-1) * fm - (tm * f1 + tm * f2 + ... + tm * fm-1)

As t1 * fm <= tm * f1, t2 * fm <= tm * f2, ..., tm-1 * fm <= tm * fm-1 F1 - F2 <= 0

So the original order is the best order.


#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <stack>
#include <assert.h>
#include <algorithm>
#include <math.h>
#include <ctime>
#include <functional>
#include <string.h>
#include <stdio.h>
#include <numeric>
#include <float.h>

using namespace std;



/*
4.6.5
*/

struct Rec {
	int time, cost, id; 
	Rec(int atime, int acost, int aid) : time(atime), cost(acost), id(aid) {}
};

bool camp(Rec r1, Rec r2) {
	return r1.time * r2.cost < r1.cost * r2.time;
}

int main() {
	int TC = 0; cin >> TC;
	bool blank = false;
	for (int tc = 1; tc <= TC; tc++) {
		int num = 0; cin >> num; 
		vector<Rec> recs; 
		for (int i = 0; i < num; i++) {
			int time, cost; cin >> time >> cost;
			recs.push_back(Rec(time, cost, i + 1)); 
		}

		stable_sort(recs.begin(), recs.end(), camp); 

		if (blank) {
			cout << endl; 
		}
		blank = true;
		for (int i = 0; i < recs.size(); i++) {
			if (i > 0) cout << " ";
			cout << recs[i].id;
		}
		cout << endl; 
	}
	return 0; 
}


【直通华为HCNA/HCNP系列S篇1】Sx7系列交换机主要特性及选型

本课程主要介绍华为最新一代Sx700系列交换机组成,各产品系列的特性、命名规则、主要机型、产品结构,以及各自主要应用等方面。这也是华为HCNA认证考试中的一个部分。
  • 2015年03月17日 22:05

110405 Shoemaker's Problem

#include using namespace std; class ShoeMaker { public: void Init(int index, int days, int punis...
  • JamesDoctor
  • JamesDoctor
  • 2013-10-15 12:06:47
  • 308

UVaOJ10026 - Shoemaker's Problem

10026 - Shoemaker's Problem Time limit: 3.000 seconds Shoemaker has N jobs (orders from custom...
  • chuan6099
  • chuan6099
  • 2013-05-23 19:20:57
  • 346

10026 - Shoemaker's Problem

贪心 #include #include #include #include using namespace std; const int maxn = 1000 + 7; int T, n...
  • THE___BEST
  • THE___BEST
  • 2016-01-23 14:32:53
  • 158

uva10026 - Shoemaker's Problem

题意: 鞋匠要完成一些任务,给你任务的完成时间t和惩罚s,让你求出怎样安排这些任务可以使惩罚更小。 思路: 贪心,感觉跟可拆背包的想法差不多,贪心决策是根据每个任务的s/t从大到小排序 代码如...
  • conatic
  • conatic
  • 2016-02-28 22:38:42
  • 117

Uva-10026 (Shoemaker's Problem)

题目链接: https://vjudge.net/problem/UVA-10026 #include #include #include #include using namespa...
  • qq_37753409
  • qq_37753409
  • 2018-01-13 23:25:02
  • 26

Shoemaker's Problem

http://www.bnuoj.com/bnuoj/problem_show.php?pid=18444 Shoemaker's Problem Time Limit: 3000ms Mem...
  • wqc359782004
  • wqc359782004
  • 2013-09-11 10:04:03
  • 621

uva10026 Shoemaker's Problem

也是简单题。 用高中
  • u011589125
  • u011589125
  • 2014-09-24 15:31:09
  • 308

UVA Shoemaker's Problem

Description  Shoemaker's Problem  Shoemaker has N jobs (orders fro...
  • yeguxin
  • yeguxin
  • 2015-02-02 16:05:39
  • 458

UVa10026 - Shoemaker's Problem

#include #include #define N 1010 int n; int t[N], s[N]; int num[N]; int cmp(const void *_p, con...
  • wuli2496
  • wuli2496
  • 2012-06-24 14:58:57
  • 642
收藏助手
不良信息举报
您举报文章:programming-challenges Shoemaker's Problem (110405) 题解
举报原因:
原因补充:

(最多只允许输入30个字)