#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 10;
const ll inf = 1e18;
vector<pair<int, int> > eve;
vector<ll> places;
ll Answer = inf;
ll calculate(int i, int j){
ll sum = 0;
for (auto k : eve){
ll tmp = min(abs(k.first - places[i]) + abs(k.second - places[i]),
abs(k.first - places[j]) + abs(k.second - places[j]));
sum += tmp;
}
return sum;
}
void find(int L, int R, int l, int r){
if (L >= R)
return;
int mid = (L + R) >> 1;
ll k = inf, idx = -1;
for (int i = l; i < min(mid, r); i++){
ll tmp = calculate(i, mid);
if (tmp < k){
k = tmp;
idx = i;
}
}
Answer = min(Answer, k);
find(L, mid, l, idx + 1);
find(mid + 1, R, idx, r);
}
ll solve(){
int n = eve.size();
vector<ll> a(2 * n + 1);
for (int i = 0; i < n; i++){
a[2 * i + 0 + 1] = eve[i].first;
a[2 * i + 1 + 1] = eve[i].second;
}
sort(a.begin() + 1, a.end());
n = 2 * n;
for (int i = 1; i <= n; i++)
a[i] += a[i - 1];
ll answer = inf;
for (int i = 1; i <= n; i++){
ll T = a[i] - a[i - 1];
answer = min(answer, T * (i - (n - i)) - a[i] + (a[n] - a[i]));
}
return answer + eve.size();
}
int main(){
ios_base::sync_with_stdio(false);
int k, n;
cin >> k >> n;
ll answer = 0;
for (int i = 0; i < n; i++){
int x, y;
char a, b;
cin >> a >> x >> b >> y;
if (a == b){
answer += abs(x - y);
continue;
}
if (a == 'B')
swap(x, y);
eve.push_back({x, y});
}
if (eve.empty())
return cout << answer << endl, 0;
if (k == 1){
cout << answer + solve() << '\n';
return 0;
}
for (int i = 0; i < eve.size(); i++){
places.push_back(eve[i].first);
places.push_back(eve[i].second);
}
sort(places.begin(), places.end());
int m = places.size();
find(0, m, 0, m);
cout << Answer + answer + eve.size() << endl;
return 0;
}
Compilation message
bridge.cpp: In function 'int main()':
bridge.cpp:82:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < eve.size(); i++){
~~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
256 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
3 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
3 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
412 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
2 ms |
384 KB |
Output is correct |
9 |
Correct |
3 ms |
384 KB |
Output is correct |
10 |
Correct |
4 ms |
384 KB |
Output is correct |
11 |
Correct |
3 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
256 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
3 ms |
384 KB |
Output is correct |
5 |
Correct |
3 ms |
384 KB |
Output is correct |
6 |
Correct |
3 ms |
384 KB |
Output is correct |
7 |
Correct |
3 ms |
384 KB |
Output is correct |
8 |
Correct |
3 ms |
384 KB |
Output is correct |
9 |
Correct |
3 ms |
384 KB |
Output is correct |
10 |
Correct |
3 ms |
384 KB |
Output is correct |
11 |
Correct |
3 ms |
384 KB |
Output is correct |
12 |
Correct |
29 ms |
2780 KB |
Output is correct |
13 |
Correct |
48 ms |
2928 KB |
Output is correct |
14 |
Correct |
51 ms |
2544 KB |
Output is correct |
15 |
Correct |
31 ms |
1788 KB |
Output is correct |
16 |
Correct |
37 ms |
2928 KB |
Output is correct |
17 |
Correct |
39 ms |
2800 KB |
Output is correct |
18 |
Correct |
50 ms |
2984 KB |
Output is correct |
19 |
Correct |
50 ms |
2800 KB |
Output is correct |
20 |
Correct |
50 ms |
2800 KB |
Output is correct |
21 |
Correct |
49 ms |
2964 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
284 KB |
Output is correct |
2 |
Correct |
3 ms |
256 KB |
Output is correct |
3 |
Correct |
3 ms |
412 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
5 |
Correct |
3 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
3 ms |
384 KB |
Output is correct |
8 |
Correct |
4 ms |
384 KB |
Output is correct |
9 |
Correct |
4 ms |
384 KB |
Output is correct |
10 |
Correct |
3 ms |
384 KB |
Output is correct |
11 |
Correct |
4 ms |
384 KB |
Output is correct |
12 |
Correct |
3 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
3 ms |
384 KB |
Output is correct |
4 |
Correct |
3 ms |
512 KB |
Output is correct |
5 |
Correct |
3 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
3 ms |
384 KB |
Output is correct |
8 |
Correct |
3 ms |
384 KB |
Output is correct |
9 |
Correct |
3 ms |
384 KB |
Output is correct |
10 |
Correct |
3 ms |
384 KB |
Output is correct |
11 |
Correct |
3 ms |
304 KB |
Output is correct |
12 |
Correct |
3 ms |
384 KB |
Output is correct |
13 |
Correct |
83 ms |
384 KB |
Output is correct |
14 |
Correct |
79 ms |
504 KB |
Output is correct |
15 |
Correct |
71 ms |
384 KB |
Output is correct |
16 |
Correct |
7 ms |
384 KB |
Output is correct |
17 |
Correct |
25 ms |
384 KB |
Output is correct |
18 |
Correct |
11 ms |
384 KB |
Output is correct |
19 |
Correct |
74 ms |
408 KB |
Output is correct |
20 |
Correct |
74 ms |
504 KB |
Output is correct |
21 |
Correct |
72 ms |
384 KB |
Output is correct |
22 |
Correct |
66 ms |
384 KB |
Output is correct |
23 |
Correct |
70 ms |
504 KB |
Output is correct |
24 |
Correct |
74 ms |
504 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
256 KB |
Output is correct |
2 |
Correct |
3 ms |
384 KB |
Output is correct |
3 |
Correct |
3 ms |
384 KB |
Output is correct |
4 |
Correct |
3 ms |
384 KB |
Output is correct |
5 |
Correct |
3 ms |
432 KB |
Output is correct |
6 |
Correct |
3 ms |
256 KB |
Output is correct |
7 |
Correct |
3 ms |
384 KB |
Output is correct |
8 |
Correct |
3 ms |
384 KB |
Output is correct |
9 |
Correct |
3 ms |
384 KB |
Output is correct |
10 |
Correct |
3 ms |
384 KB |
Output is correct |
11 |
Correct |
3 ms |
384 KB |
Output is correct |
12 |
Correct |
3 ms |
384 KB |
Output is correct |
13 |
Correct |
79 ms |
420 KB |
Output is correct |
14 |
Correct |
100 ms |
460 KB |
Output is correct |
15 |
Correct |
75 ms |
384 KB |
Output is correct |
16 |
Correct |
9 ms |
384 KB |
Output is correct |
17 |
Correct |
35 ms |
456 KB |
Output is correct |
18 |
Correct |
12 ms |
384 KB |
Output is correct |
19 |
Correct |
70 ms |
384 KB |
Output is correct |
20 |
Correct |
66 ms |
424 KB |
Output is correct |
21 |
Correct |
81 ms |
384 KB |
Output is correct |
22 |
Correct |
78 ms |
384 KB |
Output is correct |
23 |
Correct |
83 ms |
420 KB |
Output is correct |
24 |
Correct |
73 ms |
384 KB |
Output is correct |
25 |
Execution timed out |
2044 ms |
3312 KB |
Time limit exceeded |
26 |
Halted |
0 ms |
0 KB |
- |