This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//******************/
//* I<3 C++ */
//* I WANT ANY AC */
//* I LOVE PROGRAM!*/
//*IT'S long longERESTING*/
//* I LOVE PROGRAM!*/
//* IN CONTESTS */
//* GET SCORE */
//* AC CODE */
//* LET'S */
//* GO */
//* Written by: */
//* Duc Minh */
#include <bits/stdc++.h>
using namespace std;
#define TIME (1.0 * clock() / CLOCKS_PER_SEC)
#define all(a) a.begin(),a.end()
// #define push_back emplace_back
#define endl "\n"
#define all1(a) a+1,a+n+1
// #define gcd(a,b) __gcd(a,b);
// #define lcm(a,b) (a*b)/gcd(a,b);
const long long INF = (long long)1e9;
const long long MOD = (long long)1e9+7;
const long long MODD = 998244353;
const long long maxN = 2e6;
// const unsigned unsigned long long du=1000000007;
// unsigned unsigned long long w[20],v[20];
// map<unsigned unsigned long long,unsigned unsigned long long> q;
// unsigned unsigned long long n,ans=INF;
// unsigned unsigned unsigned long long a[200009],pre[200009],x=0,sum=0;
// unsigned unsigned long long n,h[1000009],j[1000009],b[1000009];
// unsigned unsigned long long t,n,x,a[1000009];
// unsigned long long t,a[10000009],max1i[10000009],maxin[10000009],maxx=-INF;
///--------------------------------
void solve();
signed main(){
long long t;
// cin >> t;
t=1;
while (t--){
solve();
}
}
///--------------------[PROBLEM SOLUTION]--------------------///
multiset<long long> ss;
long long sum=0,sum1=0,ans=INF,sum2=0;
void solve(){
long long k,n;
cin >> k >> n;
for (long long i=1; i<=n; i++){
char ch1,ch2;
long long x,y;
cin >> ch1 >> x >> ch2 >> y;
if (ch1!=ch2) {
ss.insert(x); ss.insert(y);
sum+=x+y;
}
else sum1+=2,sum2+=abs(y-x);
}
if (k==1){
auto it=ss.begin();
if (ss.size()==0) return cout << sum2, void();
advance(it, ss.size()/2);
long long tam=*it;
for (long long x: ss){
ans+=abs(tam-x);
}
ans+=(ss.size()/2);
ans+=sum2;
cout << ans;
}
else{
if (ss.size()==0) return cout << sum2, void();
auto it=ss.begin();
advance(it, ss.size()/2/2);
long long tam1=*it;
it=ss.begin();
advance(it, ss.size()/2/2+1);
long long tam2=*it;
it=ss.begin();
advance(it, ss.size()/2+(ss.size()/2/2));
long long tam3=*it;
it=ss.begin();
advance(it, ss.size()/2+(ss.size()/2/2)+1);
long long tam4=*it;
long long ans1=0, ans2=0;
long long d=0;
for (long long x: ss){
d++;
if (d<=ss.size()/2) ans1+=abs(tam1-x);
else ans2+=abs(tam3-x);
}
ans=min(ans,ans1+ans2);
d=0; ans1=0, ans2=0;
for (long long x: ss){
d++;
if (d<=ss.size()/2) ans1+=abs(tam1-x);
else ans2+=abs(tam4-x);
}
ans=min(ans,ans1+ans2);
d=0; ans1=0, ans2=0;
for (long long x: ss){
d++;
if (d<=ss.size()/2) ans1+=abs(tam2-x);
else ans2+=abs(tam3-x);
}
ans=min(ans,ans1+ans2);
d=0; ans1=0, ans2=0;
for (long long x: ss){
d++;
if (d<=ss.size()/2) ans1+=abs(tam2-x);
else ans2+=abs(tam4-x);
}
ans=min(ans,ans1+ans2);
ans+=(ss.size()/2);
ans+=sum2;
cout << ans;
}
}
Compilation message (stderr)
bridge.cpp: In function 'void solve()':
bridge.cpp:168:18: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::multiset<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
168 | if (d<=ss.size()/2) ans1+=abs(tam1-x);
| ~^~~~~~~~~~~~~
bridge.cpp:178:18: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::multiset<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
178 | if (d<=ss.size()/2) ans1+=abs(tam1-x);
| ~^~~~~~~~~~~~~
bridge.cpp:188:18: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::multiset<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
188 | if (d<=ss.size()/2) ans1+=abs(tam2-x);
| ~^~~~~~~~~~~~~
bridge.cpp:198:18: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::multiset<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
198 | if (d<=ss.size()/2) ans1+=abs(tam2-x);
| ~^~~~~~~~~~~~~
# | 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... |