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;
#pragma GCC optimize("Ofast")
typedef long long ll;
// const ll mod = 1e9 + 7;
const ll MAXN = 1e6 + 5;
#define vi vector<int>
#define vll vector<ll>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define mp make_pair
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define fi first
#define sc second
#define endl '\n'
#define gl ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
int main()
{
gl;
ll k, n, sum = 0, x, y, ans = 1e18;
char a, b;
set<ll> st;
cin >> k >> n;
vll v;
v.pb(0);
for (int i = 0; i < n; i++)
{
cin >> a >> x >> b >> y;
if (a == b)
sum += abs(x - y);
else
{
sum++;
v.pb(x);
v.pb(y);
st.insert(x);
st.insert(y);
}
}
sort(v.begin(), v.end());
vll ps(v.size(), 0);
for (int i = 1; i < v.size(); i++)
{
ps[i] = v[i];
ps[i] += ps[i - 1];
}
for (ll x : st)
{
ll le = 0, ri = v.size() - 1, pos = 0;
while (le <= ri)
{
ll m = (le + ri) / 2;
if (v[m] <= x)
{
pos = m;
le = m + 1;
}
else
ri = m - 1;
}
ans = min(ans, pos * x - ps[pos] + ps[v.size() - 1] - ps[pos] - ((ll)v.size() - pos - 1) * x);
}
if (ans == 1e18)
ans = 0;
cout << ans + sum << endl;
return 0;
}
Compilation message (stderr)
bridge.cpp: In function 'int main()':
bridge.cpp:51:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
51 | for (int i = 1; i < v.size(); i++)
| ~~^~~~~~~~~~| # | 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... |