Advertisement
Korotkodul

TG D debug 2

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