• 2015 HUAS Summer Trainning #4 D


    Given several segments of line (int the X axis) with coordinates [Li, Ri].

    You are to choose the minimalamount of them, such they would completely cover the segment [0, M].

    Input
    The first line is the number of test cases, followed by a blank line.
    Each test case in the input should contains an integer M (1 ≤ M ≤ 5000), followed by pairs “Li Ri”
    (|Li|, |Ri| ≤ 50000, i ≤ 100000), each on a separate line. Each test case of input is terminated by pair‘0 0’.
    Each test case will be separated by a single line.

    Output
    For each test case, in the first line of output your programm should print the minimal number of line
    segments which can cover segment [0, M]. In the following lines, the coordinates of segments, sorted
    by their left end (Li), should be printed in the same format as in the input. Pair ‘0 0’ should not be
    printed. If [0, M] can not be covered by given line segments, your programm should print ‘0’ (withoutquotes).
    Print a blank line between the outputs for two consecutive test cases.
    Sample Input
    2
    1
    -1 0
    -5 -3
    2 5
    0 0
    1
    -1 0
    0 1
    0 0
    Sample Output

    0
    1
    0 1

    题目大意:找到能覆盖指定区间的,最少区间数及他们的区间范围。

    解题思路:从左端点小于0的开始找,右端点找最大的,在判断这个右端点是否大于等于M,

    否的话,更新左端点,再找右端点最大的,一直这么循环找,知道找到右端点大于等于M,输出。

    注意数据范围(int会溢出,要使用long long)

    代码:

     1 #include<iostream>
     2 #include<algorithm>
     3 using namespace std;
     4 const int maxn=100000+10;
     5 struct node
     6 {
     7     int x,y;
     8 }a[maxn],b[maxn];
     9 int cmp(node a,node b)
    10 {
    11     return a.x<b.x;
    12 }
    13 int main()
    14 {
    15     int t,m,sum,x,y,l,pmax,p,max;
    16     int i;
    17     cin>>t;
    18     while(t--)
    19     {
    20         i=0;
    21         cin>>m;
    22         while(cin>>x>>y)
    23         {
    24             if(x==0&&y==0)
    25                 break;
    26             if(y>=0)
    27             {
    28             a[i].x=x;
    29             a[i].y=y;
    30             ++i;
    31             }
    32         }
    33         sort(a,a+i,cmp);
    34         if(a[0].x>0)
    35             cout<<"0"<<endl;
    36         else 
    37         {
    38             p=0,max=0,sum=0;
    39             while(p+1<i && a[p+1].x<=0)
    40             {
    41                 ++p;
    42                 if(a[p].y>a[max].y)
    43                     max=p;
    44             }
    45             p=max,b[0].x=a[p].x,b[0].y=a[p].y;
    46             while(p<i && b[sum].y<m)
    47             {
    48                 l=1,pmax=max;
    49                 while(p+1<i && a[p+1].x<=a[max].y)
    50                 {    
    51                     l=0;
    52                     ++p;
    53                     if(a[p].y>a[pmax].y)
    54                         pmax=p;
    55                 }
    56                 if(l)  break;
    57                 max=pmax;
    58                 ++sum;
    59                 b[sum].x=a[max].x;
    60                 b[sum].y=a[max].y;
    61             }
    62             if(b[sum].y>=m)
    63             {
    64                 cout<<sum+1<<endl;
    65                 for(int j=0;j<=sum;j++)
    66                     cout<<b[j].x<<" "<<b[j].y<<endl;
    67             }
    68             else cout<<"0"<<endl;
    69         }
    70         if(t)
    71             cout<<endl;
    72     }
    73     return 0;
    74 }
    View Code
  • 相关阅读:
    基于jQuery仿淘宝产品图片放大镜代码
    【家育通】 关于我们
    新房装修三大空鼓解决方法 为家居装修做好前奏
    MVC5+EF6 入门完整教程十一:细说MVC中仓储模式的应用
    MVC5+EF6 入门完整教程十
    MVC5+EF6 入门完整教程九
    MVC5+EF6 入门完整教程八
    MVC5+EF6 入门完整教程七
    MVC5+EF6 入门完整教程六
    MVC5+EF6 入门完整教程五
  • 原文地址:https://www.cnblogs.com/huaxiangdehenji/p/4716314.html
Copyright © 2020-2023  润新知