홍시홍의 프로그래밍

[백준 10775] 공항 본문

알고리즘 문제풀이/백준

[백준 10775] 공항

홍시홍 2020. 1. 9. 21:23

 

https://www.acmicpc.net/problem/10775

 

10775번: 공항

문제 오늘은 신승원의 생일이다. 박승원은 생일을 맞아 신승원에게 인천국제공항을 선물로 줬다. 공항에는 G개의 게이트가 있으며 각각은 1에서 G까지의 번호를 가지고 있다. 공항에는 P개의 비행기가 순서대로 도착할 예정이며, 당신은 i번째 비행기를 1번부터 gi (1 ≤ gi ≤ G) 번째 게이트중 하나에 영구적으로 도킹하려 한다. 비행기가 도킹된 게이트에는 다른 비행기가 도착할 수 없다. 이렇게 공항을 운영할 경우 간혹 비행기가 어떤 곳에도 도킹하지 못하는

www.acmicpc.net

1/6일 풀었던 문제

요구사항

1. 최대로 주차(?)할 수 있는 비행기 수 구하기

 

풀이

1. n번째가 차지하고 있으면 n-1번째가 비어있는가 검사한다.

Union Find자료구조를 이용한다.

#include <stdio.h>
#include <algorithm>
#include <list>
#include <iostream>

using namespace std;
 
struct NODE {
    int prev;
    int next;
    int val;
};
const int PN=23;
const int SIZE=100000;
int table[100001];

int parent[100001];
int Find(int x)
{
    if(parent[x]==x) return x;
    return parent[x]=Find(parent[x]);
}
void Union(int x, int y)
{
    x = Find(x);
    y = Find(y);
    if(x !=y){
        if(x<y){
            parent[y]=x;
        }
        else{
            parent[x]=y;
        }
    }
}

int main()
{
    int g,p;
    cin>>g>>p;
    for(int i=1 ; i <=g ; i++)
        parent[i]=i;
    int flag=0;
       int ans=0;
    for(int i=0 ; i < p ; i++){
        int x;
        scanf("%d",&x);
        if(flag==1)
        continue;
        int gate=Find(x);
    //    cout<<"gate "<<gate<<endl;

    
        if(gate==0){
            flag=1;
      //      cout<<"parent "<< parent[x]<<endl;
            //continue;
        }
        else{
            ans++;
            Union(gate,gate-1);
        //    cout<<"Union "<< parent[x]<<endl;
        }        
    }

    cout<<ans<<endl;
}

'알고리즘 문제풀이 > 백준' 카테고리의 다른 글

[백준 2887] 행성 터널  (0) 2020.01.09
[백준 9938] 방 청소  (0) 2020.01.09
[백준 10815] 숫자 카드  (0) 2020.01.09
[백준 2805] 나무 자르기  (0) 2020.01.08
[백준 1920] 수 찾기 (해시, 정렬)  (2) 2020.01.08
Comments