UVa 10026 - Shoemaker's Problem

链接:

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=967


类型: 贪心


原题:

Shoemaker has N jobs (orders from customers) which he must make. Shoemaker can work on only one job in each day. For each ithjob, 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 ithjob, 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

题目大意:
鞋匠有N个鞋子要修,对于第i双鞋子要修Ti天。顾客都希望自己的鞋子能最先修,但是没办法,鞋匠每次只能修一双鞋子。所以对于不是第一个修的鞋子,每拖延1天,鞋匠就要相应的赔偿Si块钱。求一个修鞋子的顺序,使得最后总个赔偿的价钱最少。

分析与总结:
典型的贪心问题。赔偿总费用与各个鞋子的修鞋天数和每天赔偿的钱数有关。前者是越少天越早修,后者是越多费用越早修,那么综合两个条件,用 fine/day 来进行排序即可。


代码:

/*
 * UVa: 10026 - Shoemaker's Problem
 * Result: Accept
 * Time: 0.008s
 * Author: D_Double
 */
#include<cstdio>
#include<algorithm>
using namespace std;

struct Node{
    int no;
    double day, fine;
    double price;
    friend bool operator <(const Node &a, const Node &b){
        if(a.price != b.price) return a.price>b.price;
        return a.no<b.no;
    }
}arr[1005];

int main(){
    int T, N;
    scanf("%d",&T);
    while(T--){
        scanf("%d",&N);
        for(int i=0; i<N; ++i){
            arr[i].no = i+1;
            scanf("%lf%lf", &arr[i].day, &arr[i].fine);
            arr[i].price = arr[i].fine/arr[i].day;
        }
        sort(arr, arr+N);
        for(int i=0; i<N; ++i)
            if(i) printf(" %d",arr[i].no);
            else printf("%d",arr[i].no);
        printf("\n");
        if(T) printf("\n");
    }
    return 0;
}

—— 生命的意义,在于赋予它意义。

原创http://blog.csdn.net/shuangde800By D_Double (转载请标明)


你可能感兴趣的:(Make)