Three kinds of interval coverage problem based on greedy thought

Source: Internet
Author: User

First, the interval complete coverage problem

Problem Description: Given an interval of length m, give the start and end point of the N line segment (Note that this is a closed interval), and the minimum number of segments can be used to completely cover the entire range.

Example: an interval of length 8, with an optional segment of [2,6],[1,4],[3,6],[3,7],[6,8],[2,4],[3,5].

Solution process:

1, each line segment in ascending order of the left endpoint, if the left endpoint is the same, according to the right end point ascending order, after the order is [1,4],[2,4],[2,6],[3,5],[3,6],[3,7],[6,8];

2, set a variable to indicate the area of the right end of the coverage, in the remaining segments to find all the left endpoint is less than or equal to the current coverage to the right end of the segment, select the right end of the maximum and greater than the current coverage to the right end point, repeat the above operation until the entire interval;

3, the simulation process: Assuming the first time to join [1,4], then the next can select the segment has [2,6],[3,5],[3,6],[3,7], because 3 is less than 4 and 7 is the largest, so the next selection [3,7] to overwrite, the last time can only select [6,8], This time just covers a length of 8 of the interval-->break; that the selected 3 segments can cover a large interval of 8;

4. Greedy Proof:

Requires a minimum of the line segment to cover, then the selected segment must be as long as possible, just cover the area before the place has not been considered, it can be understood that all can be covered by the left end has been overwritten, then can make the segment longer depends on the right end point, the left endpoint is not much meaning, so select the right end to overwrite.

Paper: Nyoj #12 Water Spraying Device (ii) description

There is a lawn, transverse long w, longitudinal length is H, in its horizontal center line at different locations with N (n<=10000) point-like water spray device, each water spray device I spray the effect is that it is the center radius of the RI is wetting. Please select as few water spray devices as possible in the given sprinkler system to moisten the entire lawn.

Input

The first line enters a positive integer n to indicate a total of n test data.
The first line of each set of test data has three integers n,w,h,n indicates a total of n water spray devices, W represents the transverse length of the lawn, and H indicates the longitudinal length of the lawn.
In the subsequent n rows, there are two integers, Xi and ri,xi, which represent the horizontal axis of the first water sprinkler (0 on the left), and the RI represents the radius of the circle that the water spray device can cover.

Output

Each set of test data outputs a positive integer that indicates how many water jets are required, each of which has a single row.
If there is no solution that can damp the whole lawn, output 0.

Sample input
22 8 61 14 52 10 64 56 5
Sample output
12
Problem solving: The typical interval is completely covered. Since the water spray device is placed on the transverse centerline and the circle is symmetrical, it is only possible to take half the height, then map the range of intervals covered by each sprinkler to the length of the x-axis, and then greedy to select the segment according to the above method.
AC Code:
1#include <bits/stdc++.h>2 using namespacestd;3 Const intmaxn=10005;4 intT,n,k,cnt,pos,beg;Doublew,h,lb,xi,ri,maxv;pair<Double,Double> ITV[MAXN];BOOLFlag;5 intMain () {6      while(cin>>t) {7          while(t--){8Cin>>n>>w>>h;h/=2.0;p os=cnt=0; //h take half 9              for(intI=0; i<n;++i) {TenCin>>xi>>ri; One                 if(RI&LT;H)Continue;//Ri==h also Count AITV[POS].FIRST=XI-SQRT (ri*ri-h*h); -ITV[POS++].SECOND=XI+SQRT (ri*ri-h*h); -             } theSort (itv,itv+pos); lb=0; beg=0; flag=false; -             if(itv[0].first>0) {cout<<0<<endl;Continue;}//Sort by left endpoint just see if the leftmost endpoint meets the criteria, and the rightmost endpoint is judged below -              while(lb<W) { -maxv=0; +                  for(k=beg;k<pos&&itv[k].first<=lb;++k)//ITV[K].FIRST<=LB This ensures that the entire range is continuous, i.e. the lawn will be wetted -Maxv=max (Maxv,itv[k].second);//FindThe farthest distance the left end of a segment can reach at the right end point within LB +                 if(maxv>lb) cnt++,lb=maxv,beg=k;//If there is a segment right endpoint larger than the currently covered interval right end lb, then update the value of the right end point that has been overwritten and the counter plus 1 A                 Else{flag=true; Break;}//Otherwise the description cannot cover the entire range, exit directly, output 0 at             } -             if(flag) cout<<0<<Endl; -             Elsecout<<cnt<<Endl; -         } -     } -     return 0; in}
Nyoj #6 Water Spraying device (i) description

Existing a lawn, a length of 20 meters, a width of 2 meters, to place a radius of Ri on the Central line of the water spray device, each sprinkler effect will let its center radius for the real Ri (0<ri<15) of the circle is moist, there is sufficient water spray device I (1<i<600) , and must be able to wet all the lawn, what you have to do is: choose as little as possible water spray device, the whole lawn moist.

Input
The first line m indicates that there is a m group of test data
The first line of each set of test data has an integer number N,n represents a total of n water jets, followed by a row of n real Ri,ri that indicate the radius of the circle that the water spray device can cover.
Output
Number of devices used in the output
Sample input
252 3.2 4 4.5 6 101 2 3 1 2 1.2 3 1.1 1 2
Sample output
25
Problem Solving Ideas:
AC Code:

Three kinds of interval coverage problem based on greedy thought

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.