#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:56:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
56 | scanf("%d%d", &n, &m);
| ^~~~~~~~~~~~~~~~~~~~~
teleporters.c:58:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
58 | 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 |
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 |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 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 |
296 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
288 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
332 KB |
Output is correct |
2 |
Correct |
9 ms |
692 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
460 KB |
Output is correct |
2 |
Correct |
8 ms |
716 KB |
Output is correct |
3 |
Correct |
20 ms |
1284 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
776 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
868 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
103 ms |
5312 KB |
Output is correct |
2 |
Correct |
263 ms |
12596 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
169 ms |
8944 KB |
Output is correct |
2 |
Correct |
383 ms |
18756 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
559 ms |
26876 KB |
Output is correct |
2 |
Correct |
695 ms |
32168 KB |
Output is correct |
3 |
Correct |
760 ms |
36036 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
886 ms |
36800 KB |
Output is correct |
2 |
Correct |
959 ms |
40256 KB |
Output is correct |
3 |
Correct |
906 ms |
39736 KB |
Output is correct |
4 |
Correct |
978 ms |
40272 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1090 ms |
42108 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |