This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
//#pragma GCC optimize("O2")
using namespace std;
using ll = long long;
using ld = long double;
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define sz(x) (int)x.size()
#define endl '\n'
const int mod = 1e9 + 7;
const int inf = 2e9 + 5;
const ll linf = 9e18 + 5;
int n, k;
const int N = 1e5 + 5;
ll arr[N][2];
pair<int, int> have[2 * N];
ll base = 0;
bool cmp(pair<int, int> &a, pair<int, int> &b) {
return mp(arr[a.fi][a.se], a.se) < mp(arr[b.fi][b.se], b.se);
}
void init() {
}
void input() {
cin >> k >> n;
for (int i = 1; i <= n; i++) {
char a, b;
cin >> a >> arr[i][0] >> b >> arr[i][1];
base += abs(arr[i][0] - arr[i][1]);
if (a == b) {
i--;
n--;
}
else {
if (arr[i][0] > arr[i][1]) {
swap(arr[i][0], arr[i][1]);
}
base++;
}
}
}
void solve1() {
if (k != 1) {
return;
}
if (!n) {
cout << base;
return;
}
for (int i = 1; i <= n; i++) {
have[2 * i - 1] = mp(i, 0);
have[2 * i] = mp(i, 1);
}
sort(have + 1, have + 2 * n + 1, cmp);
ll sum = 0;
int cnt = -n + 1;
for (int i = 2; i <= 2 * n; i++) {
if (have[i].se == 0) {
sum += arr[have[i].fi][have[i].se];
}
}
ll ans = linf;
for (int i = 1; i <= 2 * n; i++) {
int x = arr[have[i].fi][have[i].se];
ll now = sum + 1ll * cnt * x;
ans = min(ans, now);
if (have[i].se == 1) {
cnt++;
sum -= arr[have[i].fi][have[i].se];
}
if (i < 2 * n && have[i + 1].se == 0) {
cnt++;
sum -= arr[have[i + 1].fi][have[i + 1].se];
}
}
cout << base + 2 * ans;
}
int x(int i) {
return arr[have[i].fi][have[i].se];
}
void solve2() {
if (k != 2) {
return;
}
for (int i = 1; i <= n; i++) {
have[2 * i - 1] = mp(i, 0);
have[2 * i] = mp(i, 1);
}
sort(have + 1, have + 2 * n + 1, cmp);
ll ans = linf;
int cnt1 = 0;
int cnt2 = 0;
ll sum = 0;
for (int i = 1; i <= 2 * n; i++) {
if (x(i) > x(1) && have[i].se == 0) {
sum += x(i);
cnt2--;
}
}
set<pair<int, int>> betw;
int i = 1;
int j = 1;
while (i < 2 * n) {
ll now = sum + 1ll * cnt1 * x(i) + 1ll * cnt2 * x(j);
ans = min(ans, now);
while ((j < 2 * n && cnt2 < 0) || (i == j)) {
if (have[j].se == 1) {
int ind = have[j].fi;
if (x(i) < arr[ind][0]) {
betw.insert(mp(arr[ind][0] + arr[ind][1], ind));
sum -= arr[ind][1];
cnt2++;
}
}
j++;
if (have[j].se == 0) {
sum -= x(j);
cnt2++;
}
while (!betw.empty()) {
if (betw.begin()->fi > x(i) + x(j)) {
break;
}
sum += arr[betw.begin()->se][1];
cnt2--;
sum += arr[betw.begin()->se][0];
cnt1--;
betw.erase(betw.begin());
}
}
if (have[i].se == 1) {
sum -= x(i);
cnt1++;
}
i++;
if (have[i].se == 0) {
sum -= x(i);
cnt1++;
}
while (!betw.empty()) {
if (betw.begin()->fi > x(i) + x(j)) {
break;
}
sum += arr[betw.begin()->se][1];
cnt2--;
sum += arr[betw.begin()->se][0];
cnt1--;
betw.erase(betw.begin());
}
}
cout << base + 2 * ans;
}
void output() {
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int number_of_testcases = 1;
//cin >> number_of_testcases;
while (number_of_testcases--) {
init();
input();
solve1();
solve2();
output();
}
return 0;
}
# | 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... |