#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 br1, int 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;
}
void solve2()
{
for(int i=0; i<(int)pos.size(); i++)
{
int l=i+1, r=pos.size()-1;
while(l<=r)
{
int mid1=l+(r-l)/3;
int mid2=r-(r-l)/3;
ll ans1=ternary(pos[i], pos[mid1]);
ll ans2=ternary(pos[i], 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<<endl;
}
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
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 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 |
340 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 |
340 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 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 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 |
340 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 |
340 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 |
352 KB |
Output is correct |
12 |
Correct |
28 ms |
3128 KB |
Output is correct |
13 |
Correct |
55 ms |
3092 KB |
Output is correct |
14 |
Correct |
47 ms |
3024 KB |
Output is correct |
15 |
Correct |
36 ms |
1880 KB |
Output is correct |
16 |
Correct |
41 ms |
3096 KB |
Output is correct |
17 |
Correct |
55 ms |
3068 KB |
Output is correct |
18 |
Correct |
41 ms |
3048 KB |
Output is correct |
19 |
Correct |
55 ms |
3076 KB |
Output is correct |
20 |
Correct |
35 ms |
3032 KB |
Output is correct |
21 |
Correct |
53 ms |
3008 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 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 |
2 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
2 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
2 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 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 |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
2 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
2 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 |
78 ms |
340 KB |
Output is correct |
14 |
Correct |
125 ms |
352 KB |
Output is correct |
15 |
Correct |
164 ms |
348 KB |
Output is correct |
16 |
Correct |
7 ms |
340 KB |
Output is correct |
17 |
Correct |
44 ms |
344 KB |
Output is correct |
18 |
Correct |
26 ms |
340 KB |
Output is correct |
19 |
Correct |
79 ms |
340 KB |
Output is correct |
20 |
Correct |
169 ms |
352 KB |
Output is correct |
21 |
Correct |
109 ms |
348 KB |
Output is correct |
22 |
Correct |
180 ms |
340 KB |
Output is correct |
23 |
Correct |
89 ms |
344 KB |
Output is correct |
24 |
Correct |
173 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 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 |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
2 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
2 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 |
73 ms |
340 KB |
Output is correct |
14 |
Correct |
135 ms |
348 KB |
Output is correct |
15 |
Correct |
186 ms |
340 KB |
Output is correct |
16 |
Correct |
6 ms |
340 KB |
Output is correct |
17 |
Correct |
35 ms |
340 KB |
Output is correct |
18 |
Correct |
20 ms |
340 KB |
Output is correct |
19 |
Correct |
73 ms |
356 KB |
Output is correct |
20 |
Correct |
174 ms |
344 KB |
Output is correct |
21 |
Correct |
135 ms |
340 KB |
Output is correct |
22 |
Correct |
178 ms |
344 KB |
Output is correct |
23 |
Correct |
86 ms |
348 KB |
Output is correct |
24 |
Correct |
163 ms |
360 KB |
Output is correct |
25 |
Execution timed out |
2065 ms |
3008 KB |
Time limit exceeded |
26 |
Halted |
0 ms |
0 KB |
- |