#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,popcnt,lzcnt")
#include <iostream>
#include <vector>
#include <algorithm>
#include <deque>
#include <set>
#include <string>
#include <numeric>
#include <cmath>
#include <cassert>
#include <array>
#include <numeric>
using namespace std;
using ll = long long;
const ll INF = 1e18;
int main()
{
cin.tie(0)->sync_with_stdio(0);
int n, m;
cin >> n >> m;
vector<pair<int, int>> we(n);
vector<pair<int, int>> closest;
for (int i = 0; i < n; ++i)
{
cin >> we[i].first >> we[i].second;
closest.push_back({we[i].first, i * 2});
closest.push_back({we[i].second, i * 2 + 1});
}
sort(closest.begin(), closest.end());
vector<int> adj(2 * n, -1);
for (int i = 0; i < n; ++i)
{
auto it = upper_bound(closest.begin(), closest.end(), pair(we[i].second, (int)1e9));
if (it != closest.end()) adj[2 * i] = it->second;
it = upper_bound(closest.begin(), closest.end(), pair(we[i].first, (int)1e9));
if (it != closest.end()) adj[2 * i + 1] = it->second;
}
int start = closest.front().second;
vector<bool> used(2 * n);
auto dfs = [&adj, &used](auto self, int v) -> int
{
used[v] = true;
if (adj[v] == -1 || used[adj[v]]) return 1;
return self(self, adj[v]) + 1;
};
int res = dfs(dfs, start);
vector<int> comps;
for (int i = 0; i < 2 * n; ++i) if (!used[i]) comps.push_back(dfs(dfs, i));
sort(comps.rbegin(), comps.rend());
for (int i = 0; i < min(m, (int)comps.size()); ++i) res += comps[i] + 2;
m -= (int)comps.size();
m = max(m, 0);
res += 2 * (m - m % 2);
res += m % 2;
cout << res;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
340 KB |
Output is correct |
2 |
Correct |
9 ms |
868 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
468 KB |
Output is correct |
2 |
Correct |
10 ms |
948 KB |
Output is correct |
3 |
Correct |
26 ms |
1604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
724 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
1048 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
109 ms |
4772 KB |
Output is correct |
2 |
Correct |
316 ms |
15060 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
166 ms |
7828 KB |
Output is correct |
2 |
Correct |
390 ms |
21968 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
604 ms |
23636 KB |
Output is correct |
2 |
Correct |
765 ms |
38816 KB |
Output is correct |
3 |
Correct |
860 ms |
45164 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
944 ms |
31412 KB |
Output is correct |
2 |
Execution timed out |
1064 ms |
47900 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1097 ms |
38524 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |