#include <bits/stdc++.h>
using namespace std;
#define int long long
const int M = 1e5 + 1;
int pre[M]={},pre1[M]={},n;
int get(int a,vector<int> &v)
{
int x=upper_bound(v.begin(),v.end(),a)-v.begin();
return abs(a*x-pre[x]+pre[n]-pre[x]-(n-x)*a);
}
int get1(int a,vector<int> &v)
{
int x=upper_bound(v.begin(),v.end(),a)-v.begin();
return abs(a*x-pre1[x]+pre1[n]-pre1[x]-(n-x)*a);
}
signed main()
{
int k,su=0;
cin>>k>>n;
vector<int> v,v1;
for (int i=0;i<n;i++)
{
char c,c1;
int x,y;
cin>>c>>x>>c1>>y;
if (c==c1)
su+=abs(y-x);
else
{
if (c=='B')
swap(x,y);
v.push_back(x);
v1.push_back(y);
}
}
n=v.size();
set<int> se;
for (int i=0;i<n;i++)
se.insert(v[i]),se.insert(v1[i]);
vector<int> pos;
int ans=n*1e9;
for (int i:se)
pos.push_back(i);
if (k==1)
{
sort(v.begin(),v.end());
sort(v1.begin(),v1.end());
for (int i=0;i<n;i++)
pre[i+1]=pre[i]+v[i],pre1[i+1]=pre1[i]+v1[i];
for (int i=0;i<pos.size();i++)
ans=min(ans,get(pos[i],v)+get1(pos[i],v1));
}
else
{
for (int i=0;i<pos.size();i++)
{
for (int j=i+1;j<pos.size();j++)
{
int val=0;
for (int a=0;a<n;a++)
val+=min(abs(v[a]-pos[i])+abs(v1[a]-pos[i]),abs(v[a]-pos[j])+abs(v1[a]-pos[j]));
ans=min(ans,val);
}
}
}
cout<<ans+su+n<<endl;
return 0;
}
Compilation message
bridge.cpp: In function 'int main()':
bridge.cpp:57:17: 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]
57 | for (int i=0;i<pos.size();i++)
| ~^~~~~~~~~~~
bridge.cpp:62:17: 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]
62 | for (int i=0;i<pos.size();i++)
| ~^~~~~~~~~~~
bridge.cpp:64: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]
64 | for (int j=i+1;j<pos.size();j++)
| ~^~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 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 |
460 KB |
Output is correct |
5 |
Correct |
2 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
2 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
2 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
428 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
452 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
484 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
456 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
32 ms |
4552 KB |
Output is correct |
13 |
Correct |
162 ms |
16700 KB |
Output is correct |
14 |
Correct |
55 ms |
5048 KB |
Output is correct |
15 |
Correct |
79 ms |
10048 KB |
Output is correct |
16 |
Correct |
48 ms |
5300 KB |
Output is correct |
17 |
Correct |
115 ms |
16832 KB |
Output is correct |
18 |
Correct |
104 ms |
16448 KB |
Output is correct |
19 |
Correct |
122 ms |
16436 KB |
Output is correct |
20 |
Correct |
53 ms |
5560 KB |
Output is correct |
21 |
Correct |
117 ms |
16440 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 |
4 ms |
448 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
5 ms |
348 KB |
Output is correct |
9 |
Correct |
5 ms |
460 KB |
Output is correct |
10 |
Correct |
4 ms |
444 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
5 ms |
420 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 |
4 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
452 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
5 ms |
348 KB |
Output is correct |
9 |
Correct |
5 ms |
348 KB |
Output is correct |
10 |
Correct |
5 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
5 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
12 ms |
344 KB |
Output is correct |
15 |
Execution timed out |
2098 ms |
348 KB |
Time limit exceeded |
16 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
4 ms |
348 KB |
Output is correct |
5 |
Correct |
2 ms |
604 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
5 ms |
348 KB |
Output is correct |
9 |
Correct |
5 ms |
348 KB |
Output is correct |
10 |
Correct |
5 ms |
460 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
5 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
12 ms |
348 KB |
Output is correct |
15 |
Execution timed out |
2095 ms |
604 KB |
Time limit exceeded |
16 |
Halted |
0 ms |
0 KB |
- |