Naxocist

Parenting Partnering Returns [greedy]

Apr 19th, 2022 (edited)
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.26 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. using pi = pair<int, int>;
  5. using pii = pair<pi, char>;
  6.  
  7. int main(){
  8.     int t; scanf("%d", &t);
  9.  
  10.     for(int k=1; k<=t; ++k){
  11.         int n; scanf("%d", &n);
  12.         vector<pair<pi, int>> v;
  13.         int s, e;
  14.  
  15.         for(int i=0; i<n; ++i){
  16.             scanf("%d %d", &s, &e);
  17.             v.push_back({{s, e}, i});
  18.         }
  19.  
  20.         sort(v.begin(), v.end());
  21.  
  22.         priority_queue<pii, vector<pii>, greater<pii>> pq;
  23.         pq.push({{v[0].first.second, v[0].first.first}, 'C'});
  24.  
  25.         char ans[1005];
  26.         ans[v[0].second] = 'C';
  27.  
  28.         for(int j=1; j<n; ++j){
  29.             s = v[j].first.first; e = v[j].first.second;
  30.             int index = v[j].second;
  31.  
  32.             char c = pq.top().second;
  33.             if(s >= pq.top().first.first)  pq.pop();
  34.             else c = (c == 'C' ? 'J' : 'C');
  35.  
  36.             ans[index] = c;
  37.             pq.push({{e, s}, c});
  38.  
  39.         }
  40.  
  41.         cout << "Case #" << k << ": ";
  42.         if(pq.size() > 2) cout << "IMPOSSIBLE";
  43.         else for(int q=0; q<n; ++q) cout << ans[q];
  44.        
  45.         cout << "\n";
  46.     }
  47.     return 0;
  48. }
  49.  
  50. /*
  51. 4
  52. 3
  53. 360 480
  54. 420 540
  55. 600 660
  56. 3
  57. 0 1440
  58. 1 3
  59. 2 4
  60. 5
  61. 99 150
  62. 1 100
  63. 100 301
  64. 2 5
  65. 150 250
  66. 2
  67. 0 720
  68. 720 1440
  69. */
Add Comment
Please, Sign In to add comment