#include <bits/stdc++.h>
using namespace std;
const int dydis = 2e6 + 10;
int nxt[dydis];
int desn[dydis];
int n, k;
vector<pair<int, int> > mas;
vector<pair<int, int> > vec;
bool vis[dydis] = {};
int main(){
cin >> n >> k;
for(int i = 0; i < n; i++){
int a, b; cin >> a >> b;
vec.push_back({a, i*2});
vec.push_back({b, i*2+1});
}
sort(vec.begin(), vec.end());
for(int i = 0; i < (int)vec.size()-1; i++){
//cout << ((char)('a'+(vec[i].second/2))) << " ";
desn[vec[i].second] = vec[i+1].second;
}
//cout << endl;
desn[vec.back().second] = 2*n;
for(int i = 0; i < 2*n; i++){
nxt[i] = desn[(i ^ 1)];
// cout << "nxt[" << i << "] = " << nxt[i] << endl;
}
int start = vec[0].second;
int cur = start;
int curAns = 0;
int jauYra = 0;
while(cur != 2*n){
vis[cur] = 1;
cur = nxt[cur];
curAns++;
jauYra++;
}
vector<int> ciklai;
for(int i = 0; i < 2*n; i++){
if(vis[i]) continue;
int cur = i;
int kek = 0;
while(true){
kek++;
vis[cur] = 1;
cur = nxt[cur];
if(cur == i) break;
}
ciklai.push_back(kek);
//cout << "dedu " << kek << endl;
}
sort(ciklai.begin(), ciklai.end()); reverse(ciklai.begin(), ciklai.end());
for(int i = 0; i < min((int)ciklai.size(), k); i++){
curAns += ciklai[i] + 2;
jauYra += ciklai[i];
}
k -= min((int)ciklai.size(), k);
if(k != 0){
//cout << "k != 0" << endl;
curAns += ((k/2) * 2) * 2;
curAns += (k & 1);
}
cout << curAns;
return 0;
}
# |
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 |
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 |
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 |
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 |
1 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 |
2 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
460 KB |
Output is correct |
2 |
Correct |
11 ms |
712 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
460 KB |
Output is correct |
2 |
Correct |
12 ms |
912 KB |
Output is correct |
3 |
Correct |
29 ms |
1672 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
840 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
940 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
120 ms |
4984 KB |
Output is correct |
2 |
Correct |
306 ms |
11312 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
227 ms |
8236 KB |
Output is correct |
2 |
Correct |
441 ms |
16220 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
653 ms |
24764 KB |
Output is correct |
2 |
Correct |
798 ms |
29320 KB |
Output is correct |
3 |
Correct |
877 ms |
35016 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
948 ms |
33024 KB |
Output is correct |
2 |
Execution timed out |
1012 ms |
49728 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1096 ms |
40140 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |