Advertisement
Korotkodul

reg C

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