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], med2[N][2], p, q;
char ch1, ch2;
ll base;
vector<pair<ll, pair<ll, ll> > > ve;
priority_queue<int> q1, q3;
priority_queue<int, vector<int>, greater<int> > q2, q4;
int main()
{
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin >> k >> n;
for(int i = 0; i < n; i++)
{
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 < ve.size(); i++)
{
auto x = ve[i];
q1.push(x.S.F);
q2.push(x.S.S);
while(q1.top() > q2.top())
{
ll 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(!n)
//cout << base << endl;
if(!ve.size())
{
cout << base << endl;
return 0;
}
if(k == 1|| ve.size() == 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;
}
else
{
ll ans = 1e18 + 5;
for(int i = ve.size() - 1; i >= 0; i--)
{
auto x = ve[i];
q3.push(x.S.F);
q4.push(x.S.S);
while(q3.top() > q4.top())
{
ll to1 = q3.top(), to2 = q4.top();
q3.pop();q4.pop();
q3.push(to2);
q4.push(to1);
}
med2[i][0] = q3.top();
med2[i][1] = q4.top();
}
for(int i = 0; i < ve.size() - 1; i++)
{
ll ans1, ans2, ans3, ans4;
ans1 = ans2 = ans3 = ans4 = 0;
for(int j = 0; j <= i; j++)
{
ans1 += abs(ve[j].S.F - med[i][0]);
ans1 += abs(ve[j].S.S - med[i][0]);
ans2 += abs(ve[j].S.F - med[i][1]);
ans2 += abs(ve[j].S.S - med[i][1]);
}
for(int j = i + 1; j < ve.size(); j++)
{
ans3 += abs(ve[j].S.F - med2[i + 1][0]);
ans3 += abs(ve[j].S.S - med2[i + 1][0]);
ans4 += abs(ve[j].S.F - med2[i + 1][1]);
ans4 += abs(ve[j].S.S - med2[i + 1][1]);
}
ans = min(ans, min(ans1, ans2) + min(ans3, ans4));
}
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]);
}
ans = min(ans, min(ans1, ans2));
cout << ans + base << endl;
}
}
Compilation message (stderr)
bridge.cpp: In function 'int main()':
bridge.cpp:40:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, std::pair<long long int, long long int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
40 | for(int i = 0; i < ve.size(); i++)
| ~~^~~~~~~~~~~
bridge.cpp:72:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, std::pair<long long int, long long int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
72 | for(int i = 0; i < ve.size(); i++)
| ~~^~~~~~~~~~~
bridge.cpp:102:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, std::pair<long long int, long long int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
102 | for(int i = 0; i < ve.size() - 1; i++)
| ~~^~~~~~~~~~~~~~~
bridge.cpp:113:34: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, std::pair<long long int, long long int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
113 | for(int j = i + 1; j < ve.size(); j++)
| ~~^~~~~~~~~~~
bridge.cpp:126:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, std::pair<long long int, long long int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
126 | 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... |