栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

poj 1089 Intervals

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

poj 1089 Intervals

#include <iostream>#include <stdio.h>#include <stdlib.h>using namespace std;const int N_MAX = 50000;typedef struct INTERVAL{     int s, e;}INTERVAL_S;INTERVAL_S g_aIntervals[N_MAX]; // 读入区间数// 区间数比较函数int interval_cmp(const void *pOP1, const void *pOP2){    INTERVAL_S *pInterval1, *pInterval2;    pInterval1 = (INTERVAL_S *)pOP1;    pInterval2 = (INTERVAL_S *)pOP2;    if (pInterval1->s < pInterval2->s)  return -1;    if (pInterval1->s == pInterval2->s) return  0;    if (pInterval1->s > pInterval2->s)  return  1;}int main(){    int n;    scanf("%d", &n);    for (int i=0; i<n; i++)    {        scanf("%d %d", &g_aIntervals[i].s, &g_aIntervals[i].e);    }    // 以起点从小到大排序    qsort(g_aIntervals, n, sizeof(g_aIntervals[0]), interval_cmp);    INTERVAL_S cur_interval; // 当前处理区间    // 从前向后线性扫描    int i = 0;    while (i<n)    {        cur_interval = g_aIntervals[i];        // 一个区间的处理        int j = i+1;        while (j<n && g_aIntervals[j].s<=cur_interval.e)        { if (g_aIntervals[j].e > cur_interval.e)     cur_interval.e = g_aIntervals[j].e; ++j;        }        // 打印之        printf("%d %dn", cur_interval.s, cur_interval.e);        i = j;    }    return 0;}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/372794.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号