Ural 1303. Minimal Coverage

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

input output
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
Difficulty: 240    Printable version    Submit solution    Discussion (29)
题意:

给一些线段[l,r],求最少的线段能覆盖[0,m]区间

思路:

贪心,先按照起点排序,在取下一条线段时候,在不超过当前终点的条件下,能到达 的终点最远的

wa:看到有负的区间,想着要覆盖的是[0,m]的,就把负的值赋为0 ……却忘了输出要输出原线段    …………

傻了

#include 
   
   
    
    
#include 
    
    
#include 
     
     
      
      
#include 
      
      
       
       
#include 
       
       
         #include 
        
          #include 
         
           #include 
          
            #include 
           
             #include 
            
              #include 
             
               #include 
              
                #include 
               
                 #include 
                
                  #include 
                 
                   #include 
                  
                    #include 
                   
                     #include 
                    
                      #define pi acos(-1.0) #define maxn (101000 + 50) #define mol 1000000009 #define inf 0x3f3f3f3f #define Lowbit(x) (x & (-x)) using namespace std; typedef long long int LLI; struct node { int s; int e; } op[maxn]; bool cmp(node x,node y) { if(x.s != y.s) return x.s < y.s; return x.e > y.e; } queue 
                     
                       Que; int m,n = 0; int main() { // freopen("in.txt","r",stdin); // freopen("out.txt","w",stdout); scanf("%d",&m); while(~scanf("%d%d",&op[n].s,&op[n].e)) { n ++; } sort(op,op + n,cmp); int ends = 0,maxs = 0,len = 0; for(int i = 0; i < n; i ++) { if(op[i].s <= ends) { maxs = op[maxs].e > op[i].e ? maxs : i; } if(op[i].s > ends) { if(op[maxs].e < op[i].s) { ends = 0; break; } Que.push(maxs); ends = op[maxs].e; maxs = op[i].e > op[maxs].e ? i : maxs; len ++; } if(ends >= m) { maxs == -1; break; } } if(maxs != -1) { if(ends < m && op[maxs].e >= m) { ends = op[maxs].e; len ++; Que.push(maxs); } } if(ends < m) printf("No solution\n"); else { printf("%d\n",len); while(!Que.empty()) { printf("%d %d\n",op[Que.front()].s,op[Que.front()].e); Que.pop(); } } return 0; } 
                      
                     
                    
                   
                  
                 
                
               
              
             
            
           
          
         
       
      
      
     
     
   
   

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值