This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//In the name of Allah
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define endl '\n'
#define F first
#define S second
#define pb push_back
#define all(x) x.begin(), x.end()
const int N = 1e5 + 5;
int n, k, med[N][2], base;
vector<pair<int, pair<int, int> > > ve;
int main()
{
priority_queue<int> q1;
priority_queue<int, vector<int>, greater<int> > q2;
cin >> k >> n;
for(int i = 0; i < n; i++)
{
char ch1, ch2;
int p, q;
cin >> ch1 >> p >> ch2 >> q;
if(ch1 != ch2)
{
ve.pb({p + q, {min(p, q), max(p, q)}});
base ++;
}
else
base += abs(p - q);
}
sort(all(ve));
for(int i = 0; i < (int)ve.size(); i++)
{
auto x = ve[i];
q1.push(x.S.F);
q2.push(x.S.S);
while(q1.top() > q2.top())
{
int to1 = q1.top(), to2 = q2.top();
q1.pop();q2.pop();
q1.push(to2);
q2.push(to1);
}
med[i][0] = q1.top();
med[i][1] = q2.top();
}
//cout << med[ve.size() - 1][0] << " " << med[ve.size() - 1][1] << endl;
if(k == 1)
{
ll ans1, ans2;
ans1 = ans2 = 0;
for(int i = 0; i < ve.size(); i++)
{
ans1 += abs(ve[i].S.F - med[ve.size() - 1][0]);
ans1 += abs(ve[i].S.S - med[ve.size() - 1][0]);
ans2 += abs(ve[i].S.F - med[ve.size() - 1][1]);
ans2 += abs(ve[i].S.S - med[ve.size() - 1][1]);
}
cout << min(ans1, ans2) + base << endl;
}
}
/*
1 5 B 0 A 4 B 1 B 3 A 5 B 7 B 2 A 6 B 1 A 7
*/
Compilation message (stderr)
bridge.cpp: In function 'int main()':
bridge.cpp:60:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, std::pair<int, int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
60 | for(int i = 0; i < ve.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... |