#include <bits/stdc++.h>
#define endl '\n'
using namespace std;
typedef long long ll;
void speed()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
}
const int maxn = 1e5 + 10, smalln = 2010;
struct work
{
ll x, y;
} w[maxn];
int k, n;
ll pref[smalln][smalln][2];
bool cmp(work w1, work w2)
{
return w1.x + w1.y < w2.x + w2.y;
}
void solve()
{
cin >> k >> n;
ll ans = 0;
vector < ll > v, sx;
for (int i = 1; i <= n; i ++)
{
char c1, c2;
cin >> c1 >> w[i].x >> c2 >> w[i].y;
if (w[i].x > w[i].y)
swap(w[i].x, w[i].y);
if (c1 == c2)
{
ans = ans + w[i].y - w[i].x;
i --;
n --;
}
else
{
v.push_back(w[i].y);
v.push_back(w[i].x);
sx.push_back(w[i].x + w[i].y);
ans ++;
}
}
//for (int i = 1; i <= n; i ++)
// cout << w[i].x << " - " << w[i].y << endl;
if (k == 1)
{
sort(v.begin(), v.end());
for (int i = 0; i < v.size() / 2; i ++)
ans = ans - (v[i]);
for (int i = v.size() / 2; i < v.size(); i ++)
ans = ans + v[i];
cout << ans << endl;
}
else
{
ll best = 1e18;
if (v.empty())
best = 0;
sort(w + 1, w + n + 1, cmp);
sort(v.begin(), v.end());
for (int i = 0; i < v.size(); i ++)
{
for (int j = 1; j <= n; j ++)
{
pref[i][j][0] = pref[i][j - 1][0] + abs(v[i] - w[j].x) + abs(v[i] - w[j].y);
}
for (int j = n; j > 0; j --)
{
pref[i][j][1] = pref[i][j + 1][1] + abs(v[i] - w[j].x) + abs(v[i] - w[j].y);
}
}
for (int i = 1; i <= n; i ++)
{
///cout << "pair " << w[i].x << " " << w[i].y << endl;
}
for (int i = 0; i < v.size(); i ++)
{
int pt = 1;
for (int j = i + 1; j < v.size(); j ++)
{
while(pt <= n && w[pt].x + w[pt].y <= v[j] + v[i])
pt ++;
ll calc = 0;
/**cout << v[i] << " " << v[j] << endl;
for (int k = 1; k <= n; k ++)
{
ll s1 = abs(v[i] - w[k].x) + abs(v[i] - w[k].y);
ll s2 = abs(v[j] - w[k].x) + abs(v[j] - w[k].y);
if (s1 <= s2)
cout << 0 << " ";
else
cout << 1 << " ";
calc = calc + min(s1, s2);
}
cout << endl;*/
/**for (int k = 1; k < pt; k ++)
{
calc = calc + abs(v[i] - w[k].x) + abs(v[i] - w[k].y);
}
for (int k = pt; k <= n; k ++)
{
calc = calc + abs(v[j] - w[k].x) + abs(v[j] - w[k].y);
}
best = min(best, calc);*/
///cout << v[i] << " : " << v[j] << " : " << pt << " " << pref[i][pt][0] << " " << pref[j][pt][1] << endl;
best = min(best, pref[i][pt - 1][0] + pref[j][pt][1]);
}
}
cout << ans + best << endl;
}
}
int main()
{
solve();
return 0;
}
/**
2 5
B 0 A 4
B 1 B 3
A 5 B 7
B 2 A 6
B 1 A 7
*/
Compilation message
bridge.cpp: In function 'void solve()':
bridge.cpp:61:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
61 | for (int i = 0; i < v.size() / 2; i ++)
| ~~^~~~~~~~~~~~~~
bridge.cpp:63:38: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
63 | for (int i = v.size() / 2; i < v.size(); i ++)
| ~~^~~~~~~~~~
bridge.cpp:76:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
76 | for (int i = 0; i < v.size(); i ++)
| ~~^~~~~~~~~~
bridge.cpp:92:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
92 | for (int i = 0; i < v.size(); i ++)
| ~~^~~~~~~~~~
bridge.cpp:95:35: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
95 | for (int j = i + 1; j < v.size(); j ++)
| ~~^~~~~~~~~~
bridge.cpp:100:20: warning: unused variable 'calc' [-Wunused-variable]
100 | ll calc = 0;
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
2 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
2 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
46 ms |
5180 KB |
Output is correct |
13 |
Correct |
100 ms |
5316 KB |
Output is correct |
14 |
Correct |
63 ms |
4796 KB |
Output is correct |
15 |
Correct |
58 ms |
3008 KB |
Output is correct |
16 |
Correct |
73 ms |
5176 KB |
Output is correct |
17 |
Correct |
89 ms |
5144 KB |
Output is correct |
18 |
Correct |
81 ms |
5156 KB |
Output is correct |
19 |
Correct |
98 ms |
5228 KB |
Output is correct |
20 |
Correct |
77 ms |
5240 KB |
Output is correct |
21 |
Correct |
89 ms |
5176 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
1236 KB |
Output is correct |
4 |
Correct |
1 ms |
1236 KB |
Output is correct |
5 |
Correct |
1 ms |
852 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
1364 KB |
Output is correct |
8 |
Correct |
1 ms |
1364 KB |
Output is correct |
9 |
Correct |
1 ms |
1364 KB |
Output is correct |
10 |
Correct |
1 ms |
1364 KB |
Output is correct |
11 |
Correct |
1 ms |
1364 KB |
Output is correct |
12 |
Correct |
1 ms |
1364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
1236 KB |
Output is correct |
4 |
Correct |
1 ms |
1236 KB |
Output is correct |
5 |
Correct |
1 ms |
724 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
1364 KB |
Output is correct |
8 |
Correct |
1 ms |
1364 KB |
Output is correct |
9 |
Correct |
1 ms |
1364 KB |
Output is correct |
10 |
Correct |
1 ms |
1364 KB |
Output is correct |
11 |
Correct |
2 ms |
1364 KB |
Output is correct |
12 |
Correct |
1 ms |
1364 KB |
Output is correct |
13 |
Correct |
39 ms |
38772 KB |
Output is correct |
14 |
Correct |
42 ms |
39244 KB |
Output is correct |
15 |
Correct |
50 ms |
38348 KB |
Output is correct |
16 |
Correct |
5 ms |
5844 KB |
Output is correct |
17 |
Correct |
14 ms |
13524 KB |
Output is correct |
18 |
Correct |
7 ms |
7764 KB |
Output is correct |
19 |
Correct |
37 ms |
39628 KB |
Output is correct |
20 |
Correct |
45 ms |
39704 KB |
Output is correct |
21 |
Correct |
41 ms |
39616 KB |
Output is correct |
22 |
Correct |
45 ms |
39724 KB |
Output is correct |
23 |
Correct |
43 ms |
39632 KB |
Output is correct |
24 |
Correct |
44 ms |
39716 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
1236 KB |
Output is correct |
4 |
Correct |
1 ms |
1236 KB |
Output is correct |
5 |
Correct |
1 ms |
724 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
1364 KB |
Output is correct |
8 |
Correct |
1 ms |
1364 KB |
Output is correct |
9 |
Correct |
1 ms |
1364 KB |
Output is correct |
10 |
Correct |
1 ms |
1364 KB |
Output is correct |
11 |
Correct |
1 ms |
1364 KB |
Output is correct |
12 |
Correct |
1 ms |
1364 KB |
Output is correct |
13 |
Correct |
39 ms |
38860 KB |
Output is correct |
14 |
Correct |
42 ms |
39292 KB |
Output is correct |
15 |
Correct |
44 ms |
38392 KB |
Output is correct |
16 |
Correct |
5 ms |
5816 KB |
Output is correct |
17 |
Correct |
14 ms |
13524 KB |
Output is correct |
18 |
Correct |
7 ms |
7764 KB |
Output is correct |
19 |
Correct |
40 ms |
39624 KB |
Output is correct |
20 |
Correct |
44 ms |
39660 KB |
Output is correct |
21 |
Correct |
40 ms |
39672 KB |
Output is correct |
22 |
Correct |
46 ms |
39612 KB |
Output is correct |
23 |
Correct |
40 ms |
39604 KB |
Output is correct |
24 |
Correct |
47 ms |
39760 KB |
Output is correct |
25 |
Runtime error |
1308 ms |
139464 KB |
Execution killed with signal 11 |
26 |
Halted |
0 ms |
0 KB |
- |