This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/*
// is short or still long ???
hollwo_pelw's template(short)
// Note : -Dhollwo_pelw_local
*/
#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/trie_policy.hpp>
// #include <ext/rope>
using namespace std;
// using namespace __gnu_pbds;
// using namespace __gnu_cxx;
void FAST_IO(string filein = "", string fileout = "", string fileerr = ""){
if (fopen(filein.c_str(), "r")){
freopen(filein.c_str(), "r", stdin);
freopen(fileout.c_str(), "w", stdout);
#ifdef hollwo_pelw_local
freopen(fileerr.c_str(), "w", stderr);
#endif
}
cin.tie(0), cout.tie(0) -> sync_with_stdio(0);
}
void Hollwo_Pelw();
signed main(){
#ifdef hollwo_pelw_local
FAST_IO("input.inp", "output.out", "error.err");
auto start = chrono::steady_clock::now();
#else
FAST_IO("hollwo_pelw.inp", "hollwo_pelw.out");
#endif
int testcases = 1;
// cin >> testcases;
for (int test = 1; test <= testcases; test++){
// cout << "Case #" << test << ": ";
Hollwo_Pelw();
}
#ifdef hollwo_pelw_local
auto end = chrono::steady_clock::now();
cout << "\nExcution time : " << chrono::duration_cast<chrono::milliseconds> (end - start).count() << "[ms]" << endl;
#endif
return 0;
}
const int N = 1e6 + 5;
#define int long long
int n, k, extra, pre[N];
struct person_t {
int a, b;
person_t (int _a = 0, int _b = 0)
: a(_a), b(_b) {}
bool operator < (const person_t &o) const {
return a + b < o.a + o.b;
}
};
vector<person_t> v;
int rtot, ltot;
priority_queue<int> pql, pqr;
inline void clear() {
rtot = ltot = 0;
while (pql.size()) pql.pop();
while (pqr.size()) pqr.pop();
}
inline void insert(int v) {
cout << "insert " << v << '\n';
int mid = pql.empty() ? 10000000007 : pql.top();
if (v <= mid) {
pql.push(+ v);
ltot += v;
}
else {
pqr.push(- v);
rtot += v;
}
// cout << ltot << ' ' << rtot << '\n';
if (pql.size() < pqr.size()) {
v = - pqr.top(), pqr.pop();
pql.push(+ v);
rtot -= v, ltot += v;
}
else if (pql.size() > pqr.size() + 1) {
v = + pql.top(), pql.pop();
pqr.push(- v);
ltot -= v, rtot += v;
}
}
inline int get() {
// cout << "-> " << rtot - ltot << '\n';
return rtot - ltot;
}
void Hollwo_Pelw() {
cin >> k >> n;
for (int i = 1, x, y; i <= n; i++) {
char p, q; cin >> p >> x >> q >> y;
if (p == q) {
// same side
extra += abs(x - y);
} else {
v.emplace_back(x, y);
}
}
n = v.size();
extra += n;
if (!n) return cout << extra, (void) 0;
clear();
for (int i = 0; i < n; i++) {
insert(v[i].a);
insert(v[i].b);
pre[i] = get();
}
int res = pre[n - 1];
if (k == 2) {
clear();
for (int i = n - 1; i; i--) {
insert(v[i].a);
insert(v[i].b);
res = min(res, get() + pre[i - 1]);
}
}
cout << res + extra;
}
Compilation message (stderr)
bridge.cpp: In function 'void FAST_IO(std::string, std::string, std::string)':
bridge.cpp:18:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
18 | freopen(filein.c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
bridge.cpp:19:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
19 | freopen(fileout.c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |