# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
915873 | Matjaz | Teleporters (IOI08_teleporters) | C++14 | 991 ms | 65536 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//
// IOI2008Teleporters.cpp
//
//
// Created by Matjaz Leonardis on 24/01/2024.
//
#include <iostream>
#include <vector>
#include <algorithm>
#include <assert.h>
using namespace std;
int N,M;
int main(){
cin >> N >> M;
vector<pair<int,int> > X;
vector<int> next(4*N, -1);
for (int i=0;i<N;i++){
int W,E;
cin >> W >> E;
X.push_back(make_pair(W, 4 * i));
X.push_back(make_pair(W, 4 * i + 1));
X.push_back(make_pair(E, 4 * i + 2));
X.push_back(make_pair(E, 4 * i + 3));
next[4 * i] = 4 * i + 3;
next[4 * i + 2] = 4 * i + 1;
}
sort(X.begin(), X.end());
for (int i=0;i<X.size() - 1; i++){
//cout << i << " " << X[i].first << " " << X[i].second << endl;
if (next[X[i].second] == -1) next[X[i].second] = X[i + 1].second;
}
int jumps = 0;
vector<int> cycle;
vector<bool> visited(4 * N, false);
for (int i=0;i<X.size();i++){
//cout << next[X[i].second] << endl;
//cout << i << " " << X[i].first << " " << X[i].second << endl;
if (visited[X[i].second]) continue;
int current = X[i].second;
int l = 0;
visited[X[i].second] = true;
//cout << current << " ";
while (next[current] != X[i].second && next[current] != -1){
l++;
current = next[current];
visited[current] = true;
//cout << current << " ";
}
//cout << endl;
assert(l % 2 == 1);
//cout << "l:" << l << endl;
if (i == 0){
jumps += (l + 1) / 2;
} else cycle.push_back(l);
}
sort(cycle.rbegin(), cycle.rend());
for (int i=0;i<M;i++){
if (i >= cycle.size()) break; else
jumps += (cycle[i] + 1) / 2 + 2;
//if (i < cycle.size()) cout << cycle[i] << endl;
}
if (M > cycle.size()){
M -= cycle.size();
if (M % 2 == 0) jumps += 2 * M;
if (M % 2 == 1) jumps += 2 * M - 1;
}
cout << jumps << endl;
return 0;
}
Compilation message (stderr)
# | 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... |