#ifdef LOCAL
#define _GLIBCXX_DEBUG
#else
#pragma GCC optimize("O3,Ofast,unroll-loops")
#endif
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <cstdio>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <bitset>
#include <algorithm>
#include <cmath>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <chrono>
#include <random>
#include <complex>
#include <numeric>
#include <assert.h>
#include <functional>
#include <climits>
#include <cstring>
#include <iterator>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
using ushort = unsigned short;
#define int ll
#ifndef LOCAL
#define endl "\n"
#endif
#define f first
#define s second
#define vec vector
#define pii pair<int, int>
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define pb push_back
#define mp make_pair
template<typename T1, typename T2>
istream& operator>> (istream &in, pair<T1, T2> &p) { in >> p.f >> p.s; return in; }
template<typename T1, typename T2>
ostream& operator<< (ostream &out, pair<T1, T2> p) { out << "(" << p.f << " " << p.s << ")"; return out; }
#define printv(v) cerr << #v << " "; for (auto &x : v) { cerr << x << " "; } cerr << endl;
#define printvv(v) cerr << #v << " "; for (auto &x : v) { for (auto &y : x) { cerr << y << " "; } cerr << endl; }
#define debug(x) cerr << #x << " " << x << endl;
template<typename T>
istream& operator>> (istream &in, vector<T> &v) {
for (auto &x : v) {
in >> x;
}
return in;
}
template<typename T>
ostream& operator<< (ostream &out, vector<T> v) {
for (auto &x : v) {
out << x << " ";
}
return out;
}
template<typename T>
void operator-=(vector<T> &v, int delta) {
for (auto &x : v) {
x -= delta;
}
}
template<typename T>
void operator+=(vector<T> &v, int delta) {
for (auto &x : v) {
x += delta;
}
}
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
signed Main();
void Solve();
void PreCalc();
static void run_with_stack_size(signed (*func)(), size_t stsize) {
char *stack, *send;
stack = (char *)malloc(stsize);
send = stack+stsize-16;
send = (char *)((uintptr_t)send/16*16);
asm volatile(
"mov %%rsp, (%0)\n"
"mov %0, %%rsp\n"
:
: "r" (send));
func();
asm volatile(
"mov (%0), %%rsp\n"
:
: "r" (send));
free(stack);
}
signed main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
int tStart = clock();
run_with_stack_size(Main, 1024*1024*1024);
cout << endl << "execution time : " << (clock() - tStart) / 1e3 << endl;
#else
ios::sync_with_stdio(false);
cin.tie(0);
Main();
#endif
}
signed Main() {
PreCalc();
int t = 1;
// cin >> t;
int test = 0;
while (t--) {
++test;
#ifdef LOCAL
cout << "--------- TEST #" << test << " ----------" << endl;
#endif
Solve();
}
return 0;
}
/* This code is praying to GOD for protecting from bugs and "other things" */
void PreCalc() {
}
vector<int> get(vector<pii> a) {
vector<int> R(a.size() + 1);
multiset<int> A, B;
int sA = a[0].f, sB = a[0].s;
A.insert(a[0].f);
B.insert(a[0].s);
R[0] = 0;
R[1] = sB - sA;
for (int i = 1; i < a.size(); ++i) {
if ((*B.begin()) > a[i].f) {
A.insert(a[i].f);
sA += a[i].f;
} else {
B.insert(a[i].f);
sB += a[i].f;
}
if ((*B.begin()) > a[i].s) {
A.insert(a[i].s);
sA += a[i].s;
} else {
B.insert(a[i].s);
sB += a[i].s;
}
while (A.size() > B.size()) {
auto it = A.end();
it--;
sA -= (*it);
B.insert((*it));
sB += (*it);
A.erase(it);
}
while (B.size() > A.size()) {
auto it = B.begin();
sB -= (*it);
A.insert((*it));
sA += (*it);
B.erase(it);
}
R[i + 1] = sB - sA;
}
return R;
}
void Solve() {
int k, n;
cin >> k >> n;
vector<pii> a;
int ans = 0;
for (int i = 0; i < n; ++i) {
char p, q;
int s, t;
cin >> p >> s >> q >> t;
if (p != q) {
if (s < t) {
a.pb(mp(s, t));
} else {
a.pb(mp(t, s));
}
++ans;
} else {
ans += abs(s - t);
}
}
if (!a.empty()) {
sort(all(a), [&](pii x, pii y) {
return x.f + x.s < y.f + y.s;
});
for (auto &x : a) {
cout << x << endl;
}
vector<int> X = get(a);
if (k == 1) {
ans += X.back();
} else {
reverse(all(a));
for (auto &x: a) {
x.f = -x.f;
x.s = -x.s;
swap(x.f, x.s);
}
vector<int> Y = get(a);
int best = 1e18;
for (int i = 0; i < X.size(); ++i) {
cout << i << ' ' << X[i] + Y[X.size() - i - 1] << endl;
best = min(best, X[i] + Y[X.size() - i - 1]);
}
ans += best;
}
}
cout << ans << endl;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |