/*
بسم الله الرحمن الرحيم
Author:
(:Muhammad Aneeq:)
*/
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
#define int long long
bool comp(pair<int,int>a,pair<int,int>b)
{
return (b.second>a.second);
}
void solve()
{
int k,n;
cin>>k>>n;
long long ans=0;
int z=0;
vector<int>f;
vector<pair<int,int>>d;
for (int i=0;i<n;i++)
{
char a,b;
int x,y;
cin>>a>>x>>b>>y;
if (a==b)
ans+=abs(x-y);
else
{
z+=abs(x-y);
d.push_back({x,y});
f.push_back(x);
f.push_back(y);
}
}
sort(begin(f),end(f));
int pre[f.size()+1]={};
for (int i=0;i<f.size();i++)
pre[i+1]=pre[i]+f[i];
if (f.size())
{
if (k==1)
{
long long r=1e15+10;
for (auto i:f)
{
long long t=0;
int g=lower_bound(begin(f),end(f),i)-begin(f);
t+=pre[f.size()]-pre[g]-i*(f.size()-g);
t+=i*(g+1)-pre[g+1];
r=min(r,t);
}
ans+=r;
}
else
{
long long r=1e15+10;
for (int i=0;i<f.size();i++)
{
for (int j=i+1;j<f.size();j++)
{
long long t=0;
for (auto k:d)
t+=min(abs(k.first-f[i])+abs(k.second-f[i]),abs(k.first-f[j])+abs(k.second-f[j]));
r=min(r,t);
}
}
ans+=r;
}
}
cout<<ans+f.size()/2<<endl;
}
signed main()
{
ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
solve();
}
Compilation message
bridge.cpp: In function 'void solve()':
bridge.cpp:41:16: 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]
41 | for (int i=0;i<f.size();i++)
| ~^~~~~~~~~
bridge.cpp:61:18: 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]
61 | for (int i=0;i<f.size();i++)
| ~^~~~~~~~~
bridge.cpp:63:21: 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]
63 | for (int j=i+1;j<f.size();j++)
| ~^~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
600 KB |
Output is correct |
2 |
Correct |
1 ms |
600 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
344 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 |
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 |
1 ms |
384 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 |
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 |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
600 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 |
12 |
Correct |
21 ms |
5360 KB |
Output is correct |
13 |
Correct |
44 ms |
5312 KB |
Output is correct |
14 |
Correct |
30 ms |
4796 KB |
Output is correct |
15 |
Correct |
25 ms |
3272 KB |
Output is correct |
16 |
Correct |
25 ms |
5312 KB |
Output is correct |
17 |
Correct |
34 ms |
5360 KB |
Output is correct |
18 |
Correct |
40 ms |
5464 KB |
Output is correct |
19 |
Correct |
44 ms |
5300 KB |
Output is correct |
20 |
Correct |
26 ms |
5308 KB |
Output is correct |
21 |
Correct |
39 ms |
5300 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
5 ms |
348 KB |
Output is correct |
4 |
Correct |
4 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 |
5 ms |
348 KB |
Output is correct |
8 |
Correct |
6 ms |
348 KB |
Output is correct |
9 |
Correct |
5 ms |
344 KB |
Output is correct |
10 |
Correct |
6 ms |
344 KB |
Output is correct |
11 |
Correct |
5 ms |
344 KB |
Output is correct |
12 |
Correct |
5 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
5 ms |
348 KB |
Output is correct |
4 |
Correct |
5 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 |
5 ms |
348 KB |
Output is correct |
8 |
Correct |
5 ms |
460 KB |
Output is correct |
9 |
Correct |
5 ms |
360 KB |
Output is correct |
10 |
Correct |
6 ms |
344 KB |
Output is correct |
11 |
Correct |
6 ms |
348 KB |
Output is correct |
12 |
Correct |
5 ms |
464 KB |
Output is correct |
13 |
Execution timed out |
2029 ms |
348 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
500 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
4 ms |
344 KB |
Output is correct |
4 |
Correct |
5 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 |
5 ms |
348 KB |
Output is correct |
8 |
Correct |
7 ms |
344 KB |
Output is correct |
9 |
Correct |
5 ms |
344 KB |
Output is correct |
10 |
Correct |
6 ms |
600 KB |
Output is correct |
11 |
Correct |
5 ms |
344 KB |
Output is correct |
12 |
Correct |
5 ms |
348 KB |
Output is correct |
13 |
Execution timed out |
2086 ms |
348 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |