#pragma GCC optimize("O3")
#include <stdio.h>
#define N 1000000
unsigned int X = 12345;
int rand_() {
return (X *= 3) >> 1;
}
int xx[N * 2];
int compare_x(const void *a, const void *b) {
int i = *(int *) a;
int j = *(int *) b;
return xx[i] - xx[j];
}
int compare_(const void *a, const void *b) {
int ia = *(int *) a;
int ib = *(int *) b;
return ib - ia;
}
int (*compare)(const void *, const void *);
void sort(int *aa, int l, int r) {
while (l < r) {
int i = l, j = l, k = r, a = aa[l + rand_() % (r - l)], tmp;
while (j < k) {
int c = compare(&aa[j], &a);
if (c == 0)
j++;
else if (c < 0) {
tmp = aa[i], aa[i] = aa[j], aa[j] = tmp;
i++, j++;
} else {
k--;
tmp = aa[j], aa[j] = aa[k], aa[k] = tmp;
}
}
sort(aa, l, i);
l = k;
}
}
int main() {
static int ii[N * 2], pp[N * 2], cc[N * 2];
static char visited[N * 2];
int n, n_, m, h, i, ans;
scanf("%d%d", &n, &m);
for (i = 0; i < n * 2; i++) {
scanf("%d", &xx[i]);
ii[i] = i;
}
compare = compare_x, sort(ii, 0, n * 2);
for (h = 0; h < n * 2; h++)
pp[ii[h]] = h + 1 == n * 2 ? -1 : ii[h + 1] ^ 1;
ans = 0;
for (i = ii[0] ^ 1; i != -1; i = pp[i])
visited[i] = 1, ans++;
n_ = 0;
for (i = 0; i < n * 2; i++)
if (!visited[i]) {
int c;
c = 0;
while (!visited[i]) {
visited[i] = 1, c++;
i = pp[i];
}
cc[n_++] = c;
while (visited[i] == 1) {
visited[i] = 2;
i = pp[i];
}
}
compare = compare_, sort(cc, 0, n_);
for (i = 0; i < n_ && i < m; i++)
ans += cc[i];
ans += m * 2 - (m > n_ ? (m - n_) % 2 : 0);
printf("%d\n", ans);
return 0;
}
Compilation message
teleporters.c: In function 'main':
teleporters.c:57:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
57 | scanf("%d%d", &n, &m);
| ^~~~~~~~~~~~~~~~~~~~~
teleporters.c:59:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
59 | scanf("%d", &xx[i]);
| ^~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
332 KB |
Output is correct |
2 |
Correct |
7 ms |
588 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
332 KB |
Output is correct |
2 |
Correct |
8 ms |
588 KB |
Output is correct |
3 |
Correct |
20 ms |
1012 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
624 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
716 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
97 ms |
3668 KB |
Output is correct |
2 |
Correct |
303 ms |
8300 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
170 ms |
6064 KB |
Output is correct |
2 |
Correct |
420 ms |
12164 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
567 ms |
17744 KB |
Output is correct |
2 |
Correct |
732 ms |
21292 KB |
Output is correct |
3 |
Correct |
839 ms |
24344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
917 ms |
24132 KB |
Output is correct |
2 |
Execution timed out |
1004 ms |
26404 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1034 ms |
28080 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |