백준(C, C++)/실버

백준 23253: 자료구조는 정말 최고야

치킨먹고싶어요 2022. 7. 9. 14:23

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

 

23253번: 자료구조는 정말 최고야

위 그림처럼 책이 쌓여 있으므로, 첫 번째 더미 - 두 번째 더미 - 첫 번째 더미 - 두 번째 더미 순으로 꺼내면 책 번호순으로 나열할 수 있다.

www.acmicpc.net

 

 
#include <iostream>
#include <utility>
#include <queue>
using namespace std;
int m, n, k, t;
int lis[200000];
priority_queue<pair<intpair<intint>>> pq;
int main() {
    scanf("%d %d"&n, &m);
    for (int i = 0; i < m; i++) {
        scanf("%d"&k);
        for (int j = k - 1; j >= 0; j--) {
            scanf("%d"&t); pq.push(make_pair(-t, make_pair(i, j)));
        }
    }
    while (!pq.empty()) {
        pair<intpair<intint>> tmp = pq.top(); pq.pop();
        if (lis[tmp.second.first]++ != tmp.second.second) {
            printf("No");
            return 0;
        }
    }
    printf("Yes");
    return 0;
}
 
cs

 


사용된 알고리즘

에드 혹, 우선순위 큐


풀이

 

#include <iostream>
#include <utility>
#include <queue>
using namespace std;
int m, n, k, t;
int lis[200000];
priority_queue<pair<intpair<intint>>> pq;
int main() {
    scanf("%d %d"&n, &m);
    for (int i = 0; i < m; i++) {
        scanf("%d"&k);
        for (int j = k - 1; j >= 0; j--) { // 우선순위 큐로 책을 순서대로 넣어줍니다.
            scanf("%d"&t); 
pq.push(make_pair(-t, make_pair(i, j))); // 책이 어느 더미에 몇 번째 순서인지 표시합니다.
        }
    }
    while (!pq.empty()) {
        pair<intpair<intint>> tmp = pq.top(); pq.pop();
        if (lis[tmp.second.first]++ != tmp.second.second) { // 각 더미의 맨 위에 있는 지 확인합니다.
            printf("No"); // 맨 위에 있지 않다면 No를 출력하고 프로그램을 종료합니다.
            return 0;
        }
    }
    printf("Yes");
    return 0;
}
 
cs
 
 

'백준(C, C++) > 실버' 카테고리의 다른 글

백준 1052: 물병 [C/C++]  (0) 2022.07.11
백준 1966: 프린터 큐 [C/C++]  (0) 2022.06.11
백준 1158: 요세푸스 문제 [C언어]  (0) 2022.06.03
백준 1037 약수, c++  (0) 2022.06.02
백준 11659 구간 합 구하기4  (0) 2022.06.02