zoukankan      html  css  js  c++  java
  • (贪心) poj 1716

    Integer Intervals
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 12886   Accepted: 5449

    Description

    An integer interval [a,b], a < b, is a set of all consecutive integers beginning with a and ending with b. 
    Write a program that: finds the minimal number of elements in a set containing at least two different integers from each interval.

    Input

    The first line of the input contains the number of intervals n, 1 <= n <= 10000. Each of the following n lines contains two integers a, b separated by a single space, 0 <= a < b <= 10000. They are the beginning and the end of an interval.

    Output

    Output the minimal number of elements in a set containing at least two different integers from each interval.

    Sample Input

    4
    3 6
    2 4
    0 2
    4 7
    

    Sample Output

    4
    

    Source

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<string>
    #include<cmath>
    #include<cstdlib>
    #include<algorithm>
    #include<queue>
    #include<vector>
    #include<set>
    using namespace std;
    int n;
    struct node
    {
          int x,y;
    }e[10010];
    bool cmp(node a,node b)
    {
          return a.y<b.y;
    }
    int main()
    {
          while(scanf("%d",&n)!=EOF)
          {
                int sum=0;
                for(int i=0;i<n;i++)
                      scanf("%d%d",&e[i].x,&e[i].y);
                sort(e,e+n,cmp);
                int st=e[0].y-1,se=e[0].y;
                for(int i=1;i<n;i++)
                {
                      if(e[i].x<=st)
                            continue;
                      else if(e[i].x<=se)
                      {
                            sum++;
                            st=se;
                            se=e[i].y;
                      }
                      else
                      {
                            sum+=2;
                            st=e[i].y-1;
                            se=e[i].y;
                      }
                }
                printf("%d
    ",sum+2);
          }
          return 0;
    }
    

      

  • 相关阅读:
    JavaScript 简介
    数据库的链接语句
    sqlserver 大杂烩
    数据库操作
    Filter Big Picture
    struts2配置有命名空间的Action: 解决No configuration found for the specified action错误
    Firefox的Firebug调试
    jQuery判断元素显示或隐藏, is 函数
    Jsp Tag
    java revisit
  • 原文地址:https://www.cnblogs.com/a972290869/p/4239128.html
Copyright © 2011-2022 走看看