This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#ifdef tabr
#include "library/debug.cpp"
#else
#define debug(...)
#endif
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
vector<pair<int, int>> a(n);
vector<int> b(2 * n);
for (int i = 0; i < n; i++) {
cin >> a[i].first >> a[i].second;
b[2 * i] = a[i].first;
b[2 * i + 1] = a[i].second;
}
sort(a.begin(), a.end());
sort(b.begin(), b.end());
for (int i = 0; i < n; i++) {
a[i].first = (int) (lower_bound(b.begin(), b.end(), a[i].first) - b.begin());
a[i].second = (int) (lower_bound(b.begin(), b.end(), a[i].second) - b.begin());
}
vector<int> c(2 * n);
for (int i = 0; i < n; i++) {
c[a[i].first] = a[i].second;
c[a[i].second] = a[i].first;
}
vector<int> was(2 * n);
vector<int> d;
int ans = 0;
for (int z = 0; z < 2 * n; z++) {
if (was[z]) {
continue;
}
int now = z;
int sz = 0;
do {
sz++;
was[now] = 1;
now = c[now] + 1;
} while (now < 2 * n && now != z);
if (z != 0) {
d.emplace_back(sz);
} else {
ans = sz;
}
}
sort(d.rbegin(), d.rend());
int t = 0;
for (int i = 0; i < m; i++) {
if (i < (int) d.size()) {
ans += d[i] + 2;
} else if (t) {
ans += 3;
t ^= 1;
} else {
ans++;
t ^= 1;
}
}
cout << ans << '\n';
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |