poj2376 Cleaning Shifts Range greedy

Source: Internet
Author: User

Main topic:

(Don't say cow anymore)

Give n intervals and select the least number of intervals to cover the interval [1,t]. The n,t are all given.

The default in the title is [1,x],[x+1,t] is also possible. That is, two adjacent intervals can be between the right end of the cell and the left side of the large interval 1. This is a puzzle to know.

Problem Solving Ideas:

The key to greedy problem is to find greedy strategy. But the greedy strategy of this problem is not so obvious. And the greedy strategy does not specifically choose a certain interval. The most important thing in this question is to know under what circumstances it is necessary to add an interval.

The first is to sort, according to the left side of the interval from small to large, the right side of the same as from small to large row.

Traverse from the beginning (only a heavy loop, or time out). When traversing to the I interval [xi,yi], do the following things:

1. Set E as the right end of all currently selected intervals. If xi<=e+1, then the range is likely to be chosen. Of course there may be a lot of such intervals, so we need to find the optimal interval.

2, under the condition of satisfying 1, namely xi<=e+1. Set B to the maximum value of the right end of all the intervals that have been traversed. If the right endpoint of the I interval is yi>b, update B to Yi. Use a flag to mark whether B has been updated. So you can use Flag=1 to mark the update of B. B is there any difference in updating it? b There is no update, which means that this I interval must not be selected.

3. If the value of flag does not change, check if the left endpoint of i+1 and interval xi+1 is greater than e+1. is greater than the words that represent the number of intervals selected needs to be increased by one. The fault is generated because the interval of I is not selected. Then update E to B,flag marked as 0. To do so, you need to add an interval for yourself, so that the left end is larger than the T set in the topic.

"The selected interval needs to be added one ", which does not represent that the I interval must be selected, or it may be that the range of the front is selected.

For example:

This set of data

8 14
1 2
1 3
2 3
2 10
3 9
5 7
8 11
11 14

Traverse to 3 9 this interval, e=3,b=10,cnt=1. CNT is the number of intervals selected. The left endpoint 5 of the next set of data is found to be greater than e+1. So X needs +1. But we will find that the chosen interval is not [3,9], it should be [2,10].

Code:

1#include <iostream>2#include <cstdio>3#include <cstring>4#include <algorithm>5 using namespacestd;6 7 Const intn=25010;8pair<int,int>S[n];9 intMain ()Ten { One     //freopen ("Test.txt", "R", stdin); A     intn,i,t; -      while(SCANF ("%d%d", &n,&t)! =EOF) { -          for(i=0; i<n;i++) thescanf"%d%d",&s[i].first,&s[i].second); -Sort (s,s+n); -         intE=0, b=0, x=0; -         BOOLflag=0; +s[n].first=0x3f3f3f3f; -          for(i=0; i<n;i++) +         { A             if(s[i].first<=e+1) at             { -                 if(B<s[i].second) b=s[i].second,flag=1; -                 if(s[i+1].first>e+1&&flag) -                 { -E=b; -X + +; inflag=0; -                 } to             } +             //printf ("%d%d%d%d%d\n", s[i].first,s[i].second,e,b,flag,x); -         } the         if(e<t) printf ("-1\n"); *         Elseprintf"%d\n", x); $     }Panax Notoginseng     return  0; -}
View Code

poj2376 Cleaning Shifts Range greedy

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.