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 <iostream>
#include <vector>
#include <algorithm>
typedef long long ll;
using namespace std;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
vector<pair<int, int> > v(2 * n + 2);
v[0] = { 0, 0 }, v[1] = { 2e6 + 5, 1 };
for (int i = 1; i <= n; i++)
{
cin >> v[2 * i].first >> v[2 * i + 1].first;
v[2 * i].second = 2 * i, v[2 * i + 1].second = 2 * i + 1;
}
sort(v.begin(), v.end());
int last = v.back().second;
int k = v.size();
vector<int> nx(k, -1);
for (int i = k - 1; i >= 0; i--)
{
nx[v[i].second] = (last < 2 ? last : last ^ 1);
last = v[i].second;
}
vector<bool> vis(k, false);
int path = -1; vector<int> c;
for (int i = 0; i < k; i++) if (!vis[i])
{
int j = i, len = 0;
while (!vis[j])
{
vis[j] = true;
len++, j = nx[j];
}
if (!i) path = len-2;
else c.push_back(len);
}
sort(c.begin(), c.end());
while (m && c.size())
{
path += c.back() + 2;
c.pop_back(); m--;
}
path += ((m >> 1) << 2) + (m & 1);
cout << path << "\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... |