UVA 10026 Shoemaker's Problem

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's Problem

时间: 2024-10-13 04:04:12

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

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]

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

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

Description Problem D The Book-shelver's Problem Input: standard input Output: standard output Time Limit: 5 seconds Memory Limit: 32 MB You are given a collection of books, which must be shelved in a library bookcase ordered (from top to bottom in t

UVa 729 - The Hamming Distance Problem

题目:构造n位01串,其中有m个1的所有组合. 分析:搜索.枚举.可以利用库函数,求解,也可以利用dfs求解:我这里采用位运算计算组合数. 说明:注意库啊! #include <iostream> #include <cstdlib> #include <cstdio> using namespace std; int S[20]; int main() { int T,N,M; while ( cin >> T ) for ( int t = 1 ; t

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

Description Problem D The Book-shelver's Problem Input: standard input Output: standard output Time Limit: 5 seconds Memory Limit: 32 MB You are given a collection of books, which must be shelved in a library bookcase ordered (from top to bottom in t

uva 10245 The Closest Pair Problem (暴力+剪枝)

uva 10245 The Closest Pair Problem 题目大意:给出n个点,求出距离最近的两点间的距离.若点与点间的距离都大于10000,输出INFINITY 解题思路:这题的正统做法是分治,偷懒方法是暴力加剪枝.先按x坐标排序,然后fabs(p[i] - p[j]) > ans的点就可以直接跳过了. #include<stdio.h> #include<string.h> #include<stdlib.h> #include<algori

UVA 100 The 3n + 1 problem(超级大水题)

The 3n + 1 problem Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Description Problems in Computer Science are often classified as belonging to a certain class of problems (e.g., NP, Unsolvable, Recursive). In this problem you

uva 10026 Problem C: Edit Step Ladders

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=970 通过对每一个字符串,每一个位置进行枚举三个操作,然后二分查找操作后的字符串是否存在,dp记录. 1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #define N 25