#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<ll> pos;
ll ternary(ll br1, ll br2)
{
ll cnt=0;
for(pll u : v)
cnt+=min(abs(u.F-br1)+abs(u.S-br1)+1, abs(u.F-br2)+abs(u.S-br2)+1);
return cnt+fx;
}
void solve1()
{
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], pos[mid1]);
ll ans2=ternary(pos[mid2], 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;
}
ll ter(ll br1)
{
int l=br1+1, r=pos.size()-1;
ll cnt=MOD*MOD;
while(l<=r)
{
int mid1=l+(r-l)/3;
int mid2=r-(r-l)/3;
ll cnt1=ternary(pos[br1], pos[mid1]);
ll cnt2=ternary(pos[br1], pos[mid2]);
cnt=min({cnt, cnt1, cnt2});
if(cnt1==cnt2)
{
l=mid1+1;
r=mid2-1;
}
else if(cnt1<cnt2) r=mid2-1;
else l=mid1+1;
}
return cnt;
}
void solve2()
{
int l=0, r=pos.size()-2;
while(l<=r)
{
int mid1=l+(r-l)/3;
int mid2=r-(r-l)/3;
ll ans1=ter(mid1);
ll ans2=ter(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;
}
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));
if(k==1) solve1();
else solve2();
return 0;
}
/*
2 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 |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
328 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
372 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
388 KB |
Output is correct |
10 |
Correct |
1 ms |
368 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
27 ms |
4016 KB |
Output is correct |
13 |
Correct |
57 ms |
4076 KB |
Output is correct |
14 |
Correct |
42 ms |
3908 KB |
Output is correct |
15 |
Correct |
33 ms |
2724 KB |
Output is correct |
16 |
Correct |
35 ms |
3988 KB |
Output is correct |
17 |
Correct |
45 ms |
4048 KB |
Output is correct |
18 |
Correct |
40 ms |
3972 KB |
Output is correct |
19 |
Correct |
56 ms |
4032 KB |
Output is correct |
20 |
Correct |
36 ms |
3976 KB |
Output is correct |
21 |
Correct |
51 ms |
4036 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
328 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
320 KB |
Output is correct |
4 |
Correct |
1 ms |
324 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
320 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
324 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
328 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
324 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
324 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
2 ms |
340 KB |
Output is correct |
15 |
Correct |
4 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
2 ms |
340 KB |
Output is correct |
18 |
Correct |
2 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
464 KB |
Output is correct |
20 |
Correct |
4 ms |
340 KB |
Output is correct |
21 |
Correct |
2 ms |
340 KB |
Output is correct |
22 |
Correct |
4 ms |
404 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
4 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
324 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
2 ms |
340 KB |
Output is correct |
15 |
Correct |
4 ms |
336 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
2 ms |
352 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
4 ms |
340 KB |
Output is correct |
21 |
Correct |
3 ms |
340 KB |
Output is correct |
22 |
Correct |
5 ms |
340 KB |
Output is correct |
23 |
Correct |
2 ms |
340 KB |
Output is correct |
24 |
Correct |
3 ms |
340 KB |
Output is correct |
25 |
Correct |
172 ms |
4364 KB |
Output is correct |
26 |
Correct |
389 ms |
4312 KB |
Output is correct |
27 |
Correct |
870 ms |
4304 KB |
Output is correct |
28 |
Correct |
900 ms |
4308 KB |
Output is correct |
29 |
Correct |
926 ms |
4384 KB |
Output is correct |
30 |
Correct |
441 ms |
2808 KB |
Output is correct |
31 |
Correct |
179 ms |
4304 KB |
Output is correct |
32 |
Correct |
936 ms |
4308 KB |
Output is correct |
33 |
Correct |
400 ms |
4336 KB |
Output is correct |
34 |
Correct |
933 ms |
4316 KB |
Output is correct |
35 |
Correct |
188 ms |
4312 KB |
Output is correct |
36 |
Correct |
934 ms |
4304 KB |
Output is correct |
37 |
Incorrect |
173 ms |
4284 KB |
Output isn't correct |
38 |
Halted |
0 ms |
0 KB |
- |