Advertisement
Guest User

Untitled

a guest
Apr 19th, 2016
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.44 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. typedef long long ll;
  6. typedef pair<ll, ll> ii;
  7. #define fr first
  8. #define sc second
  9. #define mp make_pair
  10.  
  11. const int MAXN = 50010;
  12. const ll inf = (1LL << 61);
  13.  
  14. int n, k;
  15. ll budget;
  16. ii ar[MAXN];
  17.  
  18. bool cmp(ii a, ii b)
  19. {
  20.     if (a.sc == b.sc)
  21.         return a.fr > b.fr;
  22.     return a.sc < b.sc;
  23. }
  24.  
  25. bool delrem[MAXN], delcoup[MAXN], delswapp[MAXN];
  26.  
  27. int solve()
  28. {
  29.     ll cur = 0;
  30.     int ans = 0;
  31.     priority_queue<ii> coup;
  32.     priority_queue<ii, vector<ii>, greater<ii> > rem, swapp;
  33.     for (int i = 0; i < n; i++)
  34.     {
  35.         if (cur + ar[i].sc <= budget)
  36.         {
  37.             coup.push(mp(ar[i].sc, i));
  38.             swapp.push(mp(ar[i].fr - ar[i].sc, i));
  39.             cur += ar[i].sc;
  40.             ans++;
  41.         }
  42.         else
  43.         {
  44.             rem.push(mp(ar[i].fr, i));
  45.         }
  46.     }
  47.     int coupsize = coup.size();
  48.     while (coupsize > k)
  49.     {
  50.         ll a;
  51.         if (rem.empty() || coup.empty())
  52.             a = inf;
  53.         else a = rem.top().fr - coup.top().fr;
  54.         ll b;
  55.         if (swapp.empty())
  56.             b = inf;
  57.         else b = swapp.top().fr;
  58.         if (min(a, b) + cur > budget)
  59.         {
  60.             cur -= coup.top().fr;
  61.             rem.push(mp(ar[coup.top().sc].fr, coup.top().sc));
  62.             delswapp[coup.top().sc] = true;
  63.             ans--;
  64.             coup.pop();
  65.             coupsize--;
  66.         }
  67.         else if (b < a)
  68.         {
  69.             delcoup[swapp.top().sc] = true;
  70.             delrem[swapp.top().sc] = true;
  71.             cur += swapp.top().fr;
  72.             swapp.pop();
  73.             coupsize--;
  74.         }
  75.         else
  76.         {
  77.             delrem[rem.top().sc] = true;
  78.             delswapp[coup.top().sc] = true;
  79.             delswapp[rem.top().sc] = true;
  80.             cur += b;
  81.             rem.push(mp(ar[coup.top().sc].fr, coup.top().sc));
  82.             coup.pop();
  83.             rem.pop();
  84.             coupsize--;
  85.         }
  86.         while (!rem.empty() && delrem[rem.top().sc])
  87.             rem.pop();
  88.         while (!coup.empty() && delcoup[coup.top().sc])
  89.             coup.pop();
  90.         while (!swapp.empty() && delswapp[swapp.top().sc])
  91.             swapp.pop();
  92.     }
  93.     return ans;
  94. }
  95.  
  96. int main()
  97. {
  98.     scanf("%d%d", &n, &k);
  99.     scanf("%I64d", &budget);
  100.     for (int i = 0; i < n; i++)
  101.         scanf("%I64d%I64d", &ar[i].fr, &ar[i].sc);
  102.     sort(ar, ar + n, cmp);
  103.     printf("%d\n", solve());
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement