UVA 10026 Shoemaker's Problem 鞋匠的难题 贪心+排序
题意:鞋匠一口气接到了不少生意,但是做鞋需要时间,鞋匠只能一双一双地做,根据协议每笔生意如果拖延了要罚钱。
给出每笔生意需要的天数和每天的罚钱数,求出最小罚钱的排列顺序。
只要按罚款/天数去从大到小排序,如果比例一样就按序号排序(要求字典序)。
解释我就不献丑了,附上Staginner大神的证明:
对于为什么贪心策略是这个样子的,我们不妨拿相邻的两个事件a、b来说明一下。由于a、b之后的事件是固定的,所以我们无论排成ab还是排成ba后面部分的损失都是固定的,那么损失的差别主要来源于究竟是排成ab还是排b成a。排ab的损失为ta*fb,排ba的损失为tb*fa,那么如果ta*fb<tb*fa,我们就排成ab,这样可以得到fa/ta>fb/tb,推而广之,就得到了我们的贪心策略。
代码:
/*
* Author: illuz <iilluzen@gmail.com>
* Blog: http://blog.csdn.net/hcbbt
* File: uva10026.cpp
* Lauguage: C/C++
* Create Date: 2013-08-25 20:11:52
* Descripton: UVA 10026 Shoemaker's Problem, sort + struct
*/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <iostream>
#include <list>
#include <vector>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <utility>
#include <algorithm>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define repu(i, a, b) for (int i = (a); i < (b); i++)
#define repf(i, a, b) for (int i = (a); i <= (b); i++)
#define repd(i, a, b) for (int i = (a); i >= (b); i--)
#define swap(a, b) {int t = a; a = b; b = t;}
#define mc(a) memset(a, 0, sizeof(a))
#define ms(a, i) memset(a, i, sizeof(a))
#define sqr(x) ((x) * (x))
#define FI(i, x) for (typeof((x).begin()) i = (x).begin(); i != (x).end(); i++)
typedef long long LL;
typedef unsigned long long ULL; /****** TEMPLATE ENDS ******/ const int MAXN = 1002;
struct Deal {
int no;
double pri;
int day;
friend bool operator < (const Deal& a, const Deal& b) {
if (a.pri != b.pri) return a.pri > b.pri;
else return a.no < b.no;
}
} d[MAXN]; int main() {
int t, n, m;
scanf("%d", &t);
while (t--) {
scanf("%d", &n);
rep(i, n) {
d[i].no = i + 1;
scanf("%d%d", &d[i].day, &m);
d[i].pri = (double)m / (double)d[i].day;
};
sort(d, d + n);
printf("%d", d[0].no);
repu(i, 1, n) printf(" %d", d[i].no);
printf("\n");
if (t) printf("\n");
}
return 0;
}
UVA 10026 Shoemaker's Problem 鞋匠的难题 贪心+排序的更多相关文章
- uva 10026 Shoemaker's Problem(排序)
题目连接:10026 Shoemaker's Problem 题目大意:有一个鞋匠接了n双要修的鞋子, 修每双鞋需要d天,每推迟一天修将亏损val元,问按什么样的顺序修鞋可以保证损失最少,如果有多种情 ...
- uva 10026 Shoemaker's Problem
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...
- uva 10026 Shoemaker's Problem _贪心
题意:鞋匠现在有n个工作要做,每个工作要x天,没延迟一天需要付款y,鞋匠每天只能做一个工作,问使得鞋匠最少赔款的工作顺序. 思路:工作和工作之间排序,如果a.value*b.day>b.valu ...
- (贪心5.2.1)UVA 10026 Shoemaker's Problem(利用数据有序化来进行贪心选择)
/* * UVA_10026.cpp * * Created on: 2013年10月10日 * Author: Administrator */ #include <iostream> ...
- UVA 10026 Shoemaker's Problem
Shoemaker's Problem Shoemaker has N jobs (orders from customers) which he must make. Shoemaker can w ...
- UVa 101 The Blocks Problem Vector基本操作
UVa 101 The Blocks Problem 一道纯模拟题 The Problem The problem is to parse a series of commands that inst ...
- 【暑假】[深入动态规划]UVa 1380 A Scheduling Problem
UVa 1380 A Scheduling Problem 题目: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=41557 ...
- UVA - 524 Prime Ring Problem(dfs回溯法)
UVA - 524 Prime Ring Problem Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & % ...
- uva 10837 - A Research Problem(欧拉功能+暴力)
题目链接:uva 10837 - A Research Problem 题目大意:给定一个phin.要求一个最小的n.欧拉函数n等于phin 解题思路:欧拉函数性质有,p为素数的话有phip=p−1; ...
随机推荐
- ThinkPHP使用纯真IP获取物理地址时中文乱码问题
今天在用ThinkPHP通过纯真IP获取地址时,发现输出结果中文乱码,如图: 经查发现ThinkPHP的IpLocation.class.php类文件中说明:“由于使用UTF8编码 如果使用纯真IP地 ...
- react-native开发总结
项目地址:http://liu12fei08fei.github.io/blog/41react-native.html 说明 • 项目总结代码地址 • 从项目开始启动(2018-07-02)到项目进 ...
- js比较两个String字符串找出不同,并将不同处高亮显示
根据java代码改写成js,下边js文件代码: function StringBuffer() { this.__strings__ = []; }; StringBuffer.prototype.a ...
- Html.BeginForm() vs Ajax.BeginForm() in MVC3
我们知道,BeginForm()方法能创建一个Form标签,因此可以结合表单级的方法,在这个页面中.我一直在考虑Html.BeginForm()方法和Ajax.BeginForm()方法在MVC3中有 ...
- “CObject::operator =”: 无法访问 private 成员(在“CObject”类中声明)
c++工程编译报错: “CObject::operator =”: 无法访问 private 成员(在“CObject”类中声明) 错误无法直接定位源码位置,网上搜索了,也和我的代码不一样. 最后还是 ...
- c++中两个类相互包含引用的相关问题
在构造自己的类时,可能会遇到两个类相互引用的问题. 例如: class A { int i; B b; }; class B { int i; A a; }; 在这种情况下,这样就会出现一个死循环a. ...
- Windows操作系统下的MySQL主从复制及读写分离
一.主服务器(master)配置 1.修改MySQL配置文件my.ini [mysqld] log-bin=mysql-binlog-bin-index=mysql-bin.indexserver-i ...
- #pragma mark 添加分割线 及 其它类似标记 - 转
#pragma marks Comments containing: MARK: TODO: FIXME: !!!: ???: 除了使用 #pragma mark -添加分割线之外, 以上几种标记均可 ...
- Available Date 相关
Available Date 写错了怎么办? http://www.cocoachina.com/bbs/read.php?tid=7224&page=1现在好像不需要改那个availa ...
- td顶部对齐
<td width=568 colspan=3 valign="top" style='width:426.1pt;border:none; border-bottom:so ...