Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- #include <vector>
- #include <queue>
- #include <algorithm>
- #include <string>
- #include <stack>
- #include <set>
- #include <map>
- #define pii pair <int, int>
- #define pb(x) push_back(x)
- #include <ctime>
- using namespace std;
- using ll = long long;
- using ld = long double;
- using db = double;
- void cv(vector <int> &v) {
- for (auto x : v) cout << x << ' ';
- cout << "\n";
- }
- void cvl(vector <ll> &v) {
- for (auto x : v) cout << x << ' ';
- cout << "\n";
- }
- void cvv(vector <vector <int> > &v) {
- for (auto x : v) cv(x);
- cout << "\n";
- }
- void cvb(vector <bool> v) {
- for (bool x : v) cout << x << ' ';
- cout << "\n";
- }
- void cvs(vector <string> v) {
- for (auto a : v) {
- cout << a << "\n";
- }
- }
- void cvp(vector <pii> a) {
- for (auto p : a) {
- cout << p.first << ' ' << p.second << "\n";
- }
- cout << "\n";
- }
- vector <ll> a, pf;
- int n, ls = -1, k; // last < 0
- ll x, dS = 0;
- vector <ll> dulla;
- bool random = 1;
- bool sh = 0;
- ll dull() {
- ll r = 0;
- for (int i = 0; i < n; ++i) {
- dulla[i] += x;
- r += abs(dulla[i]);
- }
- return r;
- }
- ll fmr0() {
- ll one = 0, two = 0, three = 0;
- //ONE
- int oneid = -1;
- ll del = 0;
- if (ls != -1) {
- if (ls == 0) {
- if (abs(a[0]) + dS < 0) {
- one = abs(a[0]) - dS;
- oneid = 0;
- }
- } else {
- int L = 0, R = ls + 1, M;
- while (L + 1 < R) {
- M = (L + R) / 2;
- if (a[M] + dS < 0) {
- L = M;
- } else {
- R = M;
- }
- }
- one = abs(pf[L]) - dS;
- oneid = L;
- if (a[0] + dS >= 0) {
- one = 0;
- oneid = -1;
- }
- }
- }
- if (sh) {
- cout << "one = " << one << "\n";
- }
- //TWO
- if (ls != -1) {
- ll del = 0;
- if (oneid != -1) {
- del = pf[oneid];
- }
- ll pf2 = abs(pf[ls] - del);
- two = dS * (ls - oneid) - pf2;
- }
- if (sh) {
- cout << "two = " << two << "\n";
- }
- //THREE
- if (ls != n - 1) {
- del = 0;
- if (ls >= 0) {
- del = pf[ls];
- }
- ll pf3 = pf[n - 1] - del;
- three = pf3 + dS * (n - 1 - ls);
- }
- if (sh) {
- cout << "three = " << three << "\n";
- }
- if (sh) {
- cout << "one two three = " << one << ' ' << two << ' ' << three << "\n";
- }
- ll res = one + two + three;
- return res;
- }
- ll fls0() {
- }
- void slv() {
- if (!random) {
- cin >> n >> k;
- a.resize(n);
- for (ll &i : a) cin >> i;
- } else {
- n = rand() % 10 + 3;
- k = rand() % 10 + 20;
- a.resize(n);
- for (int i = 0; i < n; ++i) {
- a[i] = rand() % 100 - 30;
- }
- }
- sort(a.begin(), a.end());
- dulla = a;
- pf.assign(n, a[0]);
- for (int i = 1; i < n; ++i) {
- pf[i] = pf[i - 1] + a[i];
- }
- int i = -1;
- while (i + 1 < n && a[i + 1] < 0) {
- i++;
- }
- ls = i;
- if (sh) {
- cout << "GO\n";
- cout << "a\n";
- cvl(a);
- cout << "dulla\n";
- cvl(dulla);
- cout << "pf\n";
- cvl(pf);
- cout << "ls = " << ls << "\n";
- }
- for (int go = 0; go < k; ++go) {
- if (!random) {
- cin >> x;
- } else {
- x = rand() % 10 + abs( pow(-1, rand() % 2) * 20 );
- }
- dS += x;
- ll my;
- if (dS >= 0) {
- my = fmr0();
- } else {
- my = fls0();
- }
- ll stup = dull();
- if (stup != my) {
- cout << "WA\n";
- cout << "a\n";
- cvl(a);
- cout << "pf.size() = " << pf.size() << "\n";
- cout << "pf\n";
- cvl(pf);
- cout << "dS = " << dS << "\n";
- cout << "my stup = " << my << ' ' << stup << "\n";
- exit(0);
- } else {
- cout << "OK\n";
- }
- }
- cout << "OK " << k << " tests \n";;
- }
- /*
- WA
- a
- -24 32 50 54
- pf.size() = 4
- pf
- -24 8 58 112
- dS = 21
- my stup = 196 202
- */
- int main() {
- /*ios::sync_with_stdio(0);
- cin.tie(0);
- cout.tie(0);*/
- srand(time(0));
- while (1) {
- slv();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement