POJ 3186 Treats for the Cows(贪心)

this is the question’s link

I believe this question is a greedy algorithm ,so I sort the data first and than update the maximum vale of right value of each member of struct node

#include <iostream>
#include <cstring>
#include <algorithm>
#define ll long long
#define maxn 105
using namespace std;
struct node
{
    int left,right;
}cow[25005];
int cmp(node a,node b)
{
    return a.left < b.left;
}
int main()
{

    int m,n;
    cin>>m>>n;
    for(int i=1;i<=m;i++)
    {
        cin>>cow[i].left>>cow[i].right;
    }
    sort(cow+1,cow+m+1,cmp);
    int l = 0,i=0;
    int ans=0;
    while(l < n)
    {
        int r1 = -1,r2;
        while(cow[++i].left<=l+1)
        {
            r1 = cow[i].right >r1 ?cow[r2 = i].right:r1;

        }
        if(r1==-1)
        {
            cout<<-1;
            return 0;
        }
        ans++;
        i--;
        l=r1;
    }
    cout<<ans;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值