[贪心]Shoemaker's Problem uva10026



 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

Alex Gevak
September 16, 2000(Revised 4-10-00, Antonio Sanchez)

题意:

鞋匠有N件工作要完成。他每天只能做一件工作。并且他知道这些工作分别要几个工作天才能完成。另外,他也知道每个工作被延误一天所需被罚的罚金。延误的天数为从今天算起到该工作开始的那天(所以只有第一件工作没有罚金)。

以第一组测试资料为例说明:若工作的顺序是1 2 3 4,那罚金为:0*4+1000*3+4*2+6*5=3038。若工作的顺序是2 1 3 4,则罚金为:0*1000+1*4+4*2+6*5=42。所以第二种工作顺序的罚金较少(事实上也是最少)。

你的任务是写一个程式帮助鞋匠找出完成这N个工作的先后顺序,使得罚金最少。

解法:很明显的一道贪心的题目,找到最佳方案。

#include<iostream>
#include<algorithm>

using namespace std;

class node
{
public:
    int time,val,num;
    double ave;
}work[1010];

bool cmp(node x,node y)
{
    if(x.ave!=y.ave) return x.ave>y.ave;
    else return x.num<y.num;
}

int main()
{
    int n;
    cin>>n;
    while(n--)
    {
        int m;
        cin>>m;
        for(int i=0;i<m;i++)
        {
            cin>>work[i].time>>work[i].val;
            work[i].num=i+1;
            work[i].ave=(work[i].val*1.0)/work[i].time;
        }
        sort(work,work+m,cmp);
        cout<<work[0].num;
        for(int i=1;i<m;i++)
            cout<<" "<<work[i].num;
        cout<<endl;
        if(n) cout<<endl;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值