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 0x3F3F3F3F3F3F3F3F
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int k, n, S = 0;
cin >> k >> n;
vector<int> id, p, s;
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);
}
if (id.empty())
{
cout << S << '\n';
return 0;
}
sort(id.begin(), id.end());
p = s = id;
for (int i = 1; i < p.size(); i++) p[i] += p[i - 1];
for (int i = (int)s.size() - 2; i >= 0; i--) s[i] += s[i + 1];
int res = inf;
for (int i = 0; i < id.size(); i++)
{
int sum = 0;
sum -= p[i];
if (0 <= i + 1 && i + 1 < s.size()) sum += s[i + 1];
res = min(res, sum + id[i] * (2 * (i + 1) - (int)id.size()));
}
cout << res + S + (int)id.size() / 2 << '\n';
}
Compilation message (stderr)
bridge.cpp: In function 'int main()':
bridge.cpp:31: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]
31 | for (int i = 1; i < p.size(); i++) p[i] += p[i - 1];
| ~~^~~~~~~~~~
bridge.cpp:34: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]
34 | for (int i = 0; i < id.size(); i++)
| ~~^~~~~~~~~~~
bridge.cpp:38:29: 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 <= i + 1 && i + 1 < s.size()) sum += s[i + 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... |