#include <bits/stdc++.h>
using namespace std;
int main()
{
ios_base::sync_with_stdio(false); cin.tie(nullptr);
int k, n;
cin >> k >> n;
vector<pair<int, int>> v;
vector<int> sarasas;
vector<pair<int, int>> vidurio_taskai;
set<int> s;
long long priedas=0;
for(int i=0; i<n; i++)
{
char cg, cd;
int ig, id;
cin >> cg >> ig >> cd >> id;
if(cg!=cd)
{
priedas++;
v.push_back({ig, id});
vidurio_taskai.push_back({(ig+id), v.size()-1});
sarasas.push_back(ig);
sarasas.push_back(id);
}
else
{
priedas+=abs(ig-id);
}
}
if(k==1)
{
sort(sarasas.begin(), sarasas.end());
int tiltas=sarasas.size()/2;
long long sum=priedas;
for(int i=0; i<v.size(); i++)
{
sum+=abs(v[i].first-sarasas[tiltas])+abs(v[i].second-sarasas[tiltas]);
}
cout << sum;
}
else
{
if(vidurio_taskai.size()==0)
{
cout << priedas;
return 0;
}
sort(sarasas.begin(), sarasas.end());
sort(vidurio_taskai.begin(), vidurio_taskai.end());
for(int i=0; i<vidurio_taskai.size(); i++) vidurio_taskai[i].first/=2;
priority_queue<int> dideli, mazi;
long long suma_dideliu=0, suma_mazu=0;
long long ats[vidurio_taskai.size()-1];
for(int i=0; i<vidurio_taskai.size()-1; i++)
{
//cout << "Paimta " << i+1 << " poru: ";
int x=v[vidurio_taskai[i].second].first;
int y=v[vidurio_taskai[i].second].second;
if(x<y) swap(x, y);
if(dideli.size()==0)
{
dideli.push(-x);
mazi.push(y);
suma_dideliu+=x;
suma_mazu+=y;
}
else
{
if(y>-dideli.top())
{
suma_dideliu-=-dideli.top();
suma_mazu+=-dideli.top();
mazi.push(-dideli.top());
dideli.pop();
suma_dideliu+=x+y;
dideli.push(-x);
dideli.push(-y);
}
///prielaida, kad abu i maziausius niekada neis
else
{
suma_dideliu+=x;
suma_mazu+=y;
dideli.push(-x);
mazi.push(y);
}
}
//int mediana=-dideli.top();
ats[i]=suma_dideliu-suma_mazu;
//cout << ats[i][0] << endl;
}
///-----------------------------------------
while(!mazi.empty()) mazi.pop();
while(!dideli.empty()) dideli.pop();
suma_dideliu=0, suma_mazu=0;
for(int i=vidurio_taskai.size()-1; i>0; i--)
{
//cout << "Paimta " << vidurio_taskai.size()-i << " poru nuo galo: ";
int x=v[vidurio_taskai[i].second].first;
int y=v[vidurio_taskai[i].second].second;
if(x<y) swap(x, y);
//cout << x << ' ' << y << " ";
if(dideli.size()==0)
{
dideli.push(-x);
mazi.push(y);
suma_dideliu+=x;
suma_mazu+=y;
}
else
{
if(x>-dideli.top() || x>mazi.top())
{
suma_dideliu+=x;
suma_mazu+=y;
dideli.push(-x);
mazi.push(y);
}
///prielaida, kad abu i didziausius niekada neis
else
{
suma_mazu-=mazi.top();
suma_dideliu+=mazi.top();
dideli.push(-mazi.top());
mazi.pop();
suma_mazu+=x+y;
mazi.push(x);
mazi.push(y);
}
}
//int mediana=-dideli.top();
ats[i-1]+=suma_dideliu-suma_mazu;
//cout << ats[i-1][1] << endl;
}
long long atsakymas=ats[0];
for(int i=1; i<v.size()-1; i++)
{
atsakymas=min(atsakymas, ats[i]);
}
atsakymas+=priedas;
cout << atsakymas;
}
return 0;
}
Compilation message
bridge.cpp: In function 'int main()':
bridge.cpp:39:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
39 | for(int i=0; i<v.size(); i++)
| ~^~~~~~~~~
bridge.cpp:54:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
54 | for(int i=0; i<vidurio_taskai.size(); i++) vidurio_taskai[i].first/=2;
| ~^~~~~~~~~~~~~~~~~~~~~~
bridge.cpp:58:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
58 | for(int i=0; i<vidurio_taskai.size()-1; i++)
| ~^~~~~~~~~~~~~~~~~~~~~~~~
bridge.cpp:141:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
141 | for(int i=1; i<v.size()-1; i++)
| ~^~~~~~~~~~~
# |
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 |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
604 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 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 |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
464 KB |
Output is correct |
12 |
Correct |
13 ms |
3456 KB |
Output is correct |
13 |
Correct |
26 ms |
4280 KB |
Output is correct |
14 |
Correct |
19 ms |
3540 KB |
Output is correct |
15 |
Correct |
15 ms |
3024 KB |
Output is correct |
16 |
Correct |
16 ms |
4032 KB |
Output is correct |
17 |
Correct |
23 ms |
4436 KB |
Output is correct |
18 |
Correct |
22 ms |
4028 KB |
Output is correct |
19 |
Correct |
25 ms |
4416 KB |
Output is correct |
20 |
Correct |
17 ms |
4028 KB |
Output is correct |
21 |
Correct |
22 ms |
4200 KB |
Output is correct |
# |
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 |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 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 |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
456 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 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 |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
344 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
344 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 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 |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
600 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
452 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
516 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Correct |
1 ms |
344 KB |
Output is correct |
24 |
Correct |
1 ms |
344 KB |
Output is correct |
25 |
Correct |
26 ms |
5572 KB |
Output is correct |
26 |
Correct |
48 ms |
5640 KB |
Output is correct |
27 |
Correct |
65 ms |
6556 KB |
Output is correct |
28 |
Correct |
61 ms |
6484 KB |
Output is correct |
29 |
Correct |
61 ms |
6460 KB |
Output is correct |
30 |
Correct |
31 ms |
3536 KB |
Output is correct |
31 |
Correct |
22 ms |
5852 KB |
Output is correct |
32 |
Correct |
42 ms |
6140 KB |
Output is correct |
33 |
Correct |
36 ms |
5820 KB |
Output is correct |
34 |
Correct |
54 ms |
6076 KB |
Output is correct |
35 |
Correct |
33 ms |
6332 KB |
Output is correct |
36 |
Correct |
51 ms |
6076 KB |
Output is correct |
37 |
Correct |
22 ms |
5568 KB |
Output is correct |