zoukankan      html  css  js  c++  java
  • 【区间覆盖问题】uva 10020

    可以说是区间覆盖问题的例题...

    Note:

    区间包含+排序扫描;

      要求覆盖区间[s, t];

      1、把各区间按照Left从小到大排序,如果区间1的起点大于s,则无解(因为其他区间的左起点更大);否则选择起点在s的最长区间;

      2、选择区间[li, ri]后,新的起点应更新为ri,并且忽略所有区间在ri之前的部分;

     Minimal coverage 

    The Problem

    Given several segments of line (int the X axis) with coordinates [Li,Ri]. You are to choose the minimal amount of them, such they would completely cover the segment [0,M].

    The 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.

    The 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"(without quotes).

    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
    

    代码如下
     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstdlib>
     4 #include<cstring>
     5 #include<algorithm>
     6 using namespace std;
     7 const int maxn = 100010;
     8 struct Seg
     9 {
    10     int L, R;
    11     bool operator < (const Seg &a) const
    12     {
    13         if(L != a.L) return L < a.L;
    14         else return R > a.R;
    15     }
    16 } seg[maxn], ans[maxn];
    17 void solve(int n, int m)
    18 {
    19     int cnt = 0, l = 0, r = 0;//起点l,起点l最长区间的右端点r
    20     if(seg[0].L > l)
    21     {
    22         printf("0
    ");
    23         return ;
    24     }//无解
    25     bool ok = false;
    26     while(1)
    27     {
    28         if(l >= m)    break;
    29         int i, pos = 0;
    30         ok = false;//判断变量ok,重要!
    31         for(i = 0; i < n; i++)
    32         {
    33             if(seg[i].L <= l)
    34             {
    35                 if(seg[i].R > r)
    36                 {
    37                     pos = i;
    38                     r = seg[pos].R;
    39                     ok = true;
    40                 }//寻找起点在l的最长区间
    41             }
    42         }
    43         if(ok)
    44         {
    45             l = r;
    46             ans[cnt].L = seg[pos].L;
    47             ans[cnt++].R = seg[pos].R;
    48         }//更新起点l
    49         else break;
    50     }
    51     if(ok)
    52     {
    53         printf("%d
    ", cnt);
    54         for(int i = 0; i < cnt; i++)
    55             printf("%d %d
    ", ans[i].L, ans[i].R);
    56     }
    57     else printf("0
    ");
    58 }
    59 int main()
    60 {
    61     int T;
    62     scanf("%d", &T);
    63     for(int kase = 0; kase < T; kase++)
    64     {
    65         if(kase) printf("
    ");
    66         memset(seg, 0, sizeof(seg));
    67         memset(ans, 0, sizeof(ans));
    68         int m; scanf("%d", &m);
    69         int i = 0, L, R;
    70         while(scanf("%d%d", &L, &R))
    71         {
    72             if(!L && !R)  break;
    73             seg[i].L = L; seg[i].R = R;
    74             i++;
    75         }
    76         sort(seg, seg+i);//排序
    77         solve(i, m);
    78     }
    79     return 0;
    80 }
    View Code
  • 相关阅读:
    Python脚本文件(.py)打包为可执行文件(.exe)即避免命令行中包含Python解释器
    MVC 三步完成增删改查设计
    MVC中使用SqlServerCe
    回车转Tab
    动态代码
    Mvc 用户没有登录跳转到登录界面
    Mvc
    Mvc提交
    EF查询 linq
    EF数据迁移 Migrations
  • 原文地址:https://www.cnblogs.com/LLGemini/p/4271688.html
Copyright © 2011-2022 走看看