【codevs】【贪心】1214线段覆盖

  完了完了碰到线段就不会做了QAQ,看看大神的博客http://m.blog.csdn.net/blog/maverick1990/17262495

  代码这儿:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
const int maxn=110;
int n,ans=1;
struct line
{
    int l,r;
}a[110];
void work()
{
    int now=a[1].r;
    for(int i=2;i<=n;i++)//遍历下一区间 
    {
        if(a[i].l>=now)//如果当前区间的右端点不包含下一区间的左端点,就选择这个区间 
        {
            now=a[i].r;
            ans++;
        }
        else
        {
            if(a[i].r<now)//如果下一区间在当前区间内部,那么选择下一区间,不要当前区间,因为下一区间更短,方案更优 
            {
                now=a[i].r;
            }
        }
    }
    cout<<ans;
}
bool cmp(line a,line b)
{
    return a.l<b.l;
}
void read()
{
    cin>>n;
    int x,y;
    for(int i=1;i<=n;i++)
    {
        scanf("%d%d",&x,&y);
        if(x<y){a[i].l=x;a[i].r=y;}
        else{a[i].r=x;a[i].l=y;}
    }
    sort(a+1,a+n+1,cmp);
}
int main()
{
    read();
    work();
    return 0;
}

 

  

posted @ 2015-07-29 22:12  sajuuk  阅读(166)  评论(0编辑  收藏  举报