Link:
Http://uva.onlinejudge.org/index.php? Option = com_onlinejudge & Itemid = 8 & category = 113 & page = show_problem & problem = 967
Type: greedy
Original question:
Shoemaker has n jobs (orders from MERs mers) 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 shocould print the sequence of jobs with minimal fine. Each job shocould be represented by its number in input. All integers shocould be placed on only one output line and separated by one space.
If multiple solutions are possible, print the first lexicographically.
Sample Input
143 41 10002 25 5
Sample output
2 1 3 4
Question:The shoemaker has n shoes to be repaired, and Ti day for the I-th pair of shoes. Customers want their shoes to be repaired first, but there is no way. Each time, the shoemaker can only repair one pair of shoes. Therefore, for a shoe that is not the first to be repaired, the shoemaker must compensate Si yuan for each delay of one day. In the order of shoe repair, the price of the last total compensation is the least.
Analysis and Summary:Typical greedy problems. The total compensation cost is related to the number of shoe repair days and the amount of daily compensation. The smaller the number of days, the earlier the repair, and the earlier the cost, the more fine/day is used to sort the two conditions.
Code:
/* * UVa: 10026 - Shoemaker's Problem * Result: Accept * Time: 0.008s * Author: D_Double */#include<cstdio>#include<algorithm>using namespace std;struct Node{ int no; double day, fine; double price; friend bool operator <(const Node &a, const Node &b){ if(a.price != b.price) return a.price>b.price; return a.no<b.no; }}arr[1005];int main(){ int T, N; scanf("%d",&T); while(T--){ scanf("%d",&N); for(int i=0; i<N; ++i){ arr[i].no = i+1; scanf("%lf%lf", &arr[i].day, &arr[i].fine); arr[i].price = arr[i].fine/arr[i].day; } sort(arr, arr+N); for(int i=0; i<N; ++i) if(i) printf(" %d",arr[i].no); else printf("%d",arr[i].no); printf("\n"); if(T) printf("\n"); } return 0;}
-- The meaning of life is to give it meaning.
Original
Http://blog.csdn.net/shuangde800
, By d_double (reprinted, please mark)