#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define sz(x) (int)x.size()
#define ALL(v) v.begin(),v.end()
#define MASK(k) (1LL << (k))
#define BIT(x, i) (((x) >> (i)) & 1)
#define oo (ll)1e18
#define INF (ll)1e9
#define MOD (ll)(1e9 + 21071997)
using namespace std;
template<class T1, class T2>
bool maximize(T1 &a, T2 b){if(a < b){a = b; return true;} return false;}
template<class T1, class T2>
bool minimize(T1 &a, T2 b){if(a > b){a = b; return true;} return false;}
template<class T1, class T2>
void add(T1 &a, T2 b){a += b; if(a >= MOD) a -= MOD;}
template<class T1, class T2>
void sub(T1 &a, T2 b){a -= b; if(a < 0) a += MOD;}
template<class T>
void cps(T &v){sort(ALL(v)); v.resize(unique(ALL(v)) - v.begin());}
const int MAX = 1e5 + 10;
int K, N;
vector<pair<int, int>> a;
void solve(){
cin >> K >> N;
ll sum = 0;
for(int i=1; i<=N; i++){
char ta, tb;
int aa, bb;
cin >> ta >> aa >> tb >> bb;
if(ta == tb){
sum += abs(bb - aa);
}
else{
sum += abs(bb - aa) + 1;
a.push_back(make_pair(min(aa, bb), max(aa, bb)));
}
}
if(K == 1){
vector<int> ord, pMax, pMin;
for(auto o: a){
ord.push_back(o.fi);
ord.push_back(o.se);
pMax.push_back(o.se);
pMin.push_back(o.fi);
}
cps(ord);
sort(ALL(pMax));
sort(ALL(pMin));
vector<ll> pre(sz(a) + 3), suf(sz(a) + 3);
for(int i=1; i<=sz(a); i++){
pre[i] = pre[i - 1] + pMax[i - 1];
}
for(int i=sz(a); i>=1; i--){
suf[i] = suf[i + 1] + pMin[i - 1];
}
ll ans = oo;
for(int i: ord){
int l = lower_bound(ALL(pMax), i) - pMax.begin();
ll cur = 1LL * i * l - (pre[l]);
int r = upper_bound(ALL(pMin), i) - pMin.begin();
cur += suf[r + 1] - 1LL * i * (sz(a) - r);
minimize(ans, cur);
}
cout << sum + ans * 2 << '\n';
return;
}
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0);
// freopen("mmgraph.inp", "r", stdin);
// freopen("mmgraph.out", "w", stdout);
int t = 1;
// cin >> t;
while(t--){
solve();
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |