UVA10026 - Shoemaker's Problem

 

 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个修鞋任务需要完成,但是每次只能完成一个任务,对于还未开始的任务,每拖延一天,将罚相应的金额,怎样安排任务顺序,使得罚款总数最少。
题解:贪心问题。假设有两个任务,两个任务相应的时间和罚款分别为(x1,y1)和(x2,y2),如果第一个任务先完成,则罚款数为x1*y2,如果先完成第二个任务,则罚款数为x2*y1。比较两种方案的罚款数即可,我们显然会选择罚款数少的方案。刚开始用选择排序,果断WA了,换成冒泡排序就AC了。。。因为选择排序不是稳定的排序,不能保证方案是字典序。
 
View Code
 1 #include<stdio.h>
 2 #define MAXSN 1005
 3 typedef struct
 4 {
 5     int x;
 6     int y;
 7     int z;
 8 } node;
 9 int main(void)
10 {
11     node s[MAXSN],temp;
12     int i,j,m,n;
13     long ans1,ans2;
14     scanf("%d",&n);
15     while(n--)
16     {
17         scanf("%d",&m);
18         for(i=0; i<m; i++)
19         {
20             scanf("%d%d",&s[i].x,&s[i].y);
21             s[i].z=i+1;
22         }
23         for(i=m-2; i>=0; i--)
24             for(j=0; j<=i; j++)
25             {
26                 ans1=s[j].x*s[j+1].y;
27                 ans2=s[j].y*s[j+1].x;
28 
29                 if(ans1>ans2)
30                 {
31                     temp=s[j];
32                     s[j]=s[j+1];
33                     s[j+1]=temp;
34                 }
35             }
36         for(i=0; i<m-1; i++)
37             printf("%d ",s[i].z);
38         printf("%d\n",s[i].z);
39         if(n) printf("\n");
40     }
41     return 0;
42 }

 

 

posted on 2013-03-07 02:12  仗剑奔走天涯  阅读(237)  评论(0编辑  收藏  举报

导航