Advertisement
matheus_monteiro

M - Most Ordered Way

Mar 28th, 2024
792
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.02 KB | None | 0 0
  1. /*
  2.  * Author: Matheus Monteiro
  3.  */
  4.  
  5. #include "bits/stdc++.h"
  6.  
  7. using namespace std;
  8.  
  9. void solve() {
  10.     int n;
  11.  
  12.     cin >> n;
  13.  
  14.     vector<int> duration(n);
  15.     vector<int> deadline(n);
  16.  
  17.     for(int i = 0; i < n; ++i) {
  18.         cin >> duration[i] >> deadline[i];
  19.     }
  20.  
  21.     vector<int> position(n);
  22.     iota(position.begin(), position.end(), 0);
  23.  
  24.     sort(position.begin(), position.end(), [&](int task1, int task2) {
  25.         return deadline[task1] < deadline[task2];
  26.     });
  27.  
  28.     long long currentTime = 0;
  29.     vector<bool> performedTask(n, false);
  30.     vector<int> smallestPermutation;
  31.  
  32.     for(int i = 0; i < n; ++i) {
  33.         int bestJ = -1;
  34.         long long minGap = numeric_limits<long long>::max();
  35.         long long acumulatedDuration = 0;
  36.         for(const int &jPos : position) {
  37.             if(!performedTask[jPos]) {
  38.                 if(duration[jPos] <= minGap && (bestJ == -1 || jPos < bestJ)) {
  39.                     bestJ = jPos;
  40.                 }
  41.                 minGap = min(deadline[jPos] - duration[jPos] - currentTime - acumulatedDuration, minGap);    
  42.                 acumulatedDuration += duration[jPos];
  43.             }
  44.         }
  45.  
  46.         if(bestJ == -1) {
  47.             cout << "*\n";
  48.             return;
  49.         }
  50.  
  51.         currentTime += duration[ bestJ ];
  52.         smallestPermutation.push_back(bestJ);
  53.         performedTask[bestJ] = true;
  54.     }
  55.  
  56.     currentTime = 0;
  57.     for(const int &num : smallestPermutation) {
  58.         currentTime += duration[num];
  59.         if(currentTime > deadline[num]) {
  60.             cout << "*\n";
  61.             return;
  62.         }
  63.     }
  64.  
  65.     for(const int &num : smallestPermutation)
  66.         cout << num + 1 << ' ';
  67.     cout << endl;
  68. }
  69.  
  70. int32_t main() {
  71.     std::ios_base::sync_with_stdio(0);
  72.     std::cin.tie(0);
  73.     std::cout.tie(0);
  74.  
  75.     int numTestCases = 1;
  76.     // std::cin >> numTestCases;
  77.     for(int testCase = 1; testCase <= numTestCases; ++testCase) {
  78.         // cout << "Case #" << testCase << ": ";
  79.         solve();
  80.     }
  81.  
  82.     return 0;
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement