zoukankan      html  css  js  c++  java
  • ural 1310 Minimal Coverage

    看题都没看懂,不得不说,中国人的思维和老外还是有蛮大区别的,

    参考思路:http://www.knightzone.org/wordpress/archives/1414

    1303. Minimal Coverage

    Time limit: 1.0 second
    Memory limit: 64 MB
    Given set of line segments [Li, Ri] with integer coordinates of their end points. Your task is to find the minimal subset of the given set which covers segment [0, M] completely (M is a positive integer).

    Input

    First line of the input contains an integer M (1 ≤ M ≤ 5000). Subsequent lines of input contain pairs of integers Li and Ri (−50000 ≤ Li < Ri ≤ 50000). Each pair of coordinates is placed on separate line. Numbers in the pair are separated with space. Last line of input data contains a pair of zeroes. The set contains at least one and at most 99999 segments.

    Output

    Your program should print in the first line of output the power of minimal subset of segments which covers segment [0, M]. The list of segments of covering subset must follow. Format of the list must be the same as described in input with exception that ending pair of zeroes should not be printed. Segments should be printed in increasing order of their left end point coordinate.
    If there is no covering subset then print “No solution” to output.

    Samples

    inputoutput
    1
    -1 0
    -5 -3
    2 5
    0 0
    
    No solution
    
    1
    -1 0
    0 1
    0 0
    
    1
    0 1
    
    Problem Source: II Collegiate Students Urals Programming Contest. Yekaterinburg, April 3-4, 1998
    #include<stdio.h>
    #include<algorithm>
    using namespace std;
    struct node
    {
         int r;
         int l;
    }p[200000],q[200000];
    bool cmp(node a,node b)
    {
         return a.l<b.l;
    }
    int main()
    {
         int m,i,j,x,y,n,flag;
         while(scanf("%d",&m)!=EOF)
         {
              i=0;
              flag=0;
    
              while(scanf("%d %d",&x,&y)!=EOF)
              {
                   if(x==0&&y==0)
                   break;
                   if(x<m&&y>0)//与区间[0,m]没有交集的话就没必要保存了
                   {
                   p[i].l=x;
                   p[i].r=y;
                   i++;
                   }
              }
              n=i;
              int temp=0,right=0,num=0,k;
              sort(p,p+n,cmp);//按区间左界的值排序
              for(i=0;i<n;i++)
              {
                   k=-1;
                   for(;i<n&&p[i].l<=right;i++)//找右界最大的区间
                   {
                        if(p[i].r>temp)//
                        {
                             temp=p[i].r;
                             k=i;
                        }
                   }
                   if(k==-1)//两个区间没有交集跳出
                   {
                        break;
                   }
                   q[num++]=p[k];//保存找到的区间
                   if(temp>=m)//找到,跳出
                   {
                        flag=1;
                     break;
                   }
                   right=temp;//右值要更新
                   i--;//i要回退1
              }
              if(k!=-1&&flag)
              {
                   printf("%d
    ",num);
                   for(i=0;i<num;i++)
                   printf("%d %d
    ",q[i].l,q[i].r);
              }
              else
              printf("No solution
    ");
         }
         return 0;
    }
    View Code
  • 相关阅读:
    最详细的Vue Hello World应用开发步骤
    SAP Fiori + Vue = ?
    golang--连接redis数据库并进行增删查改
    golang--redis基本介绍
    golang--海量用户即时通讯系统
    (四十六)golang--网络编程(简易的聊天系统)
    动态规划--矿工挖矿
    (四十五)golang--反射
    动态规划--爬楼梯问题(入门)
    (四十四)golang--协程(goroutine)和管道(channel)相结合实例
  • 原文地址:https://www.cnblogs.com/llei1573/p/3211800.html
Copyright © 2011-2022 走看看