Interval coverage

Title: interval coverage
meaning of the questions: Yes n (1 <= n <= 25000) a closed interval [ai, bi] number line, select as few sections cover a specified line segment [1, t] (1 < = t <= 1,000,000).
Covering the entire point, i.e., (1,2) + (3,4) may cover (1,4).
The impossible Output -1

Input:
First line: N T and the
second row N + 1 to row: each line a closed interval.

Output:
the number of selected sections, the impossible outputs -1

Problem-solving ideas: interval coverage problem solving greedy; go head to tail, that is, the left end of the line is less than 1 point to let him into 1 (if the right end point is less than 1, does not affect); then sorted in ascending line left point . Then the right time to take maximum points (the same as if the left point, then), if different from the left point, to conduct a special judge, look at the next point is not left in the value of +1 (plus one must have tips above), if in the case, to stop output -1; if not, the point you want to overwrite the left end line into the right end point of the line segment your currently selected, and then continue to cut off heads, until the results obtained;
then this method Why can it? We left most of the segments covered, for example, we have chosen is the left point less than or equal value in the right end point of the line 1 minus 1 the largest one; if you do not choose this, then we can only choose a smaller one than this, so that you will become more than the rest of the range, your situation is not getting better even worse; or choose to continue this line problem facing the same after you finished selecting the far left. From here we can see that greed should be optimal.

Similar topics (evil cow)

Deceptive place: Please note that the above mentioned covering the whole point, I do not have this information when it comes, they thought it was a line segment covered, and then crazy to white. . . So be sure to add one!

Code:

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<vector>
using namespace std;
bool cmp(pair<int,int> a,pair<int,int> b)
{
    return a.first<b.first;
}
int main()
{
    int n,t,x,y;
    vector<pair<int,int> > h;
    while(scanf("%d%d",&n,&t)!=EOF)
    {
        int total=1,flag=-1,b=1;
        for(int i=0;i<n;i++)
        {
            scanf("%d%d",&x,&y);
            if(x>y)//由于被坑的有点厉害,就谨慎了一点
            {
                swap(x,y);
            }
            if(x<1){x=1;}//裁剪
            if(y>t){y=t;}
            h.push_back(make_pair(x,y));
        }
        if(n==0||t<1)//谨慎
        {
            printf("-1\n");
            h.clear();
            continue;
        }
        sort(h.begin(),h.end(),cmp);//排序
        if(h[0].first>1)//特判第一个
        {
            printf("-1\n");
            h.clear();
            continue;
        }
        for(int i=0;i<n;i++)//进行贪心
        {
            if(h[i].first>b)//如果当前线段左端点数值和b不同,说明左端点为b的线段已经筛选完了
            {
                if(h[i].first>flag+1)//记住,一定要加个1!!!
                {
                    printf("-1\n");
                    break;
                }
                total++;//所选的线段数加1
                for(int j=i;j<n;j++)//把左端点进行变更,并进行裁剪
                {
                    if(h[j].first<flag+1)
                    {
                        h[j].first=flag+1;
                    }else
                    {
                        break;
                    }
                }
                b=flag+1;//变更左端点
            }
            flag=max(flag,h[i].second);//求个右端点最大的
            if(flag==t)//判断一下
            {
                printf("%d\n",total);
                break;
            }
            if(i==n-1)//扫完了还没成功,就说明失败了
            {
                printf("-1\n");
            }
        }
        h.clear();
    }
}
Published 15 original articles · won praise 0 · Views 227

Guess you like

Origin blog.csdn.net/qq_43653717/article/details/104693793
Recommended