#include <iostream>
#include <set>
#include <algorithm>
#include <vector>
using namespace std;
long long BFG(vector<int>& neq)
{
int s=0;
int e=neq.size();
if(s==e)
return 0;
long long final=1e18;
long long sum=0,ot=0;
for(int j=s;j<e;j++)
sum+=neq[j];
for(int i=s;i<e;i++)
{
sum-=neq[i];
ot+=neq[i];
long long bf=i+1-s;
long long af=e-i-1;
final=min(final,(bf*((long long)(neq[i])))-ot + (sum-(af*((long long)neq[i]))));
}
return final;
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int k,n;
cin>>k>>n;
long long total=0;
vector<pair<int,int>> vlp;
vector<int> neq;
for(int j=0;j<n;j++)
{
char a,c;
int b,d;
cin>>a>>b>>c>>d;
if(a!=c)
{
neq.push_back(b);
neq.push_back(d);
vlp.push_back({b,d});
total+=1;
}
else
{
total+=abs(b-d);
}
}
sort(begin(neq),end(neq));
if(k==1)
{
cout<<total+BFG(neq)<<'\n';
}
else
{
if(vlp.size()==0)
{
cout<<total<<'\n';
return 0;
}
// Full Brute force
long long pl=1e18;
for(int i=0;i<neq.size();i++)
{
for(int j=i+1;j<neq.size();j++)
{
long long cur=0;
for(int k=0;k<vlp.size();k++)
{
cur+=min(abs(vlp[k].first-neq[i])+abs(vlp[k].second-neq[i]),abs(vlp[k].first-neq[j])+abs(vlp[k].second-neq[j]));
}
pl=min(pl,cur);
}
}
// Did not work
// long long pl=1e18;
// for(int j=0;j<vlp.size();j++)
// {
// vector<int> fpp,spp;
// for(int k=0;k<j;k++)
// {
// fpp.push_back(vlp[k].first);
// fpp.push_back(vlp[k].second);
// }
// for(int k=j;k<vlp.size();k++)
// {
// spp.push_back(vlp[k].first);
// spp.push_back(vlp[k].second);
// }
// sort(begin(fpp),end(fpp));
// sort(begin(spp),end(spp));
// pl=min(pl,BFG(fpp)+BFG(spp));
// // if((BFG(0,j)+BFG(j+1,neq.size()))==5)
// // {
// // cout<<"For "<<j<<endl;
// // }
// }
cout<<total+pl<<'\n';
}
return 0;
}
Compilation message
bridge.cpp: In function 'int main()':
bridge.cpp:67:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
67 | for(int i=0;i<neq.size();i++)
| ~^~~~~~~~~~~
bridge.cpp:69:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
69 | for(int j=i+1;j<neq.size();j++)
| ~^~~~~~~~~~~
bridge.cpp:72:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
72 | for(int k=0;k<vlp.size();k++)
| ~^~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
460 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
624 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
2 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 |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
356 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 |
2 ms |
600 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
600 KB |
Output is correct |
8 |
Correct |
2 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 |
12 |
Correct |
25 ms |
3016 KB |
Output is correct |
13 |
Correct |
37 ms |
4480 KB |
Output is correct |
14 |
Correct |
27 ms |
3276 KB |
Output is correct |
15 |
Correct |
22 ms |
2984 KB |
Output is correct |
16 |
Correct |
21 ms |
3688 KB |
Output is correct |
17 |
Correct |
28 ms |
4400 KB |
Output is correct |
18 |
Correct |
29 ms |
4036 KB |
Output is correct |
19 |
Correct |
35 ms |
4520 KB |
Output is correct |
20 |
Correct |
23 ms |
3988 KB |
Output is correct |
21 |
Correct |
30 ms |
4152 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
456 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
6 ms |
512 KB |
Output is correct |
4 |
Correct |
4 ms |
456 KB |
Output is correct |
5 |
Correct |
2 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
5 ms |
460 KB |
Output is correct |
8 |
Correct |
7 ms |
348 KB |
Output is correct |
9 |
Correct |
6 ms |
456 KB |
Output is correct |
10 |
Correct |
6 ms |
604 KB |
Output is correct |
11 |
Correct |
6 ms |
348 KB |
Output is correct |
12 |
Correct |
6 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
2 ms |
508 KB |
Output is correct |
3 |
Correct |
4 ms |
348 KB |
Output is correct |
4 |
Correct |
6 ms |
504 KB |
Output is correct |
5 |
Correct |
2 ms |
688 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
7 ms |
612 KB |
Output is correct |
8 |
Correct |
9 ms |
504 KB |
Output is correct |
9 |
Correct |
6 ms |
348 KB |
Output is correct |
10 |
Correct |
6 ms |
348 KB |
Output is correct |
11 |
Correct |
6 ms |
348 KB |
Output is correct |
12 |
Correct |
9 ms |
348 KB |
Output is correct |
13 |
Execution timed out |
2093 ms |
460 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
504 KB |
Output is correct |
2 |
Correct |
1 ms |
452 KB |
Output is correct |
3 |
Correct |
6 ms |
348 KB |
Output is correct |
4 |
Correct |
4 ms |
456 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
6 ms |
348 KB |
Output is correct |
8 |
Correct |
6 ms |
348 KB |
Output is correct |
9 |
Correct |
7 ms |
348 KB |
Output is correct |
10 |
Correct |
6 ms |
348 KB |
Output is correct |
11 |
Correct |
5 ms |
348 KB |
Output is correct |
12 |
Correct |
9 ms |
348 KB |
Output is correct |
13 |
Execution timed out |
2070 ms |
348 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |