您的位置:首页 > 其它

UVA 10026 Shoemaker's Problem

2014-07-15 15:41 489 查看



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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: