Shoemaker's Problem

Shoemaker has N jobs (orders from customers) which he must make. Shoemaker can work on only one job in each day. For each ith job, it is known the integer Ti (1<=Ti<=1000), the time in days it takes the shoemaker to finish
the job. For each day of delay before starting to work for the ith job, shoemaker must pay a fine of Si (1<=Si<=10000) cents. Your task is to help the shoemaker, writing a programm to find the sequence of jobs with minimal
total fine.

The Input

The input begins with a single positive integer on a line by itself indicating the number of the cases following, each of them as described below. This line is followed by a blank line, and there is also a blank line between two consecutive inputs.

First line of input contains an integer N (1<=N<=1000). The next N lines each contain two numbers: the time and fine of each task in order.

The Output

For each test case, the output must follow the description below. The outputs of two consecutive cases will be separated by a blank line.

You programm should print the sequence of jobs with minimal fine. Each job should be represented by its number in input. All integers should be placed on only one output line and separated by one space. If multiple solutions are possible, print the first
lexicographically.

Sample Input

1

4
3 4
1 1000
2 2
5 5

Sample Output

2 1 3 4

题意:一个鞋匠接到非常多订单。可是,每一个客户都觉得自己的订单应该被立即处理。因此,对于第i个订单。在開始处理这个订单之前,每天都要付罚金Si (1<=Si≤<=1000)。而他一天仅仅能处理一个订单。并且一个订单可能须要非常多天才干完毕。对于第i个订单。整数Ti (1<=Ti<=1000)代表处理完毕这个订单所须要的天数。

求所付罚金最少的订单处理顺序。

分析:贪心解决。对每一个订单 罚金/天数 从大到小排序。结果一样按序号排序(保证字典序最小)。

证明:假设x和y为排好的顺序中相邻的两个订单,因为x、y之后的订单顺序是固定的。所以不管是排成xy还是排成yx,对后面的罚金没有影响。罚金区别在于是排成xy还是yx。

假设是xy,则罚金为Tx*Sy;假设是yx,则罚金是Ty*Sx。

假设Tx*Sy<Ty*Sx,就排成xy;否则排成yx。

所以这样的贪心策略是正确的。

#include<cstdio>
#include<algorithm>
using namespace std; struct shoe {
int id;
int time;
int fine;
} a[1005]; bool comp(shoe x, shoe y) { //用乘法比較。避免相除以后浮点数产生误差
if(x.fine * y.time != x.time * y.fine)
return x.fine * y.time > x.time * y.fine;
return x.id < y.id;
} int main()
{
int T, n, i;
scanf("%d",&T);
while(T--) {
scanf("%d",&n);
for(i = 0; i < n; i++) {
scanf("%d%d",&a[i].time, &a[i].fine);
a[i].id = i + 1;
}
sort(a, a+n, comp);
for(i = 0; i < n - 1; i++)
printf("%d ", a[i].id);
printf("%d\n", a[n-1].id);
if(T > 0) printf("\n");
}
return 0;
}

UVA 10026 Shoemaker&#39;s Problem的更多相关文章

  1. UVA 10026 Shoemaker's Problem 鞋匠的难题 贪心+排序

    题意:鞋匠一口气接到了不少生意,但是做鞋需要时间,鞋匠只能一双一双地做,根据协议每笔生意如果拖延了要罚钱. 给出每笔生意需要的天数和每天的罚钱数,求出最小罚钱的排列顺序. 只要按罚款/天数去从大到小排 ...

  2. uva 10026 Shoemaker's Problem(排序)

    题目连接:10026 Shoemaker's Problem 题目大意:有一个鞋匠接了n双要修的鞋子, 修每双鞋需要d天,每推迟一天修将亏损val元,问按什么样的顺序修鞋可以保证损失最少,如果有多种情 ...

  3. uva 10026 Shoemaker's Problem

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...

  4. (贪心5.2.1)UVA 10026 Shoemaker's Problem(利用数据有序化来进行贪心选择)

    /* * UVA_10026.cpp * * Created on: 2013年10月10日 * Author: Administrator */ #include <iostream> ...

  5. uva 10026 Shoemaker's Problem _贪心

    题意:鞋匠现在有n个工作要做,每个工作要x天,没延迟一天需要付款y,鞋匠每天只能做一个工作,问使得鞋匠最少赔款的工作顺序. 思路:工作和工作之间排序,如果a.value*b.day>b.valu ...

  6. programming-challenges Shoemaker&#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, ..., ...

  7. UVA 10245 The Closest Pair Problem 最近点问题 分治算法

    题意,给出n个点的坐标,找出两点间最近的距离,如果小于10000就输出INFINITY. 纯暴力是会超时的,所以得另辟蹊径,用分治算法. 递归思路将点按坐标排序后,分成两块处理,最近的距离不是在两块中 ...

  8. uva 10026 Problem C: Edit Step Ladders

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...

  9. UVA - 10239 The Book-shelver&#39;s Problem

    Description Problem D The Book-shelver's Problem Input: standard input Output: standard output Time ...

随机推荐

  1. Python全栈开发之14、Javascript

    一.简介 前面我们学习了html和css,但是我们写的网页不能动起来,如果我们需要网页出现各种效果,那么我们就要学习一门新的语言了,那就是JavaScript,JavaScript是世界上最流行的脚本 ...

  2. Django实战(6):对比RoR和Django的模板系统

    scaffold的生成物虽然用处不大,但是给我们带来一些最佳实践.其中就有模板的继承和分区. 如果你深入使用过rails的模板体系,那么恭喜你:你有超强的忍耐力!而且更重要的是,你只需要3分钟就可以理 ...

  3. idea 设置svn忽略 .idea内的文件

    从ToolView中打开 Version Control  -> Local Changes  中的Default ChangesList里面的文件全选中右键选择 Move to  Anothe ...

  4. 02:实现Singleton模式

    Java实现单例模式有很多种实现方法,其中我们应根据需要选择线程安全的与非线程安全的两种方式,根据对象实现的方式又分为饱汉与饿汉方式. 这里使用java中的volatile关键字与synchroniz ...

  5. thinkphp3.2链接数据库常用的配置选项

    thinkphp3.2常用配置选项,app/Common/Conf/config.php 或者 app/Home/Conf/config.php 1 2 3 4 5 6 7 8 'SHOW_PAGE_ ...

  6. 【记录】url 中出现特殊字符该怎么办

    url中出现特殊字符+ URL 中+号表示空格 %2B 空格 URL中的空格可以用+号或者编码 %20/ 分隔目录和子目录 %2F ? 分隔实际的URL和参数 %3F % 指定特殊字符 %25 # 表 ...

  7. HDU - 1051 Wooden Sticks 贪心 动态规划

    Wooden Sticks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)    ...

  8. 字符串hash与字典树

    title: 字符串hash与字典树 date: 2018-08-01 22:05:29 tags: acm 算法 字符串 概述 这篇主要是关于字符串里的 字符串hash 和 字符串字典树,,两个都是 ...

  9. 超实用 Git 使用方式介绍

    都说程序员若是不知道 GitHub 就不是一个合格的程序员,其实这话说的过分了,不知道就学嘛,今天我们就来说说 Git 和 GitHub 到底是什么. 我们在开发软件的时候,常常是需要多人协作完成,这 ...

  10. 历史文章分类汇总-Anaconda安装第三方包(whl文件)

    本文主要是对公众号之前发布的文章进行分类整理,方面大家查阅,以后会不定期对文章汇总进行更新与发布.   一.推荐阅读: Anaconda安装第三方包(whl文件) 福布斯系列之数据分析思路篇 福布斯系 ...