이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define eb emplace_back
#define sz(V) ((int)(V).size())
#define allv(V) ((V).begin()),((V).end())
#define sorv(V) sort(allv(V))
#define upmax(a,b) (a)=max((a),(b))
#define upmin(a,b) (a)=min((a),(b))
using namespace std;
typedef long long ll;
const int MAXN = 1000005;
const int MAXX = 2000005;
priority_queue<int> PQ;
vector<int> VX;
int C[MAXX];
int LI[MAXX], RI[MAXX];
int A[MAXN], B[MAXN];
bitset<MAXX> chk;
int N, M, Ans;
void f(vector<int> &V, int i) {
chk[i] = true;
V.eb(i); i = C[i];
if(!i || chk[i]) return;
f(V, i);
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> N >> M;
for(int i = 1; i <= N; i++)
cin >> A[i] >> B[i];
VX.eb(0); VX.eb(MAXX-1);
for(int i = 1; i <= N; i++) {
VX.eb(A[i]); VX.eb(B[i]);
}
sorv(VX);
LI[0] = 0; RI[MAXX-1] = sz(VX)-1;
for(int i = 1; i + 1 < sz(VX); i++) {
RI[VX[i]] = i;
LI[VX[i]] = i+1;
}
for(int i = 1; i <= N; i++) {
C[RI[A[i]]] = LI[B[i]];
C[RI[B[i]]] = LI[A[i]];
}
{
vector<int> V; f(V, 1);
Ans = sz(V)-1;
}
for(int i = 1, n = sz(VX); i < n; i++) {
if(chk[i]) continue;
vector<int> V;
f(V, i);
PQ.push(sz(V));
}
for(; M--;) {
if(!PQ.empty()) {
Ans += PQ.top() + 2;
PQ.pop();
}
else {
Ans++;
PQ.push(1);
}
}
cout << Ans << endl;
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... |