uva 10026 - Shoemaker's Problem

19 篇文章 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 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

方法 :
本题 题意为一个鞋匠有n件工作要做,每件工作都有其消耗时间和推辞一天开始做该工作所付出的代价。
求 做这n件工作的顺序,使得总的代价最小。
假设 只有两件工作,分别为J1和J2它们消耗的时间和推辞一天开始的代价分别为T1,F1和T2,F2.
若 F1/T1 < F2/T2,则T2 * F1 < T1 * F2 ,其中T2 * F1是J1先开始的代价,T1 * F2是J2先开始的代价。
所以 要使代价最小,应该J2先开始。
反之 ,应使J1先开始。
若 有n > 2,若Fi/Ti < Fi+1/Ti+1,则交换Ji和Ji+1的顺序就可使总代价变小则可采用插入排序的思想,所以以F/T大小按降序排序,就可得到所求的序列。
代码 :
#include 
    
    
     
     
#define N 1000
typedef struct job job;
struct job{
    int no;
    double p;
};
void insertsort(job *job_arr_p[], int n)
{
    int i, j;
    job *t;
    for(i = 0; i < n; i++){
	t = job_arr_p[i];
	for(j = i-1; j >= 0 && job_arr_p[j]->p < t->p; j--)
	    job_arr_p[j+1] = job_arr_p[j];
	job_arr_p[j+1] = t;
    }
}
int main()
{
    int i, n, t;
    double time, fine;
    job job_arr[N], *job_arr_p[N];
    scanf("%d", &t);
    while(t-- >0){
	scanf("%d", &n);
	for(i = 0; i < n; i++)
	    job_arr_p[i] = job_arr + i;
	for(i = 0; i < n; i++){
	    scanf("%lf %lf", &time, &fine);
	    job_arr[i].p = fine/time;
	    job_arr[i].no = i + 1;
	}
	insertsort(job_arr_p, n);
	for(i = 0; i < n; i++)
	    printf("%d%s", job_arr_p[i]->no, i == n-1 ? "\n" : " ");
	if(t != 0)
	    printf("\n");
    }
    return 0;
}

    
    

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值