// In the name of God
// Hope is last to die
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define pb push_back
// #define int long long
#define S second
#define F first
#define mp make_pair
#define smax(x, y) (x) = max((x), (y))
#define smin(x, y) (x) = min((x), (y))
#define all(x) (x).begin(), (x).end()
#define len(x) ((int)(x).size())
const int maxn = 1e5 + 5;
const int inf = 1e9 + 7;
ll k, n, sup, slow, ps[maxn];
vector<pii> a;
multiset<int> up, low;
bool cmp(pii x, pii y){
return x.F + x.S < y.F + y.S;
}
void reset(){
if(up.size() > low.size()){
int x = (*up.begin());
low.insert(x);
up.erase(up.find(x));
slow += x;
sup -= x;
}
if(low.size() > up.size() + 1){
int x = (*low.rbegin());
low.erase(low.find(x));
up.insert(x);
sup += x;
slow -= x;
}
}
void insert(int x){
int mid = (low.size() ? (*low.rbegin()) : inf);
if(x > mid){
up.insert(x);
sup += x;
}
else{
low.insert(x);
slow += x;
}
reset();
}
int32_t main()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> k >> n;
ll sum = 0;
for(int i = 0; i < n; i++){
char h, g;
int u, v;
cin >> h >> u >> g >> v;
if(h == g) sum += abs(u - v);
else a.pb(mp(u, v));
}
sort(all(a), cmp);
n = a.size();
sum += n;
for(int i = 0; i < n; i++){
insert(a[i].S);
insert(a[i].F);
ps[i] = sup - slow;
}
ll ans = ps[n - 1] + sum;
if(k == 2){
low.clear();
up.clear();
slow = 0;
sup = 0;
for(int i = n - 1; i >= 1; i--){
insert(a[i].F);
insert(a[i].S);
smin(ans, sum + sup - slow + ps[i - 1]);
}
}
cout << ans << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
572 KB |
Output is correct |
10 |
Correct |
1 ms |
600 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
59 ms |
12032 KB |
Output is correct |
13 |
Correct |
88 ms |
13524 KB |
Output is correct |
14 |
Correct |
78 ms |
11316 KB |
Output is correct |
15 |
Correct |
51 ms |
8140 KB |
Output is correct |
16 |
Correct |
59 ms |
12796 KB |
Output is correct |
17 |
Correct |
64 ms |
13400 KB |
Output is correct |
18 |
Correct |
53 ms |
13016 KB |
Output is correct |
19 |
Correct |
71 ms |
13556 KB |
Output is correct |
20 |
Correct |
67 ms |
13008 KB |
Output is correct |
21 |
Correct |
69 ms |
13264 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
468 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
468 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
344 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
344 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
344 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
488 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
102 ms |
12136 KB |
Output is correct |
26 |
Correct |
146 ms |
12236 KB |
Output is correct |
27 |
Correct |
150 ms |
13004 KB |
Output is correct |
28 |
Correct |
156 ms |
13516 KB |
Output is correct |
29 |
Correct |
150 ms |
13648 KB |
Output is correct |
30 |
Correct |
71 ms |
7156 KB |
Output is correct |
31 |
Correct |
88 ms |
12708 KB |
Output is correct |
32 |
Correct |
101 ms |
13260 KB |
Output is correct |
33 |
Correct |
80 ms |
12940 KB |
Output is correct |
34 |
Correct |
101 ms |
13008 KB |
Output is correct |
35 |
Correct |
108 ms |
12784 KB |
Output is correct |
36 |
Correct |
103 ms |
13008 KB |
Output is correct |
37 |
Correct |
101 ms |
11844 KB |
Output is correct |