UVa Problem Solution: 10026 - Shoemaker's Problem


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:
  1. /*************************************************************************
  2.  * Copyright (C) 2008 by liukaipeng                                      *
  3.  * liukaipeng at gmail dot com                                           *
  4.  *************************************************************************/
  5. /* @JUDGE_ID 00000 10026 C++ "Shoemaker's Problem" */
  6. #include <algorithm>
  7. #include <deque>
  8. #include <fstream>
  9. #include <iostream>
  10. #include <limits>
  11. #include <list>
  12. #include <map>
  13. #include <queue>
  14. #include <set>
  15. #include <stack>
  16. #include <string>
  17. #include <vector>
  18. using namespace std;
  19.      
  20. int const jobcount = 1000;
  21.           
  22. struct job
  23. {
  24.   int id;
  25.   int time;
  26.   int fine;
  27. };
  28. struct jobcmp
  29. {
  30.   bool operator()(job const& j1, job const& j2)
  31.   {
  32.     return j1.time * j2.fine < j2.time * j1.fine;
  33.   }
  34. };
  35. int main(int argc, char *argv[])
  36. {
  37. #ifndef ONLINE_JUDGE
  38.   filebuf in, out;
  39.   cin.rdbuf(in.open((string(argv[0]) + ".in").c_str(), ios_base::in));
  40.   cout.rdbuf(out.open((string(argv[0]) + ".out").c_str(), ios_base::out));
  41. #endif
  42.   int ncases;
  43.   cin >> ncases;
  44.   while (ncases-- > 0) {
  45.     int njobs;
  46.     cin >> njobs;
  47.     job jobs[jobcount];
  48.     for (int i = 0; i < njobs; ++i) {
  49.       jobs[i].id = i + 1;
  50.       cin >> jobs[i].time >> jobs[i].fine;
  51.     }
  52.     stable_sort(jobs, jobs + njobs, jobcmp());
  53.     for (int i = 0; i < njobs; ++i) {
  54.       cout << (i > 0 ? " " : "") << jobs[i].id;
  55.     }
  56.     cout << '/n';
  57.     if (ncases > 0) cout << '/n';
  58.   }
  59.   return 0;
  60. }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值