티스토리 뷰

알고리즘/BOJ

[백준] 1781 컵라면

세진짱 2019. 8. 27. 22:46

그리디문제인제 복잡하게 생각해서 많이 틀렸다

나의 틀린 접근법은 각 날 마다 최대값을 두고 나머지 날 들은 pq 에 넣었다

그리고 각날의 최대값과 나머지날들을 비교해서 큰값을 넣었다

많은 것들을 고려했었는데 그럴 필요가 없었다

데드라인 순으로 정렬하고 내가 보고있는 데드라인까지 중 큰 값들만 남기고 버리면 된다

pq를 이용하면 쉽게 풀 수 있다

 

소스코드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
pair<int,int> arr[200200];
int n;
 
int main(){
    //freopen("input.txt","r",stdin);
    scanf(" %d",&n);
    for(int i=0;i<n;i++scanf(" %d %d",&arr[i].first,&arr[i].second);
    sort(arr,arr+n);
    priority_queue<int> pq;
    for(int i=0;i<n;i++){
        int d = arr[i].first;
        pq.push(-arr[i].second);
        while(d<pq.size()) pq.pop();
    }
    int ans=0;
    while(!pq.empty()) ans+=pq.top(),pq.pop();
    printf("%d\n",-ans);
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
 

'알고리즘 > BOJ' 카테고리의 다른 글

[백준] 2751 수 정렬하기 2  (0) 2019.09.02
[백준] 1911 흙길 보수하기  (0) 2019.08.27
[백준] 15903 카드 합체 놀이  (0) 2019.08.27
[백준] 9370 미확인 도착지  (0) 2019.08.27
[백준] 5719 거의 최단 경로  (0) 2019.08.27
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
TAG
more
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함