#include <bits/stdc++.h>
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define debug(x) cout << "Line " << __LINE__ << ", " << #x << " is " << x << endl
#define all(x) x.begin(), x.end()
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define ll long long
#define ull unsigned long long
#define pll pair<ll, ll>
#define pii pair<int, int>
#define ld long double
#define nl endl
#define tb '\t'
#define sp ' '
#define sqr(x) (x)*(x)
#define arr3 array<int, 3>
using namespace std;
const int MX=30005, MOD=1000000007, BLOCK=160, INF=2e9+7, LG=62;
const ll INFF=1000000000000000007;
const ld ERR=1e-6, pi=3.14159265358979323846;
ll N, K, ans, sumL[2], sumR[2], best;
multiset<ll> L[2], R[2];
vector<pll> A;
void balance(int idx){
while(R[idx].size() && L[idx].size()<R[idx].size()){
ll tmp=*R[idx].begin();
R[idx].erase(R[idx].begin());
L[idx].insert(tmp);
sumR[idx]-=tmp;
sumL[idx]+=tmp;
}
while(L[idx].size() && L[idx].size()-1>R[idx].size()){
ll tmp=*L[idx].rbegin();
L[idx].erase(prev(L[idx].end()));
R[idx].insert(tmp);
sumL[idx]-=tmp;
sumR[idx]+=tmp;
}
}
void ins(ll x, ll idx){
if(L[idx].empty() || x<=*L[idx].rbegin()){
L[idx].insert(x);
sumL[idx]+=x;
}else{
R[idx].insert(x);
sumR[idx]+=x;
}
balance(idx);
}
void era(ll x, ll idx){
if(R[idx].count(x)){
R[idx].erase(R[idx].find(x));
sumR[idx]-=x;
}else{
L[idx].erase(L[idx].find(x));
sumL[idx]-=x;
}
balance(idx);
}
ll comp(ll x, ll y){
return (L[x].size()*y-sumL[x]) + (sumR[x]-R[x].size()*y);
}
int main(){
fastio;
cin >> K >> N;
for(int i=0; i<N; i++){
char a, c;
int b, d;
cin >> a >> b >> c >> d;
if(a==c){
ans+=abs(b-d);
}else{
ans++;
if(a=='B')
swap(b, d);
A.pb({b+d, min(b, d)});
}
}
sort(all(A));
N=A.size();
for(int i=0; i<N; i++){
ll op=A[i].se, ed=A[i].fi-A[i].se;
ins(op, 1);
ins(ed, 1);
}
best=INFF;
if(L[1].size())
best=min(best, comp(1, *L[1].rbegin()));
if(R[1].size())
best=min(best, comp(1, *R[1].begin()));
if(K==2){
for(int i=0; i<N; i++){
ll op=A[i].se, ed=A[i].fi-A[i].se;
ins(op, 0);
ins(ed, 0);
era(op, 1);
era(ed, 1);
if(L[0].size() && L[1].size())
best=min(best, comp(0, *L[0].rbegin()) + comp(1, *L[1].rbegin()));
if(R[0].size() && L[1].size())
best=min(best, comp(0, *R[0].begin()) + comp(1, *L[1].rbegin()));
if(L[0].size() && R[1].size())
best=min(best, comp(0, *L[0].rbegin()) + comp(1, *R[1].begin()));
if(R[0].size() && R[1].size())
best=min(best, comp(0, *R[0].begin()) + comp(1, *R[1].begin()));
}
}
if(best==INFF)
best=0;
cout << ans+best << nl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
2 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
2 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
70 ms |
11224 KB |
Output is correct |
13 |
Correct |
119 ms |
11260 KB |
Output is correct |
14 |
Correct |
99 ms |
10092 KB |
Output is correct |
15 |
Correct |
55 ms |
6736 KB |
Output is correct |
16 |
Correct |
70 ms |
11228 KB |
Output is correct |
17 |
Correct |
84 ms |
11204 KB |
Output is correct |
18 |
Correct |
84 ms |
11192 KB |
Output is correct |
19 |
Correct |
90 ms |
11176 KB |
Output is correct |
20 |
Correct |
83 ms |
11236 KB |
Output is correct |
21 |
Correct |
88 ms |
11308 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
316 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
320 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
320 KB |
Output is correct |
6 |
Correct |
1 ms |
320 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
324 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
2 ms |
340 KB |
Output is correct |
14 |
Correct |
2 ms |
340 KB |
Output is correct |
15 |
Correct |
2 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
2 ms |
328 KB |
Output is correct |
20 |
Correct |
2 ms |
340 KB |
Output is correct |
21 |
Correct |
2 ms |
340 KB |
Output is correct |
22 |
Correct |
2 ms |
340 KB |
Output is correct |
23 |
Correct |
2 ms |
340 KB |
Output is correct |
24 |
Correct |
2 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
324 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
320 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
2 ms |
340 KB |
Output is correct |
14 |
Correct |
2 ms |
340 KB |
Output is correct |
15 |
Correct |
2 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
2 ms |
340 KB |
Output is correct |
21 |
Correct |
2 ms |
332 KB |
Output is correct |
22 |
Correct |
2 ms |
368 KB |
Output is correct |
23 |
Correct |
2 ms |
340 KB |
Output is correct |
24 |
Correct |
2 ms |
340 KB |
Output is correct |
25 |
Correct |
184 ms |
11972 KB |
Output is correct |
26 |
Correct |
189 ms |
12312 KB |
Output is correct |
27 |
Correct |
332 ms |
13004 KB |
Output is correct |
28 |
Correct |
332 ms |
13540 KB |
Output is correct |
29 |
Correct |
342 ms |
13596 KB |
Output is correct |
30 |
Correct |
134 ms |
7340 KB |
Output is correct |
31 |
Correct |
202 ms |
12932 KB |
Output is correct |
32 |
Correct |
193 ms |
13504 KB |
Output is correct |
33 |
Correct |
207 ms |
13224 KB |
Output is correct |
34 |
Correct |
197 ms |
13480 KB |
Output is correct |
35 |
Correct |
188 ms |
13172 KB |
Output is correct |
36 |
Correct |
196 ms |
13352 KB |
Output is correct |
37 |
Correct |
197 ms |
12084 KB |
Output is correct |