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;
typedef long long ll;
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int n,m;
cin >> n >> m;
vector<array<int,2>> v(2*n);
vector<int> p(2*n,-1);
for(int i=0;i<n;i++)
{
int l,r;
cin >> l >> r;
v[2*i]={l,2*i};
v[2*i+1]={r,2*i+1};
}
sort(v.begin(),v.end());
for(int i=0;i+1<2*n;i++) p[v[i][1]^1]=v[i+1][1];
vector<bool> vis(2*n,0);
vector<int> opt;
int res=0;
for(int i=0;i<2*n;i++)
{
int a=v[i][1];
if(vis[a]) continue;
int len=0;
while(a!=-1&&!vis[a])
{
len++;
vis[a]=1;
a=p[a];
}
if(a!=-1) opt.push_back(len);
else res+=len;
}
sort(opt.begin(),opt.end(),greater<>());
for(int len:opt)
{
if(m>0)
{
res+=(len+2);
m--;
}
}
res+=(4*(m/2));
if(m&1) res++;
cout << res << "\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... |