UVA 10026 (13.11.08)

 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 nextN lines each contain two numbers: the time and fine of eachtask 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个任务要做, 当他做一个任务时, 其他的任务肯定是做不了的, 所以要赔钱, 现在我们要求的就是按什么顺序做这些任务, 赔钱最少~?

思路: 我们任意拿出两个任务来讲, 我们肯定要对比一下, 做哪个任务可以赔钱更少, 即Vi * Tj > Vj * Ti, 由此公式判断我们先做哪一个任务好.

做法: 将公式写成cmp比较函数, 根据这个sort一下排序就OK, 然后要注意就是, 代价一样的时候, 按任务编号从小到大输出, 在cmp中加上一个条件就好~


AC代码:

#include<stdio.h>
#include<algorithm>

using namespace std;

struct Task {
    int t;
    int v;
    int num;
} task[1005];

int cmp(Task a, Task b) {
    if(a.t * b.v != b.t * a.v)
        return a.t * b.v < b.t * a.v;
    else
        return a.num < b.num;
}

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值