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>
// #include <ext/pb_ds/assoc_container.hpp>
// using namespace __gnu_pbds;
// #pragma GCC target ("avx,avx2,fma")
// #pragma GCC optimize("Ofast")
// #pragma GCC optimize("unroll-loops")
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
#define SPEED ios_base::sync_with_stdio(false); cin.tie(0), cout.tie(0)
#define rall(v) (v).rbegin(),(v).rend()
#define all(v) (v).begin(),(v).end()
#define setp fixed<<setprecision
#define OK cerr<<"OK"<<endl<<flush
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pb push_back
#define F first
#define S second
#define y0 jahdakdh
#define y1 jahsadakdakdh
#define endl '\n'
const ll MOD=1e9+7;
const ll mod=(1ll<<31)-1;
const ld eps=1e-8;
using namespace std;
mt19937 rng(std::chrono::system_clock::now().time_since_epoch().count());
int n, k;
ll ans=MOD*MOD, fx;
vector<pll> v;
vector<int> pos;
ll ternary(int br)
{
ll cnt=0;
for(pll u : v)
cnt+=abs(u.F-br)+abs(u.S-br)+1;
return cnt+fx;
}
int main()
{
SPEED;
cin>>k>>n;
for(int i=0; i<n; i++)
{
char a, b;
ll x, y;
cin>>a>>x>>b>>y;
pos.pb(x);
pos.pb(y);
if(a==b) fx+=abs(y-x);
else v.pb({x, y});
}
sort(all(pos));
int l=0, r=pos.size()-1;
while(l<=r)
{
int mid1=l+(r-l)/3;
int mid2=r-(r-l)/3;
ll ans1=ternary(pos[mid1]);
ll ans2=ternary(pos[mid2]);
ans=min({ans, ans1, ans2});
if(ans1==ans2)
{
l=mid1+1;
r=mid2-1;
}
else if(ans1<ans2) r=mid2-1;
else l=mid1+1;
}
cout<<ans;
return 0;
}
/*
1 5
B 0 A 4
B 1 B 3
A 5 B 7
B 2 A 6
B 1 A 7
*/
# | 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... |