Uva--10026(贪心)
2014-07-23 20:54:44
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
思路:经典贪心,仔细分析就可知道,在决定某两个Job(如a,b)谁排在前面的时候,判断ta * fb和tb * fa的大小即可(一样大的看序号),以这个条件写出cmp函数,排序即可。
1 /************************************************************************* 2 > File Name: Uva10700.cpp 3 > Author: Nature 4 > Mail: 564374850@qq.com 5 > Created Time: Wed 23 Jul 2014 12:39:59 AM CST 6 ************************************************************************/ 7 8 #include <cstdio> 9 #include <cstring> 10 #include <cstdlib> 11 #include <cmath> 12 #include <iostream> 13 #include <algorithm> 14 using namespace std; 15 16 struct Job{ 17 int id,t,f; 18 }j[1005]; 19 20 bool cmp(Job a,Job b){ 21 int k1 = a.t * b.f; 22 int k2 = b.t * a.f; 23 if(k1 == k2) 24 return a.id < b.id; 25 else 26 return k1 < k2; 27 } 28 29 int main(){ 30 int Case,n; 31 scanf("%d",&Case); 32 while(Case--){ 33 scanf("%d",&n); 34 for(int i = 0; i < n; ++i){ 35 j[i].id = i; 36 scanf("%d%d",&j[i].t,&j[i].f); 37 } 38 sort(j,j + n,cmp); 39 printf("%d",j[0].id + 1); 40 for(int i = 1; i < n; ++i) 41 printf(" %d",j[i].id + 1); 42 printf("\n"); 43 if(Case) 44 printf("\n"); 45 } 46 return 0; 47 }