Algorithm


Problem link : https://onlinejudge.org/index.php?option=onlinejudge&Itemid=8&page=show_problem&problem=967 

Shoemaker has N jobs (orders from customers) which he must make. Shoemaker can work on only one job in each day. For each i-th 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 i-th 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. 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. 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

Code Examples

#1 Code Example with C Programming

Code - C Programming

#include <bits/stdc++.h>
using namespace std;

int main() {
    int t,n,time,fine;
    cin >> t;
    while(t--){
        cin >> n;
        vector < tuple<int,int,int>> jobs;
        for(int i=0;i < n;i++){
            cin >> time >> fine;
            jobs.push_back(make_tuple(time,fine,i+1));
        }
        // greedy problem has sub-optimal solution
        // consider comparing two events, with all the other events being fixed
        // we compare them which gives us the less fine
        // if we choose to complete a first, the fine will be a.time*b.fine
        // if we choose to complete b first, the fine will be b.time*a.fine
        auto cmp = [](const tuple < int,int,int>& a, const tuple<int,int,int>& b){
            return (get<0>(a)*get<1>(b)) < (get<0>(b)*get<1>(a));
        };
        stable_sort(jobs.begin(),jobs.end(),cmp);
        for(auto& t : jobs){
            cout << get < 2>(t);
            if(&t == &jobs.back()) cout << endl;
            else cout << " ";
        }
        if(t) cout << endl;
    }
} 
Copy The Code & Try With Live Editor

Input

x
+
cmd
1
4
3 4
1 1000
2 2
5 5

Output

x
+
cmd
2 1 3 4
Advertisements

Demonstration


UVA Online Judge solution - 10026 - Shoemaker's Problem - UVA online judge solution in C,C++

Previous
UVA Online Judge solution - 10020 - Minimal coverage - UVA online judge solution in C,C++,Java
Next
UVA Online Judge solution - 10029 - Edit Step Ladders - UVA online judge solution in C,C++,Java