//#pragma warning( disable : 4996 )
#include <bits/stdc++.h>
//#pragma GCC target ("avx2")
//#pragma GCC optimization ("O3")
//#pragma GCC optimization ("unroll-loops")
using namespace std;
int N, K, temp1, temp2;
char tmp1, tmp2;
const long long int INTMAX = 1e18 + 4;
int main() {
/*
string problemName = "";
ifstream fin(problemName + ".in");
ofstream fout(problemName + ".out");
*/
ios::sync_with_stdio(0);
cin.tie(0);
cin >> K >> N;
bool changedr = false;
long long int oldlmedian = 0;
long long int sum = 0;
multiset < int > ::iterator lmedian;
multiset < int > ::iterator rmedian;
long long int cursumleft = 0;
long long int cursumright = 0;
long long int curmin = INTMAX;
int intervalnum = 0;
vector < pair < int, pair < int, int >>> intervals;
multiset < int > significantleft;
multiset < int > significantright;
for (int i = 0; i < N; i++) {
cin >> tmp1 >> temp1 >> tmp2 >> temp2;
if (tmp1 == tmp2) {
sum += abs(temp2 - temp1);
}
else {
sum++;
intervalnum++;
if (temp1 > temp2) {
swap(temp1, temp2);
}
intervals.push_back({
temp1 + temp2,
{
temp1,
temp2
}
});
significantright.insert(temp1);
significantright.insert(temp2);
}
}
if (intervals.size()) {
sort(intervals.begin(), intervals.end());
rmedian = next(significantright.begin(), intervalnum);
for (const int& i : significantright) {
cursumright += abs(i - *rmedian);
}
curmin = cursumright;
if (K == 1) {
cout << curmin + sum;
}
else {
temp1 = intervals[0].second.first;
temp2 = intervals[0].second.second;
significantleft.insert(temp1);
significantleft.insert(temp2);
if (significantright.lower_bound(temp2) == rmedian) {
changedr = true;
rmedian++;
}
significantright.erase(significantright.lower_bound(temp1));
significantright.erase(significantright.lower_bound(temp2));
lmedian = next(significantleft.begin(), 1);
cursumleft += (temp2 - temp1);
cursumright -= (abs(*rmedian - temp2) + abs(*rmedian - temp1));
if (temp2 <= *rmedian && !changedr) {
rmedian++;
}
curmin = min(curmin, cursumleft + cursumright);
for (int i = 1; i < intervalnum-1; i++) {
changedr = false;
temp1 = intervals[i].second.first;
temp2 = intervals[i].second.second;
significantleft.insert(temp1);
significantleft.insert(temp2);
if (significantright.lower_bound(temp2) == rmedian) {
changedr = true;
rmedian++;
}
significantright.erase(significantright.lower_bound(temp1));
significantright.erase(significantright.lower_bound(temp2));
cursumleft += (abs(*lmedian - temp2) + abs(*lmedian - temp1));
if (temp1 >= *lmedian) {
lmedian++;
}
else if (temp2 < *lmedian) {
oldlmedian = *lmedian;
lmedian--;
cursumleft -= (2 * abs(oldlmedian - *lmedian));
}
if (changedr) {
cursumright -= (temp2 - temp1);
}
else {
cursumright -= (abs(*rmedian - temp2) + abs(*rmedian - temp1));
}
if (temp2 <= *rmedian && !changedr) {
rmedian++;
}
curmin = min(curmin, cursumleft + cursumright);
}
cout << curmin + sum;
}
}
else {
cout << sum;
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
2 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
72 ms |
10764 KB |
Output is correct |
13 |
Correct |
142 ms |
10824 KB |
Output is correct |
14 |
Correct |
121 ms |
9684 KB |
Output is correct |
15 |
Correct |
78 ms |
6528 KB |
Output is correct |
16 |
Correct |
95 ms |
10848 KB |
Output is correct |
17 |
Correct |
85 ms |
10776 KB |
Output is correct |
18 |
Correct |
97 ms |
10868 KB |
Output is correct |
19 |
Correct |
116 ms |
10772 KB |
Output is correct |
20 |
Correct |
86 ms |
10828 KB |
Output is correct |
21 |
Correct |
95 ms |
10872 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
308 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
316 KB |
Output is correct |
13 |
Correct |
1 ms |
316 KB |
Output is correct |
14 |
Correct |
2 ms |
332 KB |
Output is correct |
15 |
Correct |
2 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
2 ms |
320 KB |
Output is correct |
21 |
Correct |
2 ms |
332 KB |
Output is correct |
22 |
Correct |
2 ms |
456 KB |
Output is correct |
23 |
Correct |
2 ms |
400 KB |
Output is correct |
24 |
Correct |
2 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
2 ms |
332 KB |
Output is correct |
15 |
Correct |
2 ms |
388 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
2 ms |
404 KB |
Output is correct |
24 |
Correct |
1 ms |
332 KB |
Output is correct |
25 |
Correct |
137 ms |
11604 KB |
Output is correct |
26 |
Correct |
222 ms |
11768 KB |
Output is correct |
27 |
Correct |
342 ms |
12532 KB |
Output is correct |
28 |
Correct |
394 ms |
13028 KB |
Output is correct |
29 |
Correct |
344 ms |
13096 KB |
Output is correct |
30 |
Correct |
124 ms |
7036 KB |
Output is correct |
31 |
Correct |
177 ms |
12408 KB |
Output is correct |
32 |
Correct |
152 ms |
13044 KB |
Output is correct |
33 |
Correct |
157 ms |
12732 KB |
Output is correct |
34 |
Correct |
178 ms |
13040 KB |
Output is correct |
35 |
Correct |
143 ms |
12664 KB |
Output is correct |
36 |
Correct |
146 ms |
12784 KB |
Output is correct |
37 |
Correct |
140 ms |
11604 KB |
Output is correct |