#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define bug cout << "bug" << endl
#define speed ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define all(x) x.begin(), x.end()
#define F first
#define S second
#define pll pair <ll, ll>
#define pii pair <int, int>
#define triple pair <pair <ll, ll> , ll>
#define ull unsigned long long
#define ld long double
#define pinode pair <node*, node*>
const ll INF = 9e18 + 5;
const ll inf = 1e9 + 5;
const ll N = 2e3 + 5;
const ll shift = 2e6;
const ll mod = 998244353;
const ll mod2 = 1e9 + 9;
const ll M = 1e3 + 5;
const ll LOG = 21;
const ll sp = 263;
const ll sp2 = 9973;
const int block = 100;
const double eps = 1e-10;
ll k, n, ans = 0;
vector <pll> vt;
int main(){
speed;
cin >> k >> n;
for(int i = 1; i <= n; i++){
char f1, f2;
ll p1, p2;
cin >> f1 >> p1 >> f2 >> p2;
if(f1 == f2){
ans += abs(p1 - p2);
continue;
}
if(p1 > p2)
swap(p1, p2);
vt.pb({p1, p2});
}
if(k == 1){
vector <ll> nw;
for(auto to : vt){
nw.pb(to.F);
nw.pb(to.S);
}
sort(all(nw));
ll pref[n + 5];
for(int i = 0; i < nw.size(); i++){
if(i == 0)
pref[i] = nw[i];
else
pref[i] = pref[i - 1] + nw[i];
// cout << pref[i] << " ";
}
// cout << endl;
ll res = INF;
for(int i = 0; i < nw.size(); i++){
ll col1 = i + 1, col2 = int(nw.size()) - col1;
ll cur = (col1 * nw[i] - pref[i]) + ((pref[nw.size() - 1] - pref[i]) - nw[i] * col2);
res = min(res, cur);
}
res += ans;
res += int(vt.size());
cout << res << endl;
return 0;
}
}
/*
%I64d6
1 5
B 0 A 4
B 1 B 3
A 5 B 7
B 2 A 6
B 1 A 7
%I64d
*/
Compilation message
bridge.cpp: In function 'int main()':
bridge.cpp:58:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
58 | for(int i = 0; i < nw.size(); i++){
| ~~^~~~~~~~~~~
bridge.cpp:67:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
67 | for(int i = 0; i < nw.size(); i++){
| ~~^~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
324 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |