Friday, March 18, 2011


Problem Links:

poj, uva10026,


 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


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)


Greedy + Sort.

Source Code:

//Fri Mar 18 02:32:09 CDT 2011
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>

using namespace std;

class Job
    int ID;
    int times;
    int fines;

    Job(int id, int t, int f)
        this->ID = id;
        this->times = t;
        this->fines = f;

    static bool cmp(const Job &A, const Job &B)
        if (A.fines * B.times > A.times * B.fines) return true;
        if (A.fines * B.times == A.times * B.fines) return A.ID < B.ID;
        return false;

void initialize(int N, vector<Job> &v)
    for (int i = 0; i < N; i++)
        int t, f;
        cin >> t >> f;
        v.push_back(Job(i + 1, t, f));

void solve(int N, vector<Job> v)
    std::sort(v.begin(), v.end(), Job::cmp);
    cout << v[0].ID;
    for (int i = 1; i < N; i++)
        cout << " " << v[i].ID;
    cout << endl;

int main(int argc, char* argv[])
    //freopen("", "r", stdin);
    //freopen("output.out", "w", stdout);
    int T;
    cin >> T;
    for (int k = 0; k < T; k++)
        if (k > 0) cout << endl;
        int N;
        cin >> N;
        vector<Job> maker;
        initialize(N, maker);
        solve(N, maker);
    return 0;

No comments :