acwing-- greedy Interval Problems

1. Range of

1. The choice of site range

Algorithm ideas:
1. First of all sections in the right end in ascending order
If the left endpoint ascending order, will appear
section a section b above, it is preferred the right end of a section, it will lead is not included in interval b any point.
Here Insert Picture Description

2. The positioning section to the right end and left end point of the next segment, and if an interval to find the left point and the right end than the positioning section,
this case indicates that the intermediate section has two neutral, the right end of the positioning over this interval, until the end of the cycle.
3. The problem is equivalent to the number of how many disjoint interval seek most.

Justify: Suppose we answer ans, seeking out is cnt, in order to prove cnt = ans, if they can prove ans≤ and ans≥cnt.
(1) Clearly ans≤cnt;
(2) because the conditions at the time increment cnt is no intersection of two intervals, so mark these disjoint intervals is necessary for all sections of the mark, so the final answer must be greater than or equal already marked the number of times that ans≥cnt.
Is proved: ans = cnt

#include <iostream>
#include <algorithm>

using namespace std;

typedef pair<int , int> PII;
const int N = 100010;

PII range[N];//first是右端点 second是左端点 

int ans;

int main()
{   
    int n;
    cin >> n;
    
    for(int i = 0 ; i < n ; i ++)
        cin >> range[i]. second >> range[i].first;
        
    sort(range , range + n);
    
    int  r = -2e9;
    for(int i = 0;  i < n ; i++)
    {
        if(range[i].second > r)
        {
            ans++;
            r = range[i].first;
        }
    }
    
    cout << ans << endl;
    
    
    
    return 0;
}

2. The maximum number of disjoint intervals

Algorithm thinking: exactly the same code point range selected
justify:
assuming that the last number is the largest ans, the number is obtained res.
According to the previous question we seek out res points, indicating a maximum of res intervals do not intersect. If there ans intervals do not intersect, and ans> res, you should elect ans points to the travels of each interval, but the point has been determined res traveled enough to meet all the points, indicating ans disjoint interval does not exist .

代码同上

3. Packet Interval

Title: Given N closed interval [A I , B I ], you will be divided into several groups of these intervals, so that (inclusive) there is no intersection, and as small as possible so that the number of groups between any two interior sections in each group.

Thinking algorithm:
1. The first point of the interval in the left ascending order;
2. declare a small heap root to maintain the right of each group of the rightmost point
3. Each section determines a new left point range [i] .first and small the right end of the root of the heap heap.top (),
if heap.pop ()> = range [i ] .first, described there is not a big enough for the new interval set , a need to create a new super group;
otherwise, the new discharge section to the right end of the minimal group, i.e., rightmost point heap.top () of the group (as long as it can accommodate the new section of the discharge, where the minimum set directly into the right point for convenience).
Updating the new set point right section of added / created 4.
(1) the new group, directly to the range [i] .second to stack;
(2) added to an existing group, the rightmost point of the set of pop heap, and the right end point of the new range of push can be.

Justify: ANS is set the minimum interval, the interval is seeking out CNT
1. Obviously ANS <CNT =
2. When we open to combinations cnt-1, the left end of the next section if the rightmost ≤ all groups the right end, then need to open a combination, this time range [i] .first cnt groups and have an intersection (point of the left ascending order as is, the right end of the new section so there is an existing ratio of the group small dots left case section), i.e., combined with it cnt a common point, it is necessary at least cnt groups, i.e. ans≥cnt.
is proved: ans = cnt

#include <iostream>
#include <queue>
#include <algorithm>

using namespace std;

const int N = 100010;

int n;

pair<int , int> range[N];

int main()
{
    cin >> n;
    
    for(int i = 0 ; i < n ; i++)
        cin >> range[i].first >> range[i].second;
        
    sort(range , range + n);
    
    priority_queue<int , vector<int> , greater<int>> heap;
    
    for(int i = 0 ; i < n ; i++)
    {
        if(heap.empty() || heap.top() >= range[i].first) ;
        else    heap.pop();
        
        heap.push(range[i].second);
    }
    
    cout << heap.size() << endl;
    return 0;
}

4. The cover section

Title: Given N closed interval [A I , B I ] and a segment interval [s, t], you choose to minimize the interval, the interval specified line segment completely covered.
Minimum number of output sections, if not completely cover the outputs -1.

Algorithms ideas:
1. First sorted in ascending left point;
2. starting point for st, find less st left point range, and select the largest range right point, with the right end-point update st;
3. If you can not find the left st point is less than the interval or cycle through all sections of the given range can not be covered, or -1.

#include <iostream>
#include <algorithm>

using namespace std;

const int N = 100010;

pair<int , int> range[N];

bool success;
int st , ed;
int ans;
int n;

int main()
{
    cin >>st >> ed >> n;
    
    for(int i = 0 ; i < n ; i++)
        cin >> range[i].first >> range[i].second;
        
    sort(range , range + n);
    
    for(int i = 0 ; i < n ; i++)
    {
        int j = i , r = -2e9;//r用来记录右端点最远的点
        while(j < n && range[j].first <= st)
            r = max(r , range[j++].second);
            
        if(r < st)
        {
            ans = -1;
            break;
        }
        
        ans++;
        if(r >= ed)
        {
            success = true;
            break;
        }
        
        st = r;
        i = j  -1;
    }
    
    if(!success) ans = -1;
    cout << ans << endl;
    return 0;
}
Published 14 original articles · won praise 3 · Views 348

Guess you like

Origin blog.csdn.net/Stephen_Zhao0/article/details/105207316