bzoj 1689: [Usaco2005 Open] Muddy roads 泥泞的路【贪心】

本文介绍了一种通过按区间的左端点进行排序并使用贪心策略来解决问题的方法。该方法适用于一系列区间覆盖问题,通过C++实现,并详细展示了从输入到输出的整个流程。

按左端点排序,贪心的选即可

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
const int N=10005;
int n,l,p,ans;
struct qwe
{
    int x,y;
}a[N];
bool cmp(const qwe &a,const qwe &b)
{
    return a.x<b.x;
}
int read()
{
    int r=0,f=1;
    char p=getchar();
    while(p>'9'||p<'0')
    {
        if(p=='-')
            f=-1;
        p=getchar();
    }
    while(p>='0'&&p<='9')
    {
        r=r*10+p-48;
        p=getchar();
    }
    return r*f;
}
int main()
{
    n=read(),l=read();
    for(int i=1;i<=n;i++)
       a[i].x=read(),a[i].y=read();
    sort(a+1,a+n+1,cmp);
    for(int i=1;i<=n;i++)
        if(p<a[i].y)
        {
            p=max(p,a[i].x);
            while(p<a[i].y)
                ans++,p+=l;
        }
    printf("%d",ans);
    return 0;
}

转载于:https://www.cnblogs.com/lokiii/p/9014891.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值