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;
using ll = long long;
const int N = 1001000;
bool ch[2 * N];
int w[2 * N], c[2 * N];
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int n, m;
cin >> n >> m;
for (int i = 0;i <= 2000000;i++) w[i] = i + 1;
for (int i = 1, l, r;i <= n;i++) {
cin >> l >> r;
w[l] = r + 1, c[l] = 1;
w[r] = l + 1, c[r] = 1;
}
int ans = 0;
vector<int> v;
w[2000000] = 0;
for (int i = 0;i <= 2000000;i++) {
if (!ch[i]) {
int now = i;
int res = 0;
while (!ch[now]) {
ch[now] = 1;
res += c[now];
now = w[now];
}
if (i != 0) v.push_back(res);
else ans += res;
}
}
sort(v.rbegin(), v.rend());
for (int i = 0;i < min(m, (int)v.size());i++) ans += v[i] + 2;
m -= min(m, (int)v.size());
if (m & 1) ans += 2 * m - 1;
else ans += 2 * m;
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... |