#pragma GCC optimize("O3,unroll-loops")
#include <bits/stdc++.h>
#pragma pack(0)
#define int long long
#define ll long long
#define pii pair<ll, ll>
#define pll pair<ll, ll>
#define F first
#define S second
#define MOD 1000000007
#define MOD2 998244353
#define _INFINITY 9223372036854775807
#define fast ios::sync_with_stdio(0), cin.tie(0)
using namespace std;
ll n, k, ans1, ans2 = 1e18;
vector<int> v;
vector<pii> u;
signed main()
{
clock_t t = clock();
fast;
cin >> k >> n;
for (int i = 1; i <= n; i++)
{
char c1, c2;
int p1, p2;
cin >> c1 >> p1 >> c2 >> p2;
if (c1 == c2)
ans1 += abs(p1 - p2);
else
{
v.emplace_back(p1);
v.emplace_back(p2);
u.emplace_back(make_pair(p1, p2));
}
}
sort(v.begin(), v.end());
ans2 = v.size() / 2;
if (!v.empty())
{
int mid = v[v.size() / 2];
for (int i : v)
ans2 += abs(mid - i);
}
if (k == 1)
cout << ans2 + ans1 << '\n';
else
{
random_shuffle(v.begin(), v.end());
for (int i : v)
for (int j : v)
if (i != j)
{
ll res = 0;
for (pii p : u)
res += min(abs(p.F - i) + abs(p.S - i) + 1, abs(p.F - j) + abs(p.S - j) + 1);
ans2 = min(ans2, res);
if (clock() - t > CLOCKS_PER_SEC * 1.8)
{
cout << ans1 + ans2 << '\n';
break;
}
}
cout << ans1 + ans2 << '\n';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
4 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
27 ms |
4440 KB |
Output is correct |
13 |
Correct |
49 ms |
4816 KB |
Output is correct |
14 |
Correct |
37 ms |
4696 KB |
Output is correct |
15 |
Correct |
29 ms |
3544 KB |
Output is correct |
16 |
Correct |
36 ms |
4816 KB |
Output is correct |
17 |
Correct |
53 ms |
4816 KB |
Output is correct |
18 |
Correct |
54 ms |
4816 KB |
Output is correct |
19 |
Correct |
48 ms |
4816 KB |
Output is correct |
20 |
Correct |
33 ms |
4816 KB |
Output is correct |
21 |
Correct |
43 ms |
4816 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
14 ms |
364 KB |
Output is correct |
4 |
Correct |
24 ms |
364 KB |
Output is correct |
5 |
Correct |
10 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
28 ms |
364 KB |
Output is correct |
8 |
Correct |
32 ms |
364 KB |
Output is correct |
9 |
Correct |
32 ms |
364 KB |
Output is correct |
10 |
Correct |
33 ms |
364 KB |
Output is correct |
11 |
Correct |
21 ms |
364 KB |
Output is correct |
12 |
Correct |
32 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
12 ms |
364 KB |
Output is correct |
4 |
Correct |
38 ms |
364 KB |
Output is correct |
5 |
Correct |
8 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
364 KB |
Output is correct |
7 |
Correct |
26 ms |
364 KB |
Output is correct |
8 |
Correct |
32 ms |
364 KB |
Output is correct |
9 |
Correct |
31 ms |
364 KB |
Output is correct |
10 |
Correct |
50 ms |
364 KB |
Output is correct |
11 |
Correct |
21 ms |
364 KB |
Output is correct |
12 |
Correct |
34 ms |
364 KB |
Output is correct |
13 |
Incorrect |
1810 ms |
460 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
12 ms |
364 KB |
Output is correct |
4 |
Correct |
24 ms |
364 KB |
Output is correct |
5 |
Correct |
8 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
16 ms |
364 KB |
Output is correct |
8 |
Correct |
48 ms |
364 KB |
Output is correct |
9 |
Correct |
36 ms |
492 KB |
Output is correct |
10 |
Correct |
31 ms |
364 KB |
Output is correct |
11 |
Correct |
21 ms |
364 KB |
Output is correct |
12 |
Correct |
32 ms |
364 KB |
Output is correct |
13 |
Incorrect |
1806 ms |
492 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |