This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma optimize("Bismillahirrahmanirrahim")
//█▀█─█──█──█▀█─█─█
//█▄█─█──█──█▄█─█■█
//█─█─█▄─█▄─█─█─█─█
//Allahuekber
//ahmet23 orz...
//Sani buyuk Osman Pasa Plevneden cikmam diyor.
//FatihSultanMehmedHan
//YavuzSultanSelimHan
//AbdulhamidHan
#define author tolbi
#include <bits/stdc++.h>
using namespace std;
template<typename X, typename Y> istream& operator>>(istream& in, pair<X,Y> &pr) {return in>>pr.first>>pr.second;}
template<typename X, typename Y> ostream& operator<<(ostream& os, pair<X,Y> pr) {return os<<pr.first<<" "<<pr.second;}
template<typename X> istream& operator>>(istream& in, vector<X> &arr) {for(auto &it : arr) in>>it; return in;}
template<typename X> ostream& operator<<(ostream& os, vector<X> arr) {for(auto &it : arr) os<<it<<" "; return os;}
template<typename X, size_t Y> istream& operator>>(istream& in, array<X,Y> &arr) {for(auto &it : arr) in>>it; return in;}
template<typename X, size_t Y> ostream& operator<<(ostream& os, array<X,Y> arr) {for(auto &it : arr) os<<it<<" "; return os;}
#define int long long
#define endl '\n'
#define vint(x) vector<int> x
#define deci(x) int x;cin>>x;
#define decstr(x) string x;cin>>x;
#define cinarr(x) for (auto &it : x) cin>>it;
#define coutarr(x) for (auto &it : x) cout<<it<<" ";cout<<endl;
#define sortarr(x) sort(x.begin(),x.end())
#define sortrarr(x) sort(x.rbegin(),x.rend())
#define det(x) cout<<"NO\0YES"+x*3<<endl;
#define INF LONG_LONG_MAX
#define rev(x) reverse(x.begin(),x.end());
#define ios ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define tol(bi) (1LL<<((int)(bi)))
const int MOD = 1e9+7;
mt19937 ayahya(chrono::high_resolution_clock().now().time_since_epoch().count());
int solve(vector<pair<int,int>> arr){
int n = arr.size();
vector<int> ller;
vector<int> rler;
for (int i = 0; i < arr.size(); ++i)
{
ller.push_back(arr[i].first);
rler.push_back(arr[i].second);
}
sortarr(ller);
sortarr(rler);
vector<int> lsuff = ller;
vector<int> rpref = rler;
for (int i = 1; i < ller.size(); i++){
rpref[i]+=rpref[i-1];
lsuff[ller.size()-i-1]+=lsuff[ller.size()-i];
}
int ans = INF;
function<int(int)> process = [&](int x)->int{
auto lb = lower_bound(ller.begin(), ller.end(), x);
auto rb = lower_bound(rler.begin(), rler.end(), x);
int crr = 0;
if (rb!=rler.begin()){
rb--;
crr+=x*(rb-rler.begin()+1)-rpref[rb-rler.begin()];
}
if (lb!=ller.end()){
crr+=lsuff[lb-ller.begin()]-x*(ller.end()-lb);
}
ans=min(ans,crr);
return crr;
};
int l = 0, r = 1000000000;
while (l<r){
int mid = l+(r-l)/2;
int res = process(mid);
int res2 = process(mid+1);
if (res>res2){
l=mid+1;
}
else {
r=mid;
}
}
process(l);
int hh = 0;
for (int i = 0; i < arr.size(); ++i)
{
hh+=abs(arr[i].first-arr[i].second)+1;
}
return ans*2+hh;
}
int32_t main(){
ios;
int t=1;
int tno = 0;
if (!t) cin>>t;
while (t-(tno++)){
deci(k);deci(n);
vector<pair<int,int>> arr;
int ans = 0;
for (int i = 0; i < n; ++i)
{
char a,c;
int b,d;
cin>>a>>b>>c>>d;
if (a==c) ans+=abs(b-d);
else{
arr.push_back({min(b,d),max(b,d)});
}
}
if (k==1){
ans+=solve(arr);
}
else {
if (arr.size()==0){
cout<<ans<<endl;
return 0;
}
sort(arr.begin(), arr.end(), [&](pair<int,int> a, pair<int,int> b){
return (a.first+a.second)<(b.first+b.second);
});
vector<pair<int,int>> hhehehe;
vector<int> a;
for (int i = 0; i < arr.size(); ++i)
{
hhehehe.push_back(arr[i]);
a.push_back(solve(hhehehe));
}
vector<int> b(arr.size());
hhehehe.clear();
for (int i = arr.size()-1; i >= 0; --i){
hhehehe.push_back(arr[i]);
b[i]=solve(hhehehe);
}
int crr = INF;
for (int i = 0; i < arr.size(); ++i)
{
int l = a[i];
int r = 0;
if (i+1<n) r = b[i+1];
crr=min(crr,l+r);
}
ans+=crr;
}
cout<<ans<<endl;
}
}
Compilation message (stderr)
bridge.cpp:1: warning: ignoring '#pragma optimize ' [-Wunknown-pragmas]
1 | #pragma optimize("Bismillahirrahmanirrahim")
|
bridge.cpp: In function 'long long int solve(std::vector<std::pair<long long int, long long int> >)':
bridge.cpp:40:20: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
40 | for (int i = 0; i < arr.size(); ++i)
| ~~^~~~~~~~~~~~
bridge.cpp:49:20: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
49 | for (int i = 1; i < ller.size(); i++){
| ~~^~~~~~~~~~~~~
bridge.cpp:82:20: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
82 | for (int i = 0; i < arr.size(); ++i)
| ~~^~~~~~~~~~~~
bridge.cpp:37:6: warning: unused variable 'n' [-Wunused-variable]
37 | int n = arr.size();
| ^
bridge.cpp: In function 'int32_t main()':
bridge.cpp:120:22: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
120 | for (int i = 0; i < arr.size(); ++i)
| ~~^~~~~~~~~~~~
bridge.cpp:132:22: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
132 | for (int i = 0; i < arr.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... |