ChoDog

Задача I. Робот

Dec 1st, 2019
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.59 KB | None | 0 0
  1. #include <iostream>
  2. #include <set>
  3. #include <vector>
  4. #include <algorithm>
  5. #include <limits>
  6. #define ll long long
  7.  
  8. using namespace std;
  9.  
  10. /*
  11.     4
  12.     3 5
  13.     3 5
  14.     3 5
  15.     3 5
  16.  
  17. */
  18.  
  19. bool compare(pair<int, pair<int, int>> p1, pair<int, pair<int, int>> p2) {
  20.     return (p1.second.first < p2.second.first || p1.second.first == p2.second.first && p1.second.second > p2.second.second ? true : false);
  21. }
  22.  
  23. char main() {
  24.     int n;
  25.     cin >> n;
  26.  
  27.     vector<pair<int, pair<int, ll>>> vec(n + 1);
  28.     vector<pair<ll, ll>> shtraf;
  29.     vector<ll> ans(200000 + 1, 0);
  30.     set<int> isFree;
  31.     for (int i = 1; i <= 200000; i++) isFree.insert(i);
  32.  
  33.  
  34.  
  35.     for (int i = 1; i <= n; i++) {
  36.         vec[i].first = i;
  37.         cin >> vec[i].second.first >> vec[i].second.second;
  38.     }
  39.  
  40.     sort(++vec.begin(), vec.end(), compare);
  41.  
  42.  
  43.     for (int x = 1; x <= n; x++) {
  44.         int ii = vec[x].first, di = vec[x].second.first, wi = vec[x].second.second;
  45.  
  46.         auto it = isFree.lower_bound(di);
  47.  
  48.         if (it == isFree.end()) {
  49.             it--;
  50.             ans[ii] = *it;
  51.             isFree.erase(it);
  52.             continue;
  53.         }
  54.  
  55.         if (*it == di) {
  56.             ans[ii] = di;
  57.             isFree.erase(it);
  58.             continue;
  59.         }
  60.  
  61.         if (it == isFree.begin()) {
  62.             shtraf.push_back(make_pair(ii, wi));
  63.             continue;
  64.         }
  65.  
  66.         it--;
  67.         ans[ii] = *it;
  68.         isFree.erase(it);
  69.  
  70.     }
  71.  
  72.     int j = 0;
  73.     ll bil = 0;
  74.     auto it = isFree.begin();
  75.  
  76.     for (int i = 1; i <= n; i++) {
  77.  
  78.         if (ans[i] == 0) {
  79.             ans[i] = *it;
  80.             bil += shtraf[j].second;
  81.             j++;
  82.             it++;
  83.         }
  84.         if (it == isFree.end()) break;
  85.     }
  86.  
  87.     cout << bil << endl;
  88.     for (int x = 1; x <= n; x++) {
  89.         if (ans[x] != 0) {
  90.             cout << ans[x] << ' ';
  91.         }
  92.     }
  93.  
  94.     return 0;
  95. }
Add Comment
Please, Sign In to add comment