티스토리 뷰

그리디 알고리즘으로

그냥 그대로 앞부터 덮어가면 된다

벡터로 구현하다가 구현 오류가 많이나서 틀렸다

변수를 더 잡더라도 정확하게 구현을 하자!

마지막으로 덮은 곳을 last에 두고 last와 다음 웅덩이의 시작지점을 비교했다

만약 last가 더 크다면 웅덩이의 시작지점을 last+1로 바꾼다

 

소스코드

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
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int n,l,ans;
vector<pair<int,int>> vt;
bool cmp (const pair<int,int> &a, const pair<int,int>&b){
    if(a.first==b.first) return a.second>b.second;
    return a.first<b.first;
}
int main(){
    //freopen("input.txt","r",stdin);
    scanf(" %d %d",&n,&l);
    for(int i=0;i<n;i++scanf(" %d %d",&vt[i].first,&vt[i].second);
    sort(vt.begin(),vt.end(),cmp);
    int last=-1;
    for(int i=0;i<n;i++){
        if(vt[i].second-1<=last) continue;
        if(last >= vt[i].first) vt[i].first = last+1;
        int dist = vt[i].second-vt[i].first;
        int cnt = dist/l;
        if(dist%l != 0 ) cnt++;
        ans+=cnt;
        last = vt[i].first+(cnt*l)-1;
    }
    printf("%d\n",ans);
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
 

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

[백준] 5052 전화번호 목록  (0) 2019.09.03
[백준] 2751 수 정렬하기 2  (0) 2019.09.02
[백준] 1781 컵라면  (0) 2019.08.27
[백준] 15903 카드 합체 놀이  (0) 2019.08.27
[백준] 9370 미확인 도착지  (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
글 보관함