Advertisement
Korotkodul

reg C stress2

Oct 6th, 2022 (edited)
631
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.64 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.  
  168. }
  169.  
  170. void slv() {
  171.     dS = 0;
  172.     vector <ll> alx;
  173.     aldul.clear();
  174.     if (!random) {
  175.         cin >> n >> k;
  176.         a.resize(n);
  177.         for (ll &i : a) cin >> i;
  178.     } else {
  179.         n = rand() % 10 + 3;
  180.         k = rand() % 10 + 20;
  181.         a.resize(n);
  182.         for (int i = 0; i < n; ++i) {
  183.             a[i] = rand() % 100 - 30;
  184.         }
  185.     }
  186.     sort(a.begin(), a.end());
  187.     dulla = a;
  188.     pf.assign(n, a[0]);
  189.     for (int i = 1; i < n; ++i) {
  190.         pf[i] = pf[i - 1] + a[i];
  191.     }
  192.     int i = -1;
  193.     while (i + 1 < n && a[i + 1] < 0) {
  194.         i++;
  195.     }
  196.     ls = i;
  197.     if (sh) {
  198.         cout << "GO\n";
  199.         cout << "a\n";
  200.         cvl(a);
  201.         cout << "dulla\n";
  202.         cvl(dulla);
  203.         cout << "pf\n";
  204.         cvl(pf);
  205.         cout << "ls = " << ls << "\n";
  206.     }
  207.  
  208.     for (int go = 0; go < k; ++go) {
  209.         if (!random) {
  210.             cin >> x;
  211.         } else {
  212.             x = rand() % 10 + abs( pow(-1, rand() % 2) * 20 );
  213.         }
  214.         alx.push_back(x);
  215.         dS += x;
  216.         ll my;
  217.         if (dS >= 0) {
  218.             my = fmr0();
  219.         } else {
  220.             my = fls0();
  221.         }
  222.         ll stup = dull();
  223.         if (stup != my) {
  224.             cout << "WA\n";
  225.             cout << "a\n";
  226.             cvl(a);
  227.             cout << "pf.size() = " << pf.size() << "\n";
  228.             cout << "pf\n";
  229.             cvl(pf);
  230.             cout << "dS = " << dS << "\n";
  231.             cout << "my stup = " << my << ' ' << stup << "\n";
  232.             cout << "alx\n";
  233.             cvl(alx);
  234.             cout << "aldul\n";
  235.             for (auto v: aldul) {
  236.                 cvl(v);
  237.             } cout << "\n\n";
  238.             exit(0);
  239.         } else {
  240.             cout << "OK\n";
  241.         }
  242.     }
  243.     cout << "OK " << k << " tests \n";;
  244. }
  245. /*
  246. 9 1
  247. -29 -25 -20 2 5 13 32 45 58
  248. 20
  249.  
  250. WA
  251. a
  252. -29 -25 -20 2 5 13 32 45 58
  253. pf.size() = 9
  254. pf
  255. -29 -54 -74 -72 -67 -54 -22 23 81
  256. dS = 20
  257. my stup = 309 289
  258. alx
  259. 20
  260. aldul
  261. -9 -5 0 22 25 33 52 65 78
  262. */
  263.  
  264. int main() {
  265.     /*ios::sync_with_stdio(0);
  266.     cin.tie(0);
  267.     cout.tie(0);*/
  268.     srand(time(0));
  269.     while (1) {
  270.         slv();
  271.     }
  272. }
  273.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement