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

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;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

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

时间: 2024-08-29 07:14:11

programming-challenges Shoemaker's Problem (110405) 题解的相关文章

programming-challenges Shoemaker&amp;#39;s Problem (110405) 题解

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

[题解]UVA10026 Shoemaker&#39;s Problem

链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=967 描述:要做n双鞋子,第 i 双鞋子要做Ti天,每天消耗Si的钱(当前正在做第 i 双鞋子时不耗钱).求在最少消耗钱的情况下做鞋子的顺序. 思路:贪心 明显是一个排序的模型,然后我们就思考顺序怎么确定.考虑当前两双鞋子a和b,它们的顺序对它们前后的鞋子都没有影响,它们所创造的影响仅仅

uva 10026 Shoemaker&#39;s Problem(贪心+排序)

虽然是个水题,但是在一些细节上wa了几次,好像不支持'\b'退格符号,我用在了输出空格那,结果wa了...白白 wa了几次...题意是看的题解..今天只写了两道题,速度有点慢,得加快了,以后得先认真读懂题目,题目读懂了 就相当于做出来一半然后仔细动脑想想,有想法了再敲,不能盲目的做题.另外,热烈祝贺今天c++ primer看到 了100页 思路: 这道题是让给的数据是每件工作需要做的天数和每耽误一天所需要的费用,让求一个序列使得付费最小,如果有相同答 案把字典树最小的输出...输出的是序号,该件

UVa 10026 - Shoemaker&#39;s Problem

题目:一个鞋匠接到很多任务,每个任务有一个完成需要时间,延期的任务每天会有一个罚款, 现在所有任务都延期了,问最少的罚款是多少(每天只能做一种工作). 分析:贪心.转化问题为,如果不做则罚款的数额是days*Σ(每个任务罚款): 而每做好一个就认为从这一天开始以后每天都会获得相应的罚款的收益: 求收益的最大值即可,这里按性介比排序即可. 命题:按性介比排序受益最大. 证明:价值为p[i],耗时为c[i]则: 1.如果只有两个任务则有p[i]/c[i] > p[j]/c[j]  ->  p[i]

uva10026 - Shoemaker&#39;s Problem(贪心)

题目:10026 - Shoemaker's Problem 题目大意:有个鞋匠在同一天接到了一堆的生意.可是他每天只能做一双鞋,给出做每双鞋需要的时间和推辞做鞋的赔偿.问怎样合理的分配才能使得赔偿最小. 解题思路:鞋子编号  要花的时间  需要的赔偿(每天) 1                    1                100 2                    5                  4 这样的两双鞋有两种安排: 做完1再做2      赔偿  1 * 4:

Light OJ 1004 - Monkey Banana Problem dp题解

1004 - Monkey Banana Problem PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB You are in the world of mathematics to solve the great "Monkey Banana Problem". It states that, a monkey enters into a diamond shaped two dim

Lintcode1 A+B Problem solution 题解

[题目描述] Write a function that add two numbers A and B. You should not use + or any arithmetic operators. Notice:There is no need to read data from standard input stream. Both parameters are given in function aplusb, you job is to calculate the sum and

[HAOI2011]Problem b 题解

题目大意: 对于给出的n个询问,每次求有多少个数对(x,y),满足a≤x≤b,c≤y≤d,且gcd(x,y)=k. 思路: 设f(k)为当1≤x≤n,1≤y≤m,且n≤m,使gcd(x,y)=k的数对(x,y)的对数,g(k)为当1≤x≤n,1≤y≤m,且n≤m,使k|gcd(x,y)的数对(x,y)的对数.则,莫比乌斯反演,得.和会有连续的一段相同且相同的为一定连续的一段,可证最多有2√n和2√m段,分块处理,对于每个询问可O(√n)解决. 代码: 1 #include<cstdio> 2

HDU1402:A * B Problem Plus——题解

http://acm.hdu.edu.cn/showproblem.php?pid=1402 给出两个高精度正整数,求它们的积,最长的数长度不大于5e4. FFT裸题,将每个数位看做是多项式的系数即可. 我们最后就是要求出两个多项式相乘的系数. #include<algorithm> #include<iostream> #include<cstring> #include<cstdio> #include<cmath> using namesp