码迷,mamicode.com
首页 > 其他好文 > 详细

UVA 10026-Shoemaker’s problem(贪心)

时间:2015-02-05 11:15:24      阅读:139      评论:0      收藏:0      [点我收藏+]

标签:

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 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 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)代表处理完成这个订单所需要的天数。求所付罚金最少的订单处理顺序

思路:因为鞋匠时同时接到所有订单,起始罚款的日期相同,贪心fine/day ,就是鞋匠一天可以减少的最大损失,稳定排序即可。

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#include <set>
#include <queue>
#include <stack>
#include <map>
using namespace std;
struct node
{
    int id;
    double t,f;
}q[1010];

int cmp(struct node a,struct node b)
{
    if(fabs((a.f/a.t)-(b.f/b.t)>0.00001))
        return (a.f/a.t)>(b.f/b.t);
        return a.id<b.id;
}

int main()
{
    int T,n,i;
    scanf("%d",&T);
    while(T--){
        scanf("%d",&n);
        for(i=0;i<n;i++){
            scanf("%lf %lf",&q[i].t,&q[i].f);
            q[i].id=i+1;
        }
        sort(q,q+n,cmp);
        for(i=0;i<n-1;i++)
            printf("%d ",q[i].id);
        printf("%d\n",q[i].id);
        if(T)
            printf("\n");
    }
    return 0;
}


UVA 10026-Shoemaker’s problem(贪心)

标签:

原文地址:http://blog.csdn.net/u013486414/article/details/43525179

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!