Advertisement
Korotkodul

reg C stress 1

Oct 6th, 2022 (edited)
482
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.42 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.  
  58. bool random = 1;
  59.  
  60. bool sh = 0;
  61.  
  62. ll dull() {
  63.     ll r = 0;
  64.     for (int i = 0; i < n; ++i) {
  65.         dulla[i] += x;
  66.         r += abs(dulla[i]);
  67.     }
  68.     return r;
  69. }
  70.  
  71. ll fmr0() {
  72.     ll one = 0, two = 0, three = 0;
  73.     //ONE
  74.     int oneid = -1;
  75.     ll del = 0;
  76.     if (ls != -1) {
  77.         if (ls == 0) {
  78.             if (abs(a[0]) + dS < 0) {
  79.                 one = abs(a[0]) - dS;
  80.                 oneid = 0;
  81.             }
  82.         } else {
  83.             int L = 0, R = ls + 1, M;
  84.             while (L + 1 < R) {
  85.                 M = (L + R) / 2;
  86.                 if (a[M] + dS < 0) {
  87.                     L = M;
  88.                 } else {
  89.                     R = M;
  90.                 }
  91.             }
  92.             one = abs(pf[L]) - dS;
  93.             oneid = L;
  94.             if (a[0] + dS >= 0) {
  95.                 one = 0;
  96.                 oneid = -1;
  97.             }
  98.         }
  99.     }
  100.     if (sh) {
  101.         cout << "one = " << one << "\n";
  102.     }
  103.     //TWO
  104.     if (ls != -1) {
  105.         ll del = 0;
  106.         if (oneid != -1) {
  107.             del = pf[oneid];
  108.         }
  109.         ll pf2 = abs(pf[ls] - del);
  110.         two = dS * (ls - oneid) - pf2;
  111.     }
  112.     if (sh) {
  113.         cout << "two = " << two << "\n";
  114.     }
  115.     //THREE
  116.     if (ls != n - 1) {
  117.         del = 0;
  118.         if (ls >= 0) {
  119.             del = pf[ls];
  120.         }
  121.         ll pf3 = pf[n - 1] - del;
  122.         three = pf3 + dS * (n - 1 - ls);
  123.     }
  124.     if (sh) {
  125.         cout << "three = " << three << "\n";
  126.     }
  127.     if (sh) {
  128.         cout << "one two three  = " << one << ' ' << two << ' ' << three << "\n";
  129.     }
  130.     ll res = one + two + three;
  131.     return res;
  132. }
  133.  
  134. ll fls0() {
  135.  
  136. }
  137.  
  138. void slv() {
  139.     if (!random) {
  140.         cin >> n >> k;
  141.         a.resize(n);
  142.         for (ll &i : a) cin >> i;
  143.     } else {
  144.         n = rand() % 10 + 3;
  145.         k = rand() % 10 + 20;
  146.         a.resize(n);
  147.         for (int i = 0; i < n; ++i) {
  148.             a[i] = rand() % 100 - 30;
  149.         }
  150.     }
  151.     sort(a.begin(), a.end());
  152.     dulla = a;
  153.     pf.assign(n, a[0]);
  154.     for (int i = 1; i < n; ++i) {
  155.         pf[i] = pf[i - 1] + a[i];
  156.     }
  157.     int i = -1;
  158.     while (i + 1 < n && a[i + 1] < 0) {
  159.         i++;
  160.     }
  161.     ls = i;
  162.     if (sh) {
  163.         cout << "GO\n";
  164.         cout << "a\n";
  165.         cvl(a);
  166.         cout << "dulla\n";
  167.         cvl(dulla);
  168.         cout << "pf\n";
  169.         cvl(pf);
  170.         cout << "ls = " << ls << "\n";
  171.     }
  172.  
  173.     for (int go = 0; go < k; ++go) {
  174.         if (!random) {
  175.             cin >> x;
  176.         } else {
  177.             x = rand() % 10 + abs( pow(-1, rand() % 2) * 20 );
  178.         }
  179.         dS += x;
  180.         ll my;
  181.         if (dS >= 0) {
  182.             my = fmr0();
  183.         } else {
  184.             my = fls0();
  185.         }
  186.         ll stup = dull();
  187.         if (stup != my) {
  188.             cout << "WA\n";
  189.             cout << "a\n";
  190.             cvl(a);
  191.             cout << "pf.size() = " << pf.size() << "\n";
  192.             cout << "pf\n";
  193.             cvl(pf);
  194.             cout << "dS = " << dS << "\n";
  195.             cout << "my stup = " << my << ' ' << stup << "\n";
  196.             exit(0);
  197.         } else {
  198.             cout << "OK\n";
  199.         }
  200.     }
  201.     cout << "OK " << k << " tests \n";;
  202. }
  203. /*
  204. WA
  205. a
  206. -24 32 50 54
  207. pf.size() = 4
  208. pf
  209. -24 8 58 112
  210. dS = 21
  211. my stup = 196 202
  212. */
  213.  
  214. int main() {
  215.     /*ios::sync_with_stdio(0);
  216.     cin.tie(0);
  217.     cout.tie(0);*/
  218.     srand(time(0));
  219.     while (1) {
  220.         slv();
  221.     }
  222. }
  223.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement