#include <bits/stdc++.h>
using namespace std;
int B, N;
vector<pair<int, int> > P;
namespace subtask1
{
long long solve()
{
vector<pair<int, int> > p = P;
int n = p.size();
if (n == 0) return 0;
long long ans = 1e18;
for (int i = 0; i < n; ++i)
{
long long tmp1 = 0, tmp2 = 0;
for (int j = 0; j < n; ++j)
{
tmp1 += abs(p[i].first - p[j].first) + abs(p[i].first - p[j].second);
tmp2 += abs(p[i].second - p[j].first) + abs(p[i].second - p[j].second);
}
ans = min(ans, min(tmp1, tmp2));
}
return ans + n;
}
bool satisfies()
{
return B == 1 && 1 <= N && N <= 1000;
}
}
namespace subtask2
{
long long solve()
{
long long ans = 0;
vector<int> x;
int n = P.size();
if (n == 0) return 0;
for (int i = 0; i < n; ++i)
{
x.push_back(P[i].first);
x.push_back(P[i].second);
ans -= P[i].first + P[i].second;
}
sort(x.begin(), x.end());
for (int i = 2 * n - 1; i >= n; --i)
ans += 2 * x[i];
return ans + n;
}
bool satisfies()
{
return B == 1 && 1 <= N && N <= 100000;
}
}
namespace subtask3
{
long long solve()
{
long long ans = 1e18;
int n = P.size();
if (n == 0) return 0;
vector<int> p;
for (int i = 0; i < n; ++i)
{
p.push_back(P[i].first);
p.push_back(P[i].second);
}
for (int i = 0; i < 2 * n; ++i)
for (int j = 0; j < 2 * n; ++j)
{
long long tmp = 0;
for (int k = 0; k < n; ++k)
{
int v1 = abs(P[k].first - p[i]) + abs(P[k].second - p[i]);
int v2 = abs(P[k].first - p[j]) + abs(P[k].second - p[j]);
tmp += min(v1, v2);
}
ans = min(ans, tmp);
}
return ans + n;
}
bool satisfies()
{
return B == 2 && 1 <= N && N <= 100;
}
}
namespace subtask4
{
bool cmp(pair<int, int> a, pair<int, int> b)
{
return (a.first + a.second) < (b.first + b.second);
}
long long compute(vector<pair<int, int> > P)
{
long long ans = 0;
vector<int> x;
int n = P.size();
if (n == 0) return 0;
for (int i = 0; i < n; ++i)
{
x.push_back(P[i].first);
x.push_back(P[i].second);
ans -= P[i].first + P[i].second;
}
sort(x.begin(), x.end());
for (int i = 2 * n - 1; i >= n; --i)
ans += 2 * x[i];
return ans + n;
}
long long solve()
{
long long ans = subtask2::solve();
int n = P.size();
if (n == 0) return 0;
vector<pair<int, int> > p = P;
sort(p.begin(), p.end(), cmp);
for (int i = 1; i <= n; ++i)
{
ans = min(ans,
compute(vector<pair<int, int> >(p.begin(), p.begin() + i)) +
compute(vector<pair<int, int> >(p.begin() + i, p.end())));
}
return ans;
}
bool satisfies()
{
return B == 2 && 1 <= N && N <= 1000;
}
}
namespace subtask5
{
vector<pair<int, int> > p;
int n;
bool cmp(pair<int, int> a, pair<int, int> b)
{
return (a.first + a.second) < (b.first + b.second);
}
vector<long long> compute()
{
priority_queue<int> max_heap;
priority_queue<int, vector<int>, greater<int> > min_heap;
long long sumMax = p[0].first, sumMin = p[0].second;
max_heap.push(p[0].first);
min_heap.push(p[0].second);
vector<long long> tmp;
tmp.push_back(sumMin - sumMax);
for (int i = 1; i < n; ++i)
{
int m = max_heap.top();
int M = min_heap.top();
int A = p[i].first, B = p[i].second;
if (A <= m && B <= m)
{
max_heap.pop();
max_heap.push(A);
max_heap.push(B);
min_heap.push(m);
sumMax += A + B - m;
sumMin += m;
}
else
if (A >= M && B >= M)
{
min_heap.pop();
min_heap.push(A);
min_heap.push(B);
max_heap.push(M);
sumMin += A + B - M;
sumMax += M;
}
else
{
max_heap.push(A);
min_heap.push(B);
sumMax += A;
sumMin += B;
}
tmp.push_back(sumMin - sumMax);
}
return tmp;
}
long long solve()
{
p = P;
sort(p.begin(), p.end(), cmp);
n = p.size();
if (n == 0) return 0;
vector<long long> val1n = compute();
reverse(p.begin(), p.end());
vector<long long> valn1 = compute();
long long ans = 1e18;
for (int i = 0; i < n - 1; ++i)
ans = min(ans, val1n[i] + valn1[n - i - 2]);
return min(ans + n, subtask2::solve());
}
bool satisfies()
{
return B == 2 && 1 <= N && N <= 100000;
}
}
function<bool()> validators[] =
{
subtask1::satisfies,
subtask2::satisfies,
subtask3::satisfies,
subtask4::satisfies,
subtask5::satisfies,
};
function<long long()> solvers[] =
{
subtask1::solve,
subtask2::solve,
subtask3::solve,
subtask4::solve,
subtask5::solve
};
int main()
{
scanf("%d %d", &B, &N);
long long ans = 0;
for (int i = 0; i < N; ++i)
{
char a[10], b[10];
int s, e;
scanf("%s %d %s %d", a, &s, b, &e);
if (a[0] == b[0])
ans += abs(e - s);
else
P.push_back(make_pair(min(s, e), max(s, e)));
}
vector<long long> answers;
for (int i = 0; i < 5; ++i)
if (validators[i]())
answers.push_back(solvers[i]() + ans);
sort(answers.begin(), answers.end());
assert(answers.front() == answers.back());
printf("%lld\n", answers[0]);
return 0;
}
Compilation message
bridge.cpp: In function 'int main()':
bridge.cpp:249:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
249 | scanf("%d %d", &B, &N);
| ~~~~~^~~~~~~~~~~~~~~~~
bridge.cpp:255:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
255 | scanf("%s %d %s %d", a, &s, b, &e);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
3 ms |
348 KB |
Output is correct |
4 |
Correct |
3 ms |
348 KB |
Output is correct |
5 |
Correct |
3 ms |
484 KB |
Output is correct |
6 |
Correct |
3 ms |
348 KB |
Output is correct |
7 |
Correct |
3 ms |
348 KB |
Output is correct |
8 |
Correct |
3 ms |
348 KB |
Output is correct |
9 |
Correct |
3 ms |
348 KB |
Output is correct |
10 |
Correct |
3 ms |
476 KB |
Output is correct |
11 |
Correct |
3 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
3 ms |
460 KB |
Output is correct |
4 |
Correct |
3 ms |
348 KB |
Output is correct |
5 |
Correct |
3 ms |
348 KB |
Output is correct |
6 |
Correct |
3 ms |
348 KB |
Output is correct |
7 |
Correct |
3 ms |
348 KB |
Output is correct |
8 |
Correct |
3 ms |
480 KB |
Output is correct |
9 |
Correct |
3 ms |
476 KB |
Output is correct |
10 |
Correct |
3 ms |
348 KB |
Output is correct |
11 |
Correct |
3 ms |
344 KB |
Output is correct |
12 |
Correct |
23 ms |
3520 KB |
Output is correct |
13 |
Correct |
37 ms |
5064 KB |
Output is correct |
14 |
Correct |
28 ms |
3760 KB |
Output is correct |
15 |
Correct |
22 ms |
2980 KB |
Output is correct |
16 |
Correct |
26 ms |
4300 KB |
Output is correct |
17 |
Correct |
27 ms |
5064 KB |
Output is correct |
18 |
Correct |
33 ms |
4560 KB |
Output is correct |
19 |
Correct |
36 ms |
4932 KB |
Output is correct |
20 |
Correct |
28 ms |
4660 KB |
Output is correct |
21 |
Correct |
34 ms |
4808 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
504 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
7 ms |
348 KB |
Output is correct |
4 |
Correct |
8 ms |
444 KB |
Output is correct |
5 |
Correct |
2 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
11 ms |
348 KB |
Output is correct |
8 |
Correct |
13 ms |
440 KB |
Output is correct |
9 |
Correct |
11 ms |
348 KB |
Output is correct |
10 |
Correct |
11 ms |
348 KB |
Output is correct |
11 |
Correct |
11 ms |
348 KB |
Output is correct |
12 |
Correct |
11 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
10 ms |
344 KB |
Output is correct |
4 |
Correct |
8 ms |
348 KB |
Output is correct |
5 |
Correct |
2 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
10 ms |
440 KB |
Output is correct |
8 |
Correct |
11 ms |
344 KB |
Output is correct |
9 |
Correct |
11 ms |
348 KB |
Output is correct |
10 |
Correct |
11 ms |
348 KB |
Output is correct |
11 |
Correct |
11 ms |
348 KB |
Output is correct |
12 |
Correct |
11 ms |
436 KB |
Output is correct |
13 |
Correct |
18 ms |
348 KB |
Output is correct |
14 |
Correct |
62 ms |
508 KB |
Output is correct |
15 |
Correct |
70 ms |
348 KB |
Output is correct |
16 |
Correct |
2 ms |
344 KB |
Output is correct |
17 |
Correct |
19 ms |
344 KB |
Output is correct |
18 |
Correct |
10 ms |
344 KB |
Output is correct |
19 |
Correct |
20 ms |
348 KB |
Output is correct |
20 |
Correct |
18 ms |
344 KB |
Output is correct |
21 |
Correct |
51 ms |
344 KB |
Output is correct |
22 |
Correct |
19 ms |
348 KB |
Output is correct |
23 |
Correct |
22 ms |
344 KB |
Output is correct |
24 |
Correct |
18 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
436 KB |
Output is correct |
3 |
Correct |
7 ms |
444 KB |
Output is correct |
4 |
Correct |
8 ms |
348 KB |
Output is correct |
5 |
Correct |
2 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
11 ms |
348 KB |
Output is correct |
8 |
Correct |
11 ms |
348 KB |
Output is correct |
9 |
Correct |
11 ms |
348 KB |
Output is correct |
10 |
Correct |
11 ms |
348 KB |
Output is correct |
11 |
Correct |
11 ms |
440 KB |
Output is correct |
12 |
Correct |
11 ms |
440 KB |
Output is correct |
13 |
Correct |
18 ms |
488 KB |
Output is correct |
14 |
Correct |
63 ms |
344 KB |
Output is correct |
15 |
Correct |
72 ms |
436 KB |
Output is correct |
16 |
Correct |
2 ms |
348 KB |
Output is correct |
17 |
Correct |
19 ms |
480 KB |
Output is correct |
18 |
Correct |
10 ms |
348 KB |
Output is correct |
19 |
Correct |
20 ms |
348 KB |
Output is correct |
20 |
Correct |
18 ms |
348 KB |
Output is correct |
21 |
Correct |
51 ms |
348 KB |
Output is correct |
22 |
Correct |
19 ms |
344 KB |
Output is correct |
23 |
Correct |
19 ms |
348 KB |
Output is correct |
24 |
Correct |
19 ms |
528 KB |
Output is correct |
25 |
Correct |
37 ms |
6088 KB |
Output is correct |
26 |
Correct |
48 ms |
6336 KB |
Output is correct |
27 |
Correct |
65 ms |
7096 KB |
Output is correct |
28 |
Correct |
68 ms |
7692 KB |
Output is correct |
29 |
Correct |
67 ms |
7644 KB |
Output is correct |
30 |
Correct |
35 ms |
4064 KB |
Output is correct |
31 |
Correct |
32 ms |
7100 KB |
Output is correct |
32 |
Correct |
51 ms |
7760 KB |
Output is correct |
33 |
Correct |
38 ms |
7364 KB |
Output is correct |
34 |
Correct |
65 ms |
7708 KB |
Output is correct |
35 |
Correct |
43 ms |
7260 KB |
Output is correct |
36 |
Correct |
61 ms |
7356 KB |
Output is correct |
37 |
Correct |
31 ms |
6084 KB |
Output is correct |