Advertisement
Korotkodul

TG D 2

Sep 30th, 2022
702
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.25 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <vector>
  4. #include <queue>
  5. #include <algorithm>
  6. #include <string>
  7. #include <stack>
  8. #include <set>
  9. #include <map>
  10. #define pii pair <int, int>
  11. #define pb(x) push_back(x)
  12. using namespace std;
  13. using ll = long long;
  14. using ld = long double;
  15. using db = double;
  16. void cv(vector <int> &v) {
  17.     for (auto x : v) cout << x << ' ';
  18.     cout << "\n";
  19. }
  20.  
  21. void cvl(vector <ll> &v) {
  22.     for (auto x : v) cout << x << ' ';
  23.     cout << "\n";
  24. }
  25.  
  26.  
  27. void cvv(vector <vector <int> > &v) {
  28.     for (auto x : v) cv(x);
  29.     cout << "\n";
  30. }
  31.  
  32. void cvb(vector <bool> v) {
  33.     for (bool x : v) cout << x << ' ';
  34.     cout << "\n";
  35. }
  36.  
  37. void cvs(vector <string>  v) {
  38.     for (auto a : v) {
  39.         cout << a << "\n";
  40.     }
  41. }
  42.  
  43. void cvp(vector <pii> a) {
  44.     for (auto p : a) {
  45.         cout << p.first << ' ' << p.second << "\n";
  46.     }
  47.     cout << "\n";
  48. }
  49.  
  50. struct prt{
  51.     ll r, b, id;
  52.     void see() {
  53.         cout << r << ' ' << b << ' ' << id << "\n\n";
  54.     }
  55. };
  56.  
  57. bool cmp(prt a, prt b) {
  58.     return a.r < b.r;
  59. }
  60.  
  61. struct res{
  62.     ll spent, id, L, rk;//M: M - 1 -- макс кроме нас
  63.     void see() {
  64.         cout << "res\n";
  65.         cout << "spent id M rk\n";
  66.         cout << spent << ' '<< id << ' ' << L << ' ' << rk << "\n\n";
  67.     }
  68. };
  69.  
  70. bool cmp1(res a, res b) {
  71.     return a.spent < b.spent;
  72. }
  73.  
  74.  
  75. bool sh = 1;
  76.  
  77. /*
  78. 6
  79. 1 1
  80. 1 2
  81. 3 3
  82. 3 4
  83. 5 7
  84. 5 8
  85.  
  86. 8
  87. 10 10
  88. 10 20
  89. 30 30
  90. 30 40
  91. 50 70
  92. 50 80
  93. 100 200
  94. 100 300
  95. */
  96.  
  97. int main() {
  98.     ios::sync_with_stdio(0);
  99.     cin.tie(0);
  100.     cout.tie(0);
  101.     int n; cin >> n;
  102.     vector <prt> a(n);
  103.     for (int i = 0; i < n; ++i) {
  104.         cin >> a[i].r >> a[i].b;
  105.         a[i].id = i;
  106.     }
  107.     sort(a.begin(), a.end(), cmp);
  108.     vector <ll> pf(n);
  109.     pf[0] = a[0].r;
  110.     for (int i = 1; i < n; ++i) {
  111.         pf[i] = pf[i - 1] + a[i].r;
  112.     }
  113.     vector <res> ans;
  114.  
  115.     if (sh) {
  116.         cout << "a\n";
  117.         for (auto y: a) {
  118.             y.see();
  119.         }
  120.         cout << "pf\n";
  121.         cvl(pf);
  122.     }
  123.  
  124.     for (int i = 0; i < n; ++i) {
  125.         if (a[i].b == -1) {
  126.             continue;
  127.         }
  128.         ll L = a[i].r, R = a[n - 1].r, M, ad;
  129.         if (sh) {
  130.             cout << "i = " << i << "\n";
  131.             cout << "L R = " << L << ' ' << R << "\n";
  132.         }
  133.         int lk, rk, mk;
  134.         while (L + 1 < R) {
  135.             M = (L + R) / 2;
  136.             if (sh) {
  137.                 cout << "M = " << M << "\n";
  138.             }
  139.             lk = i - 1;
  140.             rk = n - 1;
  141.             if (sh) {
  142.                 cout << "lk rk = " << lk << ' ' << rk << "\n";
  143.             }
  144.             while (lk + 1 < rk) {
  145.                 mk = (lk + rk) / 2;
  146.                 if (sh) {
  147.                     cout << "mk = " << mk << "\n";
  148.                 }
  149.                 if (a[mk].r >= M) {
  150.                     rk = mk;
  151.                 } else {
  152.                     lk = mk;
  153.                 }
  154.             }
  155.             ll del = 0;
  156.             if (rk > 0) {
  157.                 del = pf[rk - 1];
  158.             }
  159.             if (sh) {
  160.                 cout << "rk del = " << rk << ' ' << del << "\n";
  161.             }
  162.             ad = pf[n - 1] - del - (M - 1) * (n - rk);
  163.             if (sh) {
  164.                 cout << "(n - rk) = " << (n - rk ) << "\n";
  165.                 cout << "ad (cut voices) = " << ad << "\n";
  166.             }
  167.             if (a[i].r + ad >= M) {
  168.                 L = M;
  169.             } else {
  170.                 R = M;
  171.             }
  172.         }
  173.         //НЕТ!!! СРАЗУ ТУТ НАДО ДЕЛАТЬ ПРОВЕРКУ НА ОПТИМАЛЬНОСТЬ
  174.         ll spent = L - a[i].r + a[i].b;  //тк нам доостаточно уровня M!!!
  175.         //а зная итоговое rk res и M, мы смодем восстановить, что каким-то партиям мы вернем срезанные голоса
  176.         res now = {spent, a[i].id, L, rk};
  177.         ans.push_back(now);
  178.  
  179.         if (sh) {
  180.  
  181.             cout << "now\n";
  182.             now.see();
  183.         }
  184.     }
  185.     sort(ans.begin(), ans.end(), cmp1);
  186.  
  187.     if (sh) {
  188.         cout << "ans\n";
  189.         for (auto y: ans) {
  190.             y.see();
  191.         }
  192.     }
  193.  
  194.     if (sh) {
  195.         cout << "RES ANS\n";
  196.         ans[0].see();
  197.     }
  198. }
  199.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement