#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pi;
ll maks = 0,mins=1000000010;
#define fi first
#define sc second
#define mp make_pair
#define pb push_back
vector<pi> pasangan;
vector<ll> dict;
ll n;
ll independent = 0;
string a,b;
ll k;
ll x,y;
ll si;
ll ternary3(ll x){
ll ret = 0;
for(ll i=0;i<si;i++) ret+=labs(pasangan[i].fi-x)+labs(pasangan[i].sc-x);
return ret;
}
ll ternary2(ll x, ll y){
ll ret =0;
for(ll i=0;i<si;i++){
ret+=min(labs(pasangan[i].fi-x)+labs(pasangan[i].sc-x),labs(pasangan[i].fi-y)+labs(pasangan[i].sc-y));
}
return ret;
}
ll ternary1(ll x){
ll l = 0,r=dict.size()-1;
while(l<r){
if (l+10>=r){
ll i = l, j = r;
ll now =LONG_LONG_MAX;
ll ret;
for(ll k=i;k<=j;k++){
ll sek = ternary2(x,dict[k]);
if (sek<now){
now=sek;
ret=k;
}
}
l=r=ret;
}
ll lt = l+(r-l)/3;
ll rt = l+(r-l)*2/3;
if (ternary2(x,dict[lt])>ternary2(x,dict[rt])) l=lt;
else r = rt;
}
ll kel = ternary2(x,dict[l]), ker = ternary2(x,dict[r]);
return min(kel,ker);
}
int main(){
ios_base::sync_with_stdio(false);
cin>>k>>n;
while(n--){
cin >> a >> x >> b >> y;
dict.pb(x); dict.pb(y);
maks=max(maks,max(x,y));
mins=min(mins,min(x,y));
if (a==b) independent += labs(x-y);
else{
pasangan.pb(mp(x,y));
}
}
sort(dict.begin(),dict.end());
si = pasangan.size();
ll l = 0, r = dict.size()-1;
if (k==1){
while(l<r){
if (l+10>=r){
ll i = l, j=r;
ll now = LONG_LONG_MAX;
ll ap;
for (ll k=i;k<=j;k++){
ll sek = ternary3(dict[k]);
if (sek<now){
now=sek;
ap=k;
}
}
l=r=ap;
}
ll lt = l+(r-l)/3;
ll rt = l+ (r-l)*2/3;
if (ternary3(dict[lt])>ternary3(dict[rt])) l=lt;
else r=rt;
}
independent+=si;
cout << independent+min(ternary3(dict[l]),ternary3(dict[r])) << "\n";
return 0;
}
while(l<r){
if (l+10>=r){
ll i = l, j=r;
ll now = LONG_LONG_MAX;
ll ap;
for (ll k=i;k<=j;k++){
ll sek = ternary1(dict[k]);
if (sek<now){
now=sek;
ap=k;
}
}
l=r=ap;
}
ll lt = l+(r-l)/3;
ll rt = l+ (r-l)*2/3;
if (ternary1(dict[lt])>ternary1(dict[rt])) l=lt;
else r=rt;
}
independent+=si;
cout << independent+min(ternary1(dict[l]),ternary1(dict[r])) << "\n";
}
Compilation message
bridge.cpp: In function 'll ternary1(ll)':
bridge.cpp:54:21: warning: 'ret' may be used uninitialized in this function [-Wmaybe-uninitialized]
ll rt = l+(r-l)*2/3;
^
bridge.cpp: In function 'int main()':
bridge.cpp:123:22: warning: 'ap' may be used uninitialized in this function [-Wmaybe-uninitialized]
ll rt = l+ (r-l)*2/3;
^
bridge.cpp:97:23: warning: 'ap' may be used uninitialized in this function [-Wmaybe-uninitialized]
ll rt = l+ (r-l)*2/3;
^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
248 KB |
Output is correct |
2 |
Correct |
1 ms |
480 KB |
Output is correct |
3 |
Correct |
2 ms |
480 KB |
Output is correct |
4 |
Correct |
2 ms |
492 KB |
Output is correct |
5 |
Correct |
2 ms |
492 KB |
Output is correct |
6 |
Correct |
2 ms |
492 KB |
Output is correct |
7 |
Correct |
2 ms |
492 KB |
Output is correct |
8 |
Correct |
2 ms |
528 KB |
Output is correct |
9 |
Correct |
2 ms |
528 KB |
Output is correct |
10 |
Correct |
2 ms |
532 KB |
Output is correct |
11 |
Correct |
2 ms |
576 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
584 KB |
Output is correct |
2 |
Correct |
1 ms |
712 KB |
Output is correct |
3 |
Correct |
2 ms |
712 KB |
Output is correct |
4 |
Correct |
2 ms |
712 KB |
Output is correct |
5 |
Correct |
2 ms |
712 KB |
Output is correct |
6 |
Correct |
2 ms |
712 KB |
Output is correct |
7 |
Correct |
2 ms |
712 KB |
Output is correct |
8 |
Correct |
2 ms |
712 KB |
Output is correct |
9 |
Correct |
3 ms |
712 KB |
Output is correct |
10 |
Correct |
2 ms |
712 KB |
Output is correct |
11 |
Correct |
2 ms |
712 KB |
Output is correct |
12 |
Correct |
50 ms |
3928 KB |
Output is correct |
13 |
Correct |
80 ms |
3928 KB |
Output is correct |
14 |
Correct |
58 ms |
3940 KB |
Output is correct |
15 |
Correct |
51 ms |
3940 KB |
Output is correct |
16 |
Correct |
56 ms |
3940 KB |
Output is correct |
17 |
Correct |
59 ms |
3956 KB |
Output is correct |
18 |
Correct |
73 ms |
4072 KB |
Output is correct |
19 |
Correct |
72 ms |
4072 KB |
Output is correct |
20 |
Correct |
60 ms |
4072 KB |
Output is correct |
21 |
Correct |
67 ms |
4072 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4072 KB |
Output is correct |
2 |
Correct |
1 ms |
4072 KB |
Output is correct |
3 |
Correct |
2 ms |
4072 KB |
Output is correct |
4 |
Correct |
2 ms |
4072 KB |
Output is correct |
5 |
Correct |
2 ms |
4072 KB |
Output is correct |
6 |
Correct |
1 ms |
4072 KB |
Output is correct |
7 |
Correct |
2 ms |
4072 KB |
Output is correct |
8 |
Correct |
2 ms |
4072 KB |
Output is correct |
9 |
Correct |
2 ms |
4072 KB |
Output is correct |
10 |
Correct |
2 ms |
4072 KB |
Output is correct |
11 |
Correct |
2 ms |
4072 KB |
Output is correct |
12 |
Correct |
2 ms |
4072 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4072 KB |
Output is correct |
2 |
Correct |
1 ms |
4072 KB |
Output is correct |
3 |
Correct |
2 ms |
4072 KB |
Output is correct |
4 |
Correct |
2 ms |
4072 KB |
Output is correct |
5 |
Correct |
1 ms |
4072 KB |
Output is correct |
6 |
Correct |
1 ms |
4072 KB |
Output is correct |
7 |
Correct |
2 ms |
4072 KB |
Output is correct |
8 |
Correct |
2 ms |
4072 KB |
Output is correct |
9 |
Correct |
2 ms |
4072 KB |
Output is correct |
10 |
Correct |
2 ms |
4072 KB |
Output is correct |
11 |
Correct |
2 ms |
4072 KB |
Output is correct |
12 |
Correct |
2 ms |
4072 KB |
Output is correct |
13 |
Correct |
8 ms |
4072 KB |
Output is correct |
14 |
Correct |
8 ms |
4072 KB |
Output is correct |
15 |
Correct |
8 ms |
4072 KB |
Output is correct |
16 |
Correct |
3 ms |
4072 KB |
Output is correct |
17 |
Correct |
6 ms |
4072 KB |
Output is correct |
18 |
Correct |
3 ms |
4072 KB |
Output is correct |
19 |
Correct |
8 ms |
4072 KB |
Output is correct |
20 |
Correct |
8 ms |
4072 KB |
Output is correct |
21 |
Correct |
8 ms |
4072 KB |
Output is correct |
22 |
Correct |
8 ms |
4072 KB |
Output is correct |
23 |
Correct |
11 ms |
4072 KB |
Output is correct |
24 |
Correct |
9 ms |
4072 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4072 KB |
Output is correct |
2 |
Correct |
2 ms |
4072 KB |
Output is correct |
3 |
Correct |
2 ms |
4072 KB |
Output is correct |
4 |
Correct |
2 ms |
4072 KB |
Output is correct |
5 |
Correct |
2 ms |
4072 KB |
Output is correct |
6 |
Correct |
1 ms |
4072 KB |
Output is correct |
7 |
Correct |
2 ms |
4072 KB |
Output is correct |
8 |
Correct |
2 ms |
4072 KB |
Output is correct |
9 |
Correct |
2 ms |
4072 KB |
Output is correct |
10 |
Correct |
2 ms |
4072 KB |
Output is correct |
11 |
Correct |
2 ms |
4072 KB |
Output is correct |
12 |
Correct |
2 ms |
4072 KB |
Output is correct |
13 |
Correct |
8 ms |
4072 KB |
Output is correct |
14 |
Correct |
8 ms |
4072 KB |
Output is correct |
15 |
Correct |
8 ms |
4072 KB |
Output is correct |
16 |
Correct |
3 ms |
4072 KB |
Output is correct |
17 |
Correct |
4 ms |
4072 KB |
Output is correct |
18 |
Correct |
3 ms |
4072 KB |
Output is correct |
19 |
Correct |
9 ms |
4072 KB |
Output is correct |
20 |
Correct |
9 ms |
4072 KB |
Output is correct |
21 |
Correct |
8 ms |
4072 KB |
Output is correct |
22 |
Correct |
8 ms |
4072 KB |
Output is correct |
23 |
Correct |
8 ms |
4072 KB |
Output is correct |
24 |
Correct |
9 ms |
4072 KB |
Output is correct |
25 |
Correct |
1519 ms |
4072 KB |
Output is correct |
26 |
Correct |
1501 ms |
4072 KB |
Output is correct |
27 |
Correct |
1543 ms |
4072 KB |
Output is correct |
28 |
Correct |
1627 ms |
6340 KB |
Output is correct |
29 |
Correct |
1558 ms |
8636 KB |
Output is correct |
30 |
Correct |
765 ms |
8636 KB |
Output is correct |
31 |
Correct |
1493 ms |
11540 KB |
Output is correct |
32 |
Correct |
1546 ms |
13876 KB |
Output is correct |
33 |
Correct |
1501 ms |
15832 KB |
Output is correct |
34 |
Correct |
1552 ms |
18156 KB |
Output is correct |
35 |
Correct |
1531 ms |
20100 KB |
Output is correct |
36 |
Correct |
1541 ms |
22108 KB |
Output is correct |
37 |
Incorrect |
1507 ms |
22916 KB |
Output isn't correct |