[BOJ 15748] Rest Stops

2023. 1. 25. 23:33Baekjoon

728x90

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

 

15748번: Rest Stops

The first line of input contains four integers: $L$, $N$, $r_F$, and $r_B$. The next $N$ lines describe the rest stops. For each $i$ between $1$ and $N$, the $i+1$-st line contains two integers $x_i$ and $c_i$, describing the position of the $i$-th rest st

www.acmicpc.net

 

- 문제 요약

 

 

 

- 알고리즘 정리

 

Xi와 Ci가 주어질 때, Ci에 초점을 맞춰서 문제를 해결하면 됩니다.

Ci가 큰 순서대로(우선순위 큐 활용) 쉬면서 그리디 하게 케이스를 처리해 나가면 간단하게 풀 수 있습니다.

현재 인덱스는 idx 변수에 넣어놓고 매 반복마다 정류장의 위치와 비교해가며 확인했습니다.

 

 

- 코드 작성

 

#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
struct D{
    int x;
	int c;
};
struct cmp{
    bool operator()(D a,D b){
        return a.c<b.c;
    }
};
ll l,n,f,b,idx,result;
priority_queue<D,vector<D>,cmp>pq;
 
int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
    cin>>l>>n>>f>>b;
    for(int i=0;i<n;i++){
        int x,c;
        cin>>x>>c;
        pq.push({x,c});
    }
    while(!pq.empty()){
        D a=pq.top();
        if(idx<a.x){
            result+=((a.x-idx)*f-(a.x-idx)*b)*a.c;
            idx=a.x;
        }
        pq.pop();
    }
    cout<<result;
}

코드 제출 결과

강조되고 반복되는 컴파일 에러는 PS러를 불안하게 해요....

728x90

'Baekjoon' 카테고리의 다른 글

[BOJ 14454] Secret Cow Code  (0) 2023.01.27
[BOJ 26973] Circular Barn  (0) 2023.01.27
[BOJ 14452] Cow Dance Show  (0) 2023.01.23
[BOJ 14172] Moocast  (0) 2023.01.20
[BOJ 5896] 효율적으로 소 사기  (0) 2023.01.17