Advertisement
Korotkodul

TG D 1

Oct 5th, 2022
649
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.43 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 pll pair <long long, long long>
  12. #define pb(x) push_back(x)
  13. using namespace std;
  14. using ll = long long;
  15. using ld = long double;
  16. using db = double;
  17. void cv(vector <int> &v) {
  18.     for (auto x : v) cout << x << ' ';
  19.     cout << "\n";
  20. }
  21.  
  22. void cvl(vector <ll> &v) {
  23.     for (auto x : v) cout << x << ' ';
  24.     cout << "\n";
  25. }
  26.  
  27.  
  28. void cvv(vector <vector <int> > &v) {
  29.     for (auto x : v) cv(x);
  30.     cout << "\n";
  31. }
  32.  
  33. void cvb(vector <bool> v) {
  34.     for (bool x : v) cout << x << ' ';
  35.     cout << "\n";
  36. }
  37.  
  38. void cvs(vector <string>  v) {
  39.     for (auto a : v) {
  40.         cout << a << "\n";
  41.     }
  42. }
  43.  
  44. void cvp(vector <pii> a) {
  45.     for (auto p : a) {
  46.         cout << p.first << ' ' << p.second << "\n";
  47.     }
  48.     cout << "\n";
  49. }
  50.  
  51. void cvpl(vector <pll> a) {
  52.     for (auto p : a) {
  53.         cout << p.first << ' ' << p.second << "\n";
  54.     }
  55.     cout << "\n";
  56. }
  57.  
  58. struct prt{
  59.     ll r, b, id;
  60.     void see() {
  61.         cout << r << ' ' << b << ' ' << id << "\n\n";
  62.     }
  63. };
  64.  
  65. bool cmp(prt a, prt b) {
  66.     return a.r < b.r || (a.r == b.r && a.b > b.b);
  67. }
  68.  
  69. struct res{
  70.     ll spent, id, L, rk;//M: M - 1
  71.     void see() {
  72.         cout << "res\n";
  73.         cout << "spent id L rk\n";
  74.         cout << spent << ' '<< id << ' ' << L << ' ' << rk << "\n\n";
  75.     }
  76. };
  77.  
  78. bool cmp1(res a, res b) {
  79.     return a.spent < b.spent;
  80. }
  81.  
  82.  
  83.  
  84. /*
  85. 6
  86. 1 1
  87. 1 2
  88. 3 3
  89. 3 4
  90. 5 7
  91. 5 8
  92.  
  93. 8
  94. 10 13
  95. 10 20
  96. 30 30
  97. 30 40
  98. 50 70
  99. 50 80
  100. 100 200
  101. 100 300
  102.  
  103. 8
  104. 10 13
  105. 100 20
  106. 300 30
  107. 300 40
  108. 500 70
  109. 500 80
  110. 510 200
  111. 510 300
  112.  
  113.  
  114. MISTAKE FOUND!!!
  115. 8
  116. 10 13
  117. 100 20
  118. 300 30
  119. 300 40
  120. 500 70
  121. 500 80
  122. 511 200
  123. 511 300
  124. 77
  125. 5
  126. 10 100 300 300 507 500 507 506
  127. */
  128. bool sh = 0;
  129. const ll inf = 2e9;
  130. #include <ctime>
  131. #include <numeric>
  132. vector <prt> a;
  133. vector <ll> vote;
  134. vector <pll> raw;
  135.  
  136. bool check(int wn_id) {
  137.     return vote[wn_id] == *max_element(vote.begin(), vote.end()) && count(vote.begin(), vote.end(), vote[wn_id]) == 1;
  138. }
  139.  
  140. bool rand_mode = 0;
  141.  
  142. bool debug = 0;
  143.  
  144. int main() {
  145.     ios::sync_with_stdio(0);
  146.     cin.tie(0);
  147.     cout.tie(0);
  148.     srand(time(0));
  149.     int n;
  150.     if (rand_mode) {
  151.         n = rand() % 100 + 3;
  152.     } else {
  153.         cin >> n;
  154.     }
  155.  
  156.     a.resize(n);
  157.     raw.resize(n);
  158.     for (int i = 0; i < n; ++i) {
  159.         if (!rand_mode) {
  160.             cin >> a[i].r >> a[i].b;
  161.             if (a[i].b == -1) {
  162.                 a[i].b = inf;
  163.             }
  164.             raw[i] = {a[i].r, a[i].b};
  165.             a[i].id = i;
  166.             continue;
  167.         }
  168.         a[i].r = rand() % 100;
  169.         a[i].b = rand() % 100;
  170.         raw[i] = {a[i].r, a[i].b};
  171.         a[i].id = i;
  172.     }
  173.  
  174.     sort(a.begin(), a.end(), cmp);
  175.     vector <ll> pf(n);
  176.     vector <ll> win_vote(n);
  177.     pf[0] = a[0].r;
  178.     for (int i = 1; i < n; ++i) {
  179.         pf[i] = pf[i - 1] + a[i].r;
  180.     }
  181.     vector <res> ans;
  182.  
  183.     if (sh) {
  184.         cout << "n = " << n << "\n";
  185.         cout << "raw data :\n";
  186.         cvpl(raw);
  187.         cout << "a\n";
  188.         for (auto y : a) {
  189.             y.see();
  190.         }
  191.         cout << "pf\n";
  192.         cvl(pf);
  193.     }
  194.  
  195.     if (n == 1) {
  196.         cout << a[0].b << "\n" << 1 << "\n" << a[0].r;
  197.         exit(0);
  198.     }
  199.  
  200.     if (a[n - 1].b != -1) {
  201.         if (sh) {
  202.             cout << "last\n";
  203.         }
  204.         res last = {a[n - 1].b, a[n - 1].id, inf, inf}; //n - 1 -->???
  205.         win_vote[a[n - 1].id] = a[n - 1].r;
  206.         if (a[n - 1].r == a[n - 2].r) {
  207.             last.spent++;
  208.             win_vote[a[n - 1].id]++;
  209.         }
  210.  
  211.         ans.push_back(last);
  212.     }
  213.  
  214.  
  215.     for (int i = 0; i < n; ++i) {
  216.         if (a[i].b == inf) {
  217.             if (sh) {
  218.                 cout << "skip\n";
  219.             }
  220.             continue;
  221.         }
  222.         if (a[i].r == a[n - 1].r) {
  223.             if (sh) cout << "break\n";
  224.             break;
  225.         }
  226.         ll L = 0, R = a[n - 1].r + 10, M, ad, spent;
  227.         if (sh) {
  228.             cout << "i = " << i << "\n";
  229.             //cout << "L R = " << L << ' ' << R << "\n";
  230.         }
  231.         int lk, rk, mk, goodrk;
  232.         while (L + 1 < R) {
  233.             M = (L + R) / 2;
  234.             if (sh) {
  235.                //cout << "M = " << M << "\n";
  236.             }
  237.             lk = i - 1;
  238.             rk = n - 1;
  239.             if (sh) {
  240.                 //cout << "lk rk = " << lk << ' ' << rk << "\n";
  241.             }
  242.             while (lk + 1 < rk) {
  243.                 mk = (lk + rk) / 2;
  244.                 if (sh) {
  245.                     //cout << "mk = " << mk << "\n";
  246.                 }
  247.                 if (a[mk].r >= M) {
  248.                     rk = mk;
  249.                 } else {
  250.                     lk = mk;
  251.                 }
  252.             }
  253.             ll del = 0;
  254.             if (rk > 0) {
  255.                 del = pf[rk - 1];
  256.             }
  257.             if (sh) {
  258.                 //cout << "rk del = " << rk << ' ' << del << "\n";
  259.             }
  260.             ad = pf[n - 1] - del - (M - 1) * (n - rk);
  261.             if (sh) {
  262.                 //cout << "(n - rk) = " << (n - rk ) << "\n";
  263.                 //cout << "ad (cut voices) = " << ad << "\n";
  264.             }
  265.             if (a[i].r + ad >= M) {
  266.                 L = M;
  267.                 //spent = ad + a[i].b;//- (n - rk - 1)???
  268.                 spent = L + 1 - a[i].r + a[i].b;
  269.                 win_vote[a[i].id] = a[i].r + ad;
  270.                 goodrk = rk;
  271.             } else {
  272.                 R = M;
  273.             }
  274.         }
  275.  
  276.         res now = {spent, a[i].id, L, goodrk};
  277.         ans.push_back(now);
  278.         if (sh) {
  279.             cout << "L = " << L << "\n";
  280.             cout << "spent = " << spent << "\n";
  281.             cout << "now\n";
  282.             now.see();
  283.         }
  284.     }
  285.     sort(ans.begin(), ans.end(), cmp1);
  286.  
  287.     if (sh) {
  288.         cout << "ans\n";
  289.         for (auto y : ans) {
  290.             y.see();
  291.         }
  292.     }
  293.  
  294.     if (sh) {
  295.         cout << "RES ANS\n";
  296.         ans[0].see();
  297.     }
  298.  
  299.     res get = ans[0];
  300.     vote.resize(n);
  301.     /*for (int i = 0; i < n; ++i) {
  302.         if (a[i].id < get.rk) {
  303.             vote[a[i].id] = a[i].r;
  304.         } else {
  305.             if (sh) {
  306.                 cout << "a[i].id = " << a[i].id << "\n";
  307.                 cout << "get.L - 1 = " << get.L - 1 << "\n";
  308.             }
  309.             vote[a[i].id] = get.L - 1;
  310.         }
  311.     }*/
  312.     for (int i = n - 1; i >= get.rk; --i) {
  313.         vote[a[i].id] = get.L - 1;
  314.     }
  315.     for (int i = min(get.rk - 1, (long long)(n - 1)); i >= 0; --i) {
  316.         vote[a[i].id] = a[i].r;
  317.     }
  318.     vote[get.id] = win_vote[get.id];
  319.     if (a[n - 1].id == get.id && win_vote[get.id] == raw[get.id].first + 1) {
  320.         vote[a[n - 2].id]--;
  321.     }
  322.     /*int i = n - 1;
  323.     while (i > get.rk && vote[get.id] - (get.L - 1) > 1) {
  324.         get.spent--;
  325.         vote[a[i].id]++;
  326.         vote[get.id]--;
  327.         i--;
  328.     }*/
  329.  
  330.     int idx = n;
  331.     //>= get.rk + 1
  332.     while (idx - 1 >= 0 && vote[get.id] > get.L + 1) {//this way wi will go till >= rk + 1 from n - 1
  333.         idx--;
  334.         vote[a[idx].id]++;
  335.         vote[get.id]--;
  336.     }
  337.  
  338.     cout << get.spent << "\n";
  339.     cout << get.id + 1 << "\n";
  340.     cvl(vote);
  341.     if (debug) {
  342.         ll S = accumulate(vote.begin(), vote.end(), 0);
  343.         if (S != pf[n - 1]) {
  344.             cout << "BAD SUM\n";
  345.             cout << "S pf[n - 1] = " << S << ' ' << pf[n - 1] << "\n";
  346.             cout << "CHANGED VOICES\n";
  347.             for (int i = 0; i < n; ++i) {
  348.                 if (vote[i] != raw[i].first) {
  349.                     cout << "i bef aft = " << i << ' ' << raw[i].first << ' ' << vote[i] << "\n";
  350.                 }
  351.             }
  352.         } else {
  353.             cout << "OK SUM\n";
  354.         }
  355.         cout << "CHANGED VOICES\n";
  356.         for (int i = 0; i < n; ++i) {
  357.             if (vote[i] != raw[i].first) {
  358.                 cout << "i bef aft = " << i << ' ' << raw[i].first << ' ' << vote[i] << "\n";
  359.             }
  360.         }
  361.         /*cout << "raw data :\n";
  362.         cout << "n = \n" << n << "\n";
  363.         cvpl(raw);*/
  364.  
  365.         if (sh) {
  366.             cout << "a\n";
  367.             for (auto y : a) {
  368.                 y.see();
  369.             } cout << "\n";
  370.         }
  371.         if (check(get.id)) {
  372.             cout << "OK\n";
  373.         } else {
  374.             cout << "WA\n";
  375.         }
  376.     }
  377. }
  378. /*
  379. LAST THIND TO IMPROVE IF WA:
  380. check vote[get.id]--
  381. get.spent--
  382. vote[a[i].id]++
  383. right in bin search
  384. */
  385.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement