#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());
/* if (n <= 1){
int m = places.size();
ll tot = inf;
for (int i = 0; i < m; i++){
for (int j = i + 1; j < m; j++){
ll sum = 0;
sum = calculate(i, j);
tot = min(tot, sum + answer + m / 2);
}
}
return cout << tot << endl, 0;
}
*/ if (n <= 1000){
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++){
~~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
3 ms |
384 KB |
Output is correct |
3 |
Correct |
2 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 |
3 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
3 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
3 ms |
384 KB |
Output is correct |
11 |
Correct |
3 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
256 KB |
Output is correct |
2 |
Correct |
3 ms |
384 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 |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 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 |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
3 ms |
356 KB |
Output is correct |
12 |
Correct |
29 ms |
2800 KB |
Output is correct |
13 |
Correct |
51 ms |
2772 KB |
Output is correct |
14 |
Correct |
52 ms |
2540 KB |
Output is correct |
15 |
Correct |
36 ms |
1908 KB |
Output is correct |
16 |
Correct |
39 ms |
2920 KB |
Output is correct |
17 |
Correct |
51 ms |
2924 KB |
Output is correct |
18 |
Correct |
52 ms |
2796 KB |
Output is correct |
19 |
Correct |
51 ms |
2800 KB |
Output is correct |
20 |
Correct |
35 ms |
2800 KB |
Output is correct |
21 |
Correct |
59 ms |
2800 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
384 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 |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
4 ms |
384 KB |
Output is correct |
8 |
Correct |
4 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 |
# |
결과 |
실행 시간 |
메모리 |
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 |
4 ms |
320 KB |
Output is correct |
5 |
Correct |
2 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 |
6 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 |
72 ms |
384 KB |
Output is correct |
14 |
Correct |
90 ms |
436 KB |
Output is correct |
15 |
Correct |
70 ms |
440 KB |
Output is correct |
16 |
Correct |
9 ms |
384 KB |
Output is correct |
17 |
Correct |
22 ms |
384 KB |
Output is correct |
18 |
Correct |
12 ms |
384 KB |
Output is correct |
19 |
Correct |
71 ms |
384 KB |
Output is correct |
20 |
Correct |
75 ms |
504 KB |
Output is correct |
21 |
Correct |
65 ms |
384 KB |
Output is correct |
22 |
Correct |
74 ms |
504 KB |
Output is correct |
23 |
Correct |
71 ms |
384 KB |
Output is correct |
24 |
Correct |
73 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
3 ms |
384 KB |
Output is correct |
5 |
Correct |
2 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 |
384 KB |
Output is correct |
12 |
Correct |
3 ms |
384 KB |
Output is correct |
13 |
Correct |
61 ms |
508 KB |
Output is correct |
14 |
Correct |
67 ms |
508 KB |
Output is correct |
15 |
Correct |
73 ms |
504 KB |
Output is correct |
16 |
Correct |
10 ms |
384 KB |
Output is correct |
17 |
Correct |
22 ms |
384 KB |
Output is correct |
18 |
Correct |
15 ms |
384 KB |
Output is correct |
19 |
Correct |
104 ms |
504 KB |
Output is correct |
20 |
Correct |
89 ms |
476 KB |
Output is correct |
21 |
Correct |
93 ms |
436 KB |
Output is correct |
22 |
Correct |
73 ms |
384 KB |
Output is correct |
23 |
Correct |
75 ms |
384 KB |
Output is correct |
24 |
Correct |
77 ms |
476 KB |
Output is correct |
25 |
Incorrect |
31 ms |
4080 KB |
Output isn't correct |
26 |
Halted |
0 ms |
0 KB |
- |