///~~~LOTA~~~///
#include <bits/stdc++.h>
using namespace std;
#define nl '\n'
#define ff first
#define ss second
#define ll long long
#define append push_back
#define pii pair<int,int>
#define all(x) (x).begin(),(x).end()
#define N 100001
ll o;
int n,m;
vector<ll> x,y,z;
ll dist(ll p,ll q){
ll r=0;
for(int i=0;i<m;i++){
if((x[i]>=p && p>=y[i]) ||
(x[i]>=q && q>=y[i])) continue;
r+=min(min(abs(p-x[i]),abs(p-y[i]))
,min(abs(q-x[i]),abs(q-y[i])))*2;
}
return r;
}
void solve1(){
ll r=0;
for(int j=536870912;j>0;j/=2)
if(dist(r+j,r+j)<=dist(r+j-1,r+j-1))
r+=j;
cout<<o+dist(r,r);
}
void solve2(){
ll r=dist(0,0);
for(auto& i:z)
for(auto& j:z)
r=min(r,dist(i,j));
cout<<o+r;
}
int main(){
ios_base::sync_with_stdio(false);cin.tie(NULL);
char c,d;
int t,p,q;
cin>>t;
cin>>n;
for(int i=o=0;i<n;i++){
cin>>c>>p>>d>>q;
o+=abs(p-q);
if(c!=d){
o++;
m++;
z.append(p);
z.append(q);
x.append(max(p,q));
y.append(min(p,q));
}
}
if(t==1) solve1();
else solve2();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 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 |
508 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 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
596 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 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 |
2 ms |
348 KB |
Output is correct |
12 |
Correct |
34 ms |
4812 KB |
Output is correct |
13 |
Correct |
56 ms |
4960 KB |
Output is correct |
14 |
Correct |
40 ms |
4556 KB |
Output is correct |
15 |
Correct |
38 ms |
2512 KB |
Output is correct |
16 |
Correct |
30 ms |
4808 KB |
Output is correct |
17 |
Correct |
37 ms |
4984 KB |
Output is correct |
18 |
Correct |
29 ms |
4812 KB |
Output is correct |
19 |
Correct |
70 ms |
5060 KB |
Output is correct |
20 |
Correct |
55 ms |
4808 KB |
Output is correct |
21 |
Correct |
60 ms |
4812 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
5 ms |
348 KB |
Output is correct |
4 |
Correct |
12 ms |
344 KB |
Output is correct |
5 |
Correct |
3 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
6 ms |
344 KB |
Output is correct |
8 |
Correct |
14 ms |
348 KB |
Output is correct |
9 |
Correct |
8 ms |
348 KB |
Output is correct |
10 |
Correct |
18 ms |
348 KB |
Output is correct |
11 |
Correct |
10 ms |
348 KB |
Output is correct |
12 |
Correct |
17 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
8 ms |
344 KB |
Output is correct |
4 |
Correct |
12 ms |
348 KB |
Output is correct |
5 |
Correct |
3 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
5 ms |
344 KB |
Output is correct |
8 |
Correct |
14 ms |
344 KB |
Output is correct |
9 |
Correct |
10 ms |
348 KB |
Output is correct |
10 |
Correct |
18 ms |
348 KB |
Output is correct |
11 |
Correct |
11 ms |
344 KB |
Output is correct |
12 |
Correct |
16 ms |
348 KB |
Output is correct |
13 |
Execution timed out |
2074 ms |
348 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
6 ms |
600 KB |
Output is correct |
4 |
Correct |
12 ms |
348 KB |
Output is correct |
5 |
Correct |
3 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
5 ms |
348 KB |
Output is correct |
8 |
Correct |
14 ms |
452 KB |
Output is correct |
9 |
Correct |
10 ms |
348 KB |
Output is correct |
10 |
Correct |
20 ms |
460 KB |
Output is correct |
11 |
Correct |
10 ms |
348 KB |
Output is correct |
12 |
Correct |
16 ms |
348 KB |
Output is correct |
13 |
Execution timed out |
2017 ms |
460 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |