This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define inf 0x3F3F3F3F
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int k, n, S = 0;
cin >> k >> n;
vector<int> id, L, pL, sL, R, pR, sR;
for (int i = 0; i < n; i++)
{
char a, b;
int l, r;
cin >> a >> l >> b >> r;
if (l > r) swap(l, r);
if (a == b) S += r - l;
else id.push_back(l), id.push_back(r), L.push_back(r), R.push_back(l);
}
sort(id.begin(), id.end()), sort(L.begin(), L.end()), sort(R.begin(), R.end());
id.resize(unique(id.begin(), id.end()) - id.begin());
pL = sL = L, pR = sR = R;
for (int i = 1; i < pL.size(); i++) pL[i] += pL[i - 1];
for (int i = 1; i < pR.size(); i++) pR[i] += pR[i - 1];
for (int i = (int)sL.size() - 2; i >= 0; i--) sL[i] += sL[i + 1];
for (int i = (int)sR.size() - 2; i >= 0; i--) sR[i] += sR[i + 1];
int res = inf;
for (int i : id)
{
int sum = 0, pi = lower_bound(L.begin(), L.end(), i) - L.begin() - 1, si = upper_bound(R.begin(), R.end(), i) - R.begin(), cnt = 0;
if (0 <= pi && pi < L.size()) sum -= pL[pi], cnt += pi + 1;
if (0 <= pi + 1 && pi + 1 < L.size()) sum += sL[pi + 1];
if (0 <= si && si < R.size()) sum += sR[si], cnt -= (int)R.size() - si;
if (0 <= si - 1 && si - 1 < R.size()) sum -= pR[si - 1];
res = min(res, sum + 2 * cnt * i + S + (int)L.size());
}
cout << res << '\n';
}
Compilation message (stderr)
bridge.cpp: In function 'int main()':
bridge.cpp:27:21: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
27 | for (int i = 1; i < pL.size(); i++) pL[i] += pL[i - 1];
| ~~^~~~~~~~~~~
bridge.cpp:28:21: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
28 | for (int i = 1; i < pR.size(); i++) pR[i] += pR[i - 1];
| ~~^~~~~~~~~~~
bridge.cpp:35:23: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
35 | if (0 <= pi && pi < L.size()) sum -= pL[pi], cnt += pi + 1;
| ~~~^~~~~~~~~~
bridge.cpp:36:31: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
36 | if (0 <= pi + 1 && pi + 1 < L.size()) sum += sL[pi + 1];
| ~~~~~~~^~~~~~~~~~
bridge.cpp:37:23: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
37 | if (0 <= si && si < R.size()) sum += sR[si], cnt -= (int)R.size() - si;
| ~~~^~~~~~~~~~
bridge.cpp:38:31: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
38 | if (0 <= si - 1 && si - 1 < R.size()) sum -= pR[si - 1];
| ~~~~~~~^~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |