Advertisement
Korotkodul

reg C stress OK

Oct 6th, 2022 (edited)
601
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.05 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. #include <ctime>
  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.  
  52. vector <ll> a, pf;
  53. int n, ls = -1, k; // last < 0
  54. ll x, dS = 0;
  55.  
  56. vector <ll> dulla;
  57. vector <vector <ll> > aldul;
  58.  
  59. bool random = 1;
  60.  
  61. bool sh = 0;
  62.  
  63.  
  64. ll dull() {
  65.     if (sh) {
  66.         cout << "x = " << x << "\n";
  67.     }
  68.     ll r = 0;
  69.     for (int i = 0; i < n; ++i) {
  70.         dulla[i] += x;
  71.         r += abs(dulla[i]);
  72.     }
  73.     aldul.push_back(dulla);
  74.     if (sh) {
  75.         cout << "dulla\n";
  76.         cvl(dulla);
  77.     }
  78.     return r;
  79. }
  80.  
  81. ll fmr0() {
  82.     ll one = 0, two = 0, three = 0;
  83.     //ONE
  84.     int oneid = -1;
  85.     ll del = 0;
  86.     if (ls != -1) {
  87.         if (ls == 0) {
  88.             if (a[0] + dS < 0) {
  89.                 if (sh) {
  90.                     cout << "A\n";
  91.                 }
  92.                 one = abs(a[0]) - dS;
  93.                 oneid = 0;
  94.             }
  95.         } else {
  96.             if (sh) {
  97.                 cout << "B\n";
  98.             }
  99.             int L = 0, R = ls + 1, M;
  100.             while (L + 1 < R) {
  101.                 M = (L + R) / 2;
  102.                 if (a[M] + dS < 0) {
  103.                     L = M;
  104.                 } else {
  105.                     R = M;
  106.                 }
  107.             }
  108.             if (sh) {
  109.                 cout << "L = " << L << "\n";
  110.             }
  111.             one = abs(pf[L]) - dS * (L + 1);
  112.             oneid = L;
  113.             if (a[0] + dS >= 0) {
  114.                 if (sh) {
  115.                     cout << "C\n";
  116.                 }
  117.                 one = 0;
  118.                 oneid = -1;
  119.             }
  120.         }
  121.     }
  122.     if (sh) {
  123.         cout << "oneid = " << oneid << "\n";
  124.         cout << "one = " << one << "\n";
  125.     }
  126.     //TWO
  127.     if (ls != -1) {
  128.         if (sh) {
  129.             cout << "TWO\n";
  130.         }
  131.         ll del = 0;
  132.         if (oneid != -1) {
  133.             del = pf[oneid];
  134.         }
  135.         ll pf2 = abs(pf[ls] - del);
  136.         two = dS * (ls - oneid) - pf2;
  137.         if (sh) {
  138.             cout << "del = " << del << "\n";
  139.             cout << "pf2 = " << pf2 << "\n";
  140.             cout << "ls - oneid = " << (ls - oneid) << "\n";
  141.             cout << "dS * (ls - oneid) = " << dS * (ls - oneid) << "\n";
  142.         }
  143.     }
  144.     if (sh) {
  145.         cout << "two = " << two << "\n";
  146.     }
  147.     //THREE
  148.     if (ls != n - 1) {
  149.         del = 0;
  150.         if (ls >= 0) {
  151.             del = pf[ls];
  152.         }
  153.         ll pf3 = pf[n - 1] - del;
  154.         three = pf3 + dS * (n - 1 - ls);
  155.     }
  156.     if (sh) {
  157.         cout << "three = " << three << "\n";
  158.     }
  159.     if (sh) {
  160.         cout << "one two three  = " << one << ' ' << two << ' ' << three << "\n";
  161.     }
  162.     ll res = one + two + three;
  163.     return res;
  164. }
  165.  
  166. ll fls0() {
  167.     ll one = 0, two = 0, three = 0;
  168.     ll del = 0;
  169.     //ONE
  170.     int oneid = -1;
  171.     if (ls != -1) {
  172.         one = abs(pf[ls]) + abs(dS) * (ls + 1);
  173.         oneid = ls;
  174.     }
  175.     if (sh) {
  176.         cout << "one = " << one << "\n";
  177.         cout << "oneid = " << oneid << "\n";
  178.     }
  179.     if (ls == n - 1) {
  180.         if (sh) {
  181.             cout << "ret one\n";
  182.         }
  183.         return one;
  184.     }
  185.     //TWO
  186.     bool is2 = 0;
  187.     int L = ls + 1, R = n, M;
  188.     if (a[ls + 1] - abs(dS) < 0) {
  189.         is2 = 1;
  190.         while (L + 1 < R) {
  191.             M = (L + R) / 2;
  192.             if (a[M] - abs(dS) < 0) {
  193.                 L = M;
  194.             } else {
  195.                 R = M;
  196.             }
  197.         }
  198.         del = 0;
  199.         if (oneid != -1) {
  200.             del = pf[oneid];
  201.         }
  202.         ll pf2 = abs(pf[L] - del);
  203.         two = abs(dS) * (L - oneid) - pf2;
  204.         if (L == n - 1) {
  205.             return one + two;
  206.         }
  207.     } else {
  208.         cout << "no2\n";
  209.     }
  210.     if (sh) {
  211.         cout << "two  = " << two << "\n";
  212.     }
  213.     //THREE
  214.     del = 0;
  215.     ll nmb = n;
  216.     if (is2) {
  217.         del = pf[L];
  218.         nmb = n - 1 - L;
  219.     } else if (oneid != -1) {
  220.         del = pf[oneid];
  221.         nmb = n - 1 - oneid;
  222.     }
  223.     ll pf3 = abs(pf[n - 1] - del);
  224.     three = pf3 - abs(dS) * nmb;
  225.     if (sh) {
  226.         cout << "three = " << three << "\n";
  227.     }
  228.     //TOT
  229.     ll res = one + two + three;
  230.     return res;
  231. }
  232.  
  233. void slv() {
  234.     dS = 0;
  235.     vector <ll> alx;
  236.     aldul.clear();
  237.     if (!random) {
  238.         cin >> n >> k;
  239.         a.resize(n);
  240.         for (ll &i : a) cin >> i;
  241.     } else {
  242.         n = rand() % 10 + 3;
  243.         k = rand() % 10 + 20;
  244.         a.resize(n);
  245.         for (int i = 0; i < n; ++i) {
  246.             a[i] = rand() % 100 - 30;
  247.         }
  248.     }
  249.     sort(a.begin(), a.end());
  250.     dulla = a;
  251.     pf.assign(n, a[0]);
  252.     for (int i = 1; i < n; ++i) {
  253.         pf[i] = pf[i - 1] + a[i];
  254.     }
  255.     int i = -1;
  256.     while (i + 1 < n && a[i + 1] < 0) {
  257.         i++;
  258.     }
  259.     ls = i;
  260.     if (sh) {
  261.         cout << "GO\n";
  262.         cout << "a\n";
  263.         cvl(a);
  264.         cout << "dulla\n";
  265.         cvl(dulla);
  266.         cout << "pf\n";
  267.         cvl(pf);
  268.         cout << "ls = " << ls << "\n";
  269.     }
  270.  
  271.     for (int go = 0; go < k; ++go) {
  272.         if (!random) {
  273.             cin >> x;
  274.         } else {
  275.             x = rand() % 10 + pow(-1, rand() % 2) * 20;
  276.         }
  277.         alx.push_back(x);
  278.         dS += x;
  279.         ll my;
  280.         if (dS >= 0) {
  281.             my = fmr0();
  282.         } else {
  283.             my = fls0();
  284.         }
  285.         ll stup = dull();
  286.         if (stup != my) {
  287.             cout << "WA\n";
  288.             cout << "a\n";
  289.             cvl(a);
  290.             cout << "pf.size() = " << pf.size() << "\n";
  291.             cout << "pf\n";
  292.             cvl(pf);
  293.             cout << "dS = " << dS << "\n";
  294.             cout << "my stup = " << my << ' ' << stup << "\n";
  295.             cout << "alx\n";
  296.             cvl(alx);
  297.             cout << "aldul\n";
  298.             for (auto v: aldul) {
  299.                 cvl(v);
  300.             } cout << "\n\n";
  301.             exit(0);
  302.         } else {
  303.             cout << "OK\n";
  304.         }
  305.     }
  306.     cout << "OK " << k << " tests \n";;
  307. }
  308. /*
  309. 7 1
  310. -26 -8 20 22 28 33 69
  311. -15
  312.  
  313.  
  314. WA
  315. a
  316. -26 -8 20 22 28 33 69
  317. pf.size() = 7
  318. pf
  319. -26 -34 -14 8 36 69 138
  320. dS = -15
  321. my stup = 64 161
  322. alx
  323. -15
  324. aldul
  325. -41 -23 5 7 13 18 54
  326. */
  327.  
  328. int main() {
  329.     /*ios::sync_with_stdio(0);
  330.     cin.tie(0);
  331.     cout.tie(0);*/
  332.     srand(time(0));
  333.     while (1) {
  334.         slv();
  335.     }
  336. }
  337.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement