POJ 1089 Intervals
Posted mostiray
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ 1089 Intervals相关的知识,希望对你有一定的参考价值。
简单的贪心,POJ不能用C11,硬是把C11的特性改回来了
代码
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
const int INF = 1e9;
struct node {
int s, e;
node() : s(0), e(0) {}
node(int _s, int _e) : s(_s), e(_e) {}
};
bool cmp(const node &a, const node &b) { return a.s < b.s; };
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
vector<node> record(n);
for (int i = 0; i < n; ++i) {
cin >> record[i].s >> record[i].e;
}
sort(record.begin(), record.end(), cmp);
int lastStart = record[0].s, lastEnd = record[0].e;
vector<node> ans;
for (int i = 1; i < n; ++i) {
if (record[i].s <= lastEnd) {
if (lastEnd < record[i].e) {
lastEnd = record[i].e;
}
} else {
ans.push_back(node(lastStart, lastEnd));
lastStart = record[i].s;
lastEnd = record[i].e;
}
if (i == n - 1) ans.push_back(node(lastStart, lastEnd));
}
for (int i = 0; i < ans.size(); ++i) {
cout << ans[i].s << ‘ ‘ << ans[i].e << ‘
‘;
}
return 0;
}
以上是关于POJ 1089 Intervals的主要内容,如果未能解决你的问题,请参考以下文章