码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 1089 Intervals

时间:2020-07-03 00:43:01      阅读:63      评论:0      收藏:0      [点我收藏+]

标签:val   include   inf   using   org   algo   sts   bool   turn   

题目地址

简单的贪心,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 << ‘\n‘;
    }
    return 0;
}

POJ 1089 Intervals

标签:val   include   inf   using   org   algo   sts   bool   turn   

原文地址:https://www.cnblogs.com/mostiray/p/13227949.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!