티스토리 뷰
문제
알고리즘
priority queue
풀이
소들의 도착시간과 풀을 먹는 시간이 주어질 때, 가장 오래 기다리는 소의 시간을 출력하는 문제입니다. 한 소가 풀을 먹을 때 다른 소들은 기다려야하며, 대기하는 소들이 여럿일 때는 연장자부터 풀을 먹습니다.
우선순위 큐를 사용하는 문제입니다. info 구조체를 만들어 풀도록 합시다. 각각 나이, 도착시간, 풀을 먹는 시간에 대한 정보를 담고 있습니다. 구조체를 우선순위 큐, lower bound에서 사용하기 위해 각각 비교함수들을 작성했습니다. 우선순위 큐에 있는 소들을 처리할 때, 또다른 소가 도착할 수 있음을 유의합시다. 마지막 소까지 시뮬레이션이 끝나고 큐를 모두 비워내기 위해 for문의 범위를 i<N 에서 i≤N으로 수정하였습니다.
코드
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define REP(i, n) for (int i = 1; i <= n; ++i)
using namespace std;
const int MAXN = 1e5;
struct info {
int l, s, t;
};
struct cmp {
bool operator()(info a, info b) {
return a.l > b.l;
}
};
bool operator<(info const a, info const b) {
if (a.s == b.s)
return a.l < b.l;
return a.s < b.s;
};
priority_queue<info, vector<info>, cmp> pq;
int N;
info cow[MAXN];
info k;
int main() {
#ifndef ONLINE_JUDGE
freopen("in", "r", stdin);
freopen("out", "w", stdout);
#endif
cin.tie(NULL);
cout.tie(NULL);
ios::sync_with_stdio(false);
cin >> N;
rep(i, N) {
cow[i].l = i;
cin >> cow[i].s >> cow[i].t;
}
sort(cow, cow + N);
int ans = -1;
for (int i = 0; i <= N;) {
if (pq.empty()) {
k.s = cow[i].s + cow[i].t;
i += 1;
} else {
auto [L, S, T] = pq.top();
pq.pop();
ans = max(ans, k.s - S);
k.s += T;
}
int nxt = lower_bound(cow + i, cow + N, k) - cow;
for (; i < nxt; ++i) {
pq.emplace(cow[i]);
}
}
cout << ans;
return 0;
}
'Algorithm' 카테고리의 다른 글
[백준 17037] Painting the Barn (Silver) (0) | 2020.12.05 |
---|---|
[백준 16768] Mooyo Mooyo (0) | 2020.11.22 |
[백준 18267] Milk Visits (0) | 2020.11.17 |
[백준 13510] 트리와 쿼리1 (0) | 2020.11.16 |
[백준 18784] Triangles (Silver) (0) | 2020.11.15 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 트라이
- 세그먼트트리
- kmp
- DP
- bfs
- sweeping
- union find
- Oracle
- 이분매칭
- Fenwick
- 이분탐색
- hld
- 정렬
- dfs
- string
- SCC
- Suffix Array
- implementation
- 2-SAT
- 동적계획법
- knapsack
- spring boot
- dijkstra
- spring
- greedy
- sorting
- Segment tree
- 펜윅트리
- 스위핑
- 좌표압축
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함