티스토리 뷰

백준

백준 소스코드 [C++] 10775 공항

Hani_Levenshtein 2021. 3. 10. 17:52

www.acmicpc.net/problem/10775

 

10775번: 공항

예제 1 : [2][?][?][1] 형태로 도킹시킬 수 있다. 3번째 비행기는 도킹시킬 수 없다. 예제 2 : [1][2][3][?] 형태로 도킹 시킬 수 있고, 4번째 비행기는 절대 도킹 시킬 수 없어서 이후 추가적인 도킹은 불

www.acmicpc.net

백준 소스코드 [C++] 10775 공항

#include <iostream>
#include <algorithm>
#include <queue>
#include <string.h>
#include <limits.h>
#include <vector>
#include <math.h>
#include <stack>
#include <bitset>
#include <string>
#include <set>
#include <map>
#include <unordered_map>
#include <sstream>
#define all(v) v.begin(), v.end()
#define pii pair<int, int>
#define make_unique(v) v.erase(unique(v.begin(), v.end()), v.end())
typedef long long ll;
using namespace std;

int gates[100001];
int getGate(int x) {
	if (x == gates[x])return x;
	return gates[x] = getGate(gates[x]);
}

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int n, m,k;
	bool flag = false;
	int res = 0;
	cin >> n;
	cin >> m;
	for (int i = 0;i <= n;i++) gates[i] = i;
	for (int i = 0;i < m;i++) {
		cin >> k;
		if (flag == true) continue;
		int resGate = getGate(k);
		if (resGate == 0) flag = true;
		else {
			gates[resGate] = resGate - 1;
			res++;
		}
	}
	cout << res;
	return 0;
}
댓글