Simply sort will do the job. Given to jobs, j1 and j2. which should be done first? Do j1 first will cause a total fine to j2 of j1.time * j2.fine and do j2 first will cause a total fine to j1 of j2.time * j1.fine.
Code:
- /*************************************************************************
- * Copyright (C) 2008 by liukaipeng *
- * liukaipeng at gmail dot com *
- *************************************************************************/
- /* @JUDGE_ID 00000 10026 C++ "Shoemaker's Problem" */
- #include <algorithm>
- #include <deque>
- #include <fstream>
- #include <iostream>
- #include <limits>
- #include <list>
- #include <map>
- #include <queue>
- #include <set>
- #include <stack>
- #include <string>
- #include <vector>
- using namespace std;
- int const jobcount = 1000;
- struct job
- {
- int id;
- int time;
- int fine;
- };
- struct jobcmp
- {
- bool operator()(job const& j1, job const& j2)
- {
- return j1.time * j2.fine < j2.time * j1.fine;
- }
- };
- int main(int argc, char *argv[])
- {
- #ifndef ONLINE_JUDGE
- filebuf in, out;
- cin.rdbuf(in.open((string(argv[0]) + ".in").c_str(), ios_base::in));
- cout.rdbuf(out.open((string(argv[0]) + ".out").c_str(), ios_base::out));
- #endif
- int ncases;
- cin >> ncases;
- while (ncases-- > 0) {
- int njobs;
- cin >> njobs;
- job jobs[jobcount];
- for (int i = 0; i < njobs; ++i) {
- jobs[i].id = i + 1;
- cin >> jobs[i].time >> jobs[i].fine;
- }
- stable_sort(jobs, jobs + njobs, jobcmp());
- for (int i = 0; i < njobs; ++i) {
- cout << (i > 0 ? " " : "") << jobs[i].id;
- }
- cout << '/n';
- if (ncases > 0) cout << '/n';
- }
- return 0;
- }