uva 10026 Shoemaker's Problem (贪心)

来源:互联网 发布:qt4.7.4下载 windows 编辑:程序博客网 时间:2024/05/16 10:23

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



贪心,感觉见过,但忘了哪见的的了……应该是hdu上
相当于求最优总时间吧,按照fine/time排序,使得每天做的工作是理论上扣费最多的一件…………额应该就是这意思吧……



找了个大哥的证明(:зゝ∠)

解释我就不献丑了,附上Staginner大神的证明:

对于为什么贪心策略是这个样子的,我们不妨拿相邻的两个事件a、b来说明一下。由于a、b之后的事件是固定的,所以我们无论排成ab还是排成ba后面部分的损失都是固定的,那么损失的差别主要来源于究竟是排成ab还是排b成a。排ab的损失为ta*fb,排ba的损失为tb*fa,那么如果ta*fb

#include<cstdio>#include<iostream>#include<cstring>#include<algorithm>using namespace std;struct why{    int ti,fi,in;};why f[10000];int t,n;bool cmp(why a,why b){    return (((a.fi*1.0)/a.ti)>((b.fi*1.0)/b.ti));}int main(){    scanf("%d",&t);    for (int q=1;q<=t;q++)    {        if (q!=1)            printf("\n");        scanf("%d",&n);        for (int i=1;i<=n;i++)        {            scanf("%d%d",&f[i].ti,&f[i].fi);            f[i].in=i;        }           sort(f+1,f+n+1,cmp);        for (int i=1;i<=n;i++)            if (i==1)                printf("%d",f[i].in);            else                printf(" %d",f[i].in);        printf("\n");    }}
0 0
原创粉丝点击