#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 {
sort(arr.begin(), arr.end(), [&](pair<int,int> a, pair<int,int> b){
return (a.first<b.first);
});
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);
}
sort(arr.begin(), arr.end(), [&](pair<int,int> a, pair<int,int> b){
return (a.second<b.second);
});
hhehehe.clear();
a.clear();
for (int i = 0; i < arr.size(); ++i)
{
hhehehe.push_back(arr[i]);
a.push_back(solve(hhehehe));
}
b.clear();
b.resize(arr.size());
hhehehe.clear();
for (int i = arr.size()-1; i >= 0; --i){
hhehehe.push_back(arr[i]);
b[i]=solve(hhehehe);
}
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
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:116: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]
116 | for (int i = 0; i < arr.size(); ++i)
| ~~^~~~~~~~~~~~
bridge.cpp:128: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]
128 | for (int i = 0; i < arr.size(); ++i)
| ~~^~~~~~~~~~~~
bridge.cpp:140: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]
140 | for (int i = 0; i < arr.size(); ++i)
| ~~^~~~~~~~~~~~
bridge.cpp:152: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]
152 | for (int i = 0; i < arr.size(); ++i)
| ~~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
480 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
600 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
19 ms |
7144 KB |
Output is correct |
13 |
Correct |
32 ms |
7060 KB |
Output is correct |
14 |
Correct |
24 ms |
6600 KB |
Output is correct |
15 |
Correct |
18 ms |
4464 KB |
Output is correct |
16 |
Correct |
19 ms |
7292 KB |
Output is correct |
17 |
Correct |
23 ms |
7256 KB |
Output is correct |
18 |
Correct |
21 ms |
7288 KB |
Output is correct |
19 |
Correct |
32 ms |
7056 KB |
Output is correct |
20 |
Correct |
22 ms |
7112 KB |
Output is correct |
21 |
Correct |
32 ms |
7120 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |