Advertisement
Korotkodul

NG D 4

Sep 30th, 2022 (edited)
661
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.92 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 || (a.r == b.r && a.b > b.b);
  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 L 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 = 0;
  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 13
  88. 10 20
  89. 30 30
  90. 30 40
  91. 50 70
  92. 50 80
  93. 100 200
  94. 100 300
  95.  
  96. 8
  97. 10 13
  98. 100 20
  99. 300 30
  100. 300 40
  101. 500 70
  102. 500 80
  103. 510 200
  104. 510 300
  105.  
  106.  
  107. MISTAKE FOUND!!!
  108. 8
  109. 10 13
  110. 100 20
  111. 300 30
  112. 300 40
  113. 500 70
  114. 500 80
  115. 511 200
  116. 511 300
  117. 77
  118. 5
  119. 10 100 300 300 507 500 507 506
  120. */
  121.  
  122. const ll inf = 2e9;
  123.  
  124.  
  125.  
  126. int main() {
  127.     ios::sync_with_stdio(0);
  128.     cin.tie(0);
  129.     cout.tie(0);
  130.     int n; cin >> n;
  131.     vector <prt> a(n);
  132.     for (int i = 0; i < n; ++i) {
  133.         cin >> a[i].r >> a[i].b;
  134.         a[i].id = i;
  135.     }
  136.     sort(a.begin(), a.end(), cmp);
  137.     vector <ll> pf(n);
  138.     vector <ll> win_vote(n);
  139.     pf[0] = a[0].r;
  140.     for (int i = 1; i < n; ++i) {
  141.         pf[i] = pf[i - 1] + a[i].r;
  142.     }
  143.     vector <res> ans;
  144.  
  145.     if (sh) {
  146.         cout << "a\n";
  147.         for (auto y : a) {
  148.             y.see();
  149.         }
  150.         cout << "pf\n";
  151.         cvl(pf);
  152.     }
  153.  
  154.     if (n == 1) {
  155.         cout << a[0].b;
  156.         exit(0);
  157.     }
  158.  
  159.     /*проверка для a[i].r == a[n - 1].r: учтено что на последнем месте самая малая взятка
  160.      по сравнениею с теми кто прежде стоит и за кого столько же голосует*/
  161.     if (a[n - 1].b != -1) {
  162.         if (sh) {
  163.             cout << "last\n";
  164.         }
  165.         res last = {a[n - 1].b, a[n - 1].id, inf, inf}; //n - 1 -->???
  166.         if (a[n - 1].r == a[n - 2].r) {
  167.             last.spent++;
  168.         }
  169.         win_vote[a[n - 1].id] = a[n - 1].r;
  170.         ans.push_back(last);
  171.     }
  172.  
  173.  
  174.     for (int i = 0; i < n; ++i) {
  175.         if (a[i].b == -1) {
  176.             if (sh) {
  177.                 cout << "skip\n";
  178.             }
  179.             continue;
  180.         }
  181.         if (a[i].r == a[n - 1].r) {
  182.             if (sh) cout << "break\n";
  183.             break;
  184.         }
  185.         ll L = 0, R = a[n - 1].r + 10, M, ad, spent;
  186.         if (sh) {
  187.             cout << "i = " << i << "\n";
  188.             cout << "L R = " << L << ' ' << R << "\n";
  189.         }
  190.         int lk, rk, mk;
  191.         while (L + 1 < R) {
  192.             M = (L + R) / 2;
  193.             if (sh) {
  194.                cout << "M = " << M << "\n";
  195.             }
  196.             lk = i - 1;
  197.             rk = n - 1;
  198.             if (sh) {
  199.                 //cout << "lk rk = " << lk << ' ' << rk << "\n";
  200.             }
  201.             while (lk + 1 < rk) {
  202.                 mk = (lk + rk) / 2;
  203.                 if (sh) {
  204.                     //cout << "mk = " << mk << "\n";
  205.                 }
  206.                 if (a[mk].r >= M) {
  207.                     rk = mk;
  208.                 } else {
  209.                     lk = mk;
  210.                 }
  211.             }
  212.             ll del = 0;
  213.             if (rk > 0) {
  214.                 del = pf[rk - 1];
  215.             }
  216.             if (sh) {
  217.                 //cout << "rk del = " << rk << ' ' << del << "\n";
  218.             }
  219.             ad = pf[n - 1] - del - (M - 1) * (n - rk);
  220.             if (sh) {
  221.                 //cout << "(n - rk) = " << (n - rk ) << "\n";
  222.                 cout << "ad (cut voices) = " << ad << "\n";
  223.             }
  224.             if (a[i].r + ad >= M) {
  225.                 L = M;
  226.                 spent = ad + a[i].b;//- (n - rk - 1)
  227.                 win_vote[a[i].id] = a[i].r + ad;
  228.             } else {
  229.                 R = M;
  230.             }
  231.         }
  232.  
  233.         res now = {spent, a[i].id, L, rk};
  234.         ans.push_back(now);
  235.         if (sh) {
  236.             cout << "L = " << L << "\n";
  237.             cout << "spent = " << spent << "\n";
  238.             cout << "now\n";
  239.             now.see();
  240.         }
  241.     }
  242.     sort(ans.begin(), ans.end(), cmp1);
  243.  
  244.     if (sh) {
  245.         cout << "ans\n";
  246.         for (auto y : ans) {
  247.             y.see();
  248.         }
  249.     }
  250.  
  251.     if (sh) {
  252.         cout << "RES ANS\n";
  253.         ans[0].see();
  254.     }
  255.  
  256.     res get = ans[0];
  257.     vector <ll> vote(n);
  258.     for (int i = 0; i < n; ++i) {
  259.         if (i < get.rk) {
  260.             vote[a[i].id] = a[i].r;
  261.         } else {
  262.             if (sh) {
  263.                 cout << "a[i].id = " << a[i].id << "\n";
  264.                 cout << "get.L - 1 = " << get.L - 1 << "\n";
  265.             }
  266.             vote[a[i].id] = get.L - 1;
  267.         }
  268.     }
  269.     vote[get.id] = win_vote[get.id];
  270.     int i = n - 1;
  271.     while (i > get.rk && vote[get.id] - (get.L - 1) > 1) {
  272.         get.spent--;
  273.         vote[a[i].id]++;
  274.         vote[get.id]--;
  275.         i--;
  276.     }
  277.     cout << get.spent << "\n";
  278.     cout << get.id + 1 << "\n";
  279.     cvl(vote);
  280. }
  281. /*
  282. LAST THIND TO IMPROVE IF WA:
  283. check vote[get.id]--
  284. get.spent--
  285. vote[a[i].id]++
  286. right in bin search
  287. */
  288.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement