HDOJ 2037 구간 욕심+우선 순위
6185 단어 탐욕책기계 시험 지침의 길
문제풀이 사고방식: 구간 욕심, 욕심 전략은 매번 종료 시간이 가장 빠른 구간을 선택하고 우선 대기열을 사용하여 종료 시간의 전후에 따라 각 구간을 저장한다.
AC 코드:
#include
#include
#include
#include
#include
using namespace std;
struct node {
int l, r;
bool operator < (const node &a) const {
return r > a.r;//
}
}list[101];
priority_queue<node> q;
int main() {
int n;
while (cin >> n) {
if (n == 0)break;
//while (!q.empty())q.pop();
for (int i = 1; i <= n; i++) {
cin >> list[i].l >> list[i].r;
q.push(list[i]);
}
int ans = 0; int now = 0;
while (!q.empty()) {
node tmp = q.top(); q.pop();
if (tmp.l < now) { continue; }
ans++;
now = tmp.r;
}
cout << ans << endl;
}
return 0;
}