/*
بسم الله الرحمن الرحيم
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;
long long t=0;
int i=f[d.size()-1];
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++)
{
}
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:59: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]
59 | for (int i=0;i<f.size();i++)
| ~^~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
552 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 |
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 |
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 |
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 |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
19 ms |
5308 KB |
Output is correct |
13 |
Correct |
33 ms |
5292 KB |
Output is correct |
14 |
Correct |
29 ms |
4772 KB |
Output is correct |
15 |
Correct |
20 ms |
3276 KB |
Output is correct |
16 |
Correct |
21 ms |
5304 KB |
Output is correct |
17 |
Correct |
24 ms |
5344 KB |
Output is correct |
18 |
Correct |
30 ms |
5352 KB |
Output is correct |
19 |
Correct |
33 ms |
5120 KB |
Output is correct |
20 |
Correct |
26 ms |
5312 KB |
Output is correct |
21 |
Correct |
30 ms |
5312 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 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |