bzoj 1651: [Usaco2006 Feb]Stall Reservations 专用牛棚【贪心+堆||差分】

这个题方法还挺多的,不过洛谷上要输出方案所以用堆最方便

先按起始时间从小到大排序。

我用的是greater重定义优先队列(小根堆)。用pair存牛棚用完时间(first)和牛棚编号(second),每次查看队首的first是否比当前牛的起始时间早,是则弹出队首记录当前牛的答案,再把新的pair放进去,否则增加牛棚,同样要塞进队里

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<queue>
using namespace std;
const int N=200005;
int n,ans,bl[N];
priority_queue<pair<int,int>,vector<pair<int,int> >,greater<pair<int,int> > >q;
struct qwe
{
    int l,r,id;
}a[N];
bool cmp(const qwe &a,const qwe &b)
{
    return a.l<b.l||(a.l==b.l&&a.r<b.r);
}
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();
    for(int i=1;i<=n;i++)
        a[i].l=read(),a[i].r=read(),a[i].id=i;
    sort(a+1,a+1+n,cmp);
    ans=1,bl[a[1].id]=1;
    q.push(make_pair(a[1].r,1));
    for(int i=2;i<=n;i++)
    {
        if(q.top().first<a[i].l)
        {
            bl[a[i].id]=q.top().second;
            q.push(make_pair(a[i].r,q.top().second));
            q.pop();
        }
        else
        {
            bl[a[i].id]=++ans;
            q.push(make_pair(a[i].r,ans));
        }
    }
    printf("%d\n",ans);
    // for(int i=1;i<=n;i++)
        // printf("%d\n",bl[i]);
    return 0;
}

如果不输出方案的话,因为时间范围是1e6,所以直接差分做即可

#include<iostream>
#include<cstdio>
using namespace std;
const int N=1000005;
int n,ans,mx,t[N];
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();
    for(int i=1;i<=n;i++)
    {
        int l=read(),r=read();
        t[l]++,t[r+1]--;
        mx=max(mx,r);
    }
    for(int i=1;i<=mx;i++)
        ans=max(ans,t[i]+=t[i-1]);
    printf("%d\n",ans);
    return 0;
}

原文地址:https://www.cnblogs.com/lokiii/p/8982554.html

时间: 2024-10-11 11:42:51

bzoj 1651: [Usaco2006 Feb]Stall Reservations 专用牛棚【贪心+堆||差分】的相关文章

BZOJ 1651: [Usaco2006 Feb]Stall Reservations 专用牛棚( 线段树 )

线段树.. -------------------------------------------------------------------------------------- #include<cstdio> #include<cstring> #include<algorithm> #include<iostream> #define rep( i , n ) for( int i = 0 ; i < n ; i++ ) #define c

BZOJ 1651: [Usaco2006 Feb]Stall Reservations 专用牛棚

题目 1651: [Usaco2006 Feb]Stall Reservations 专用牛棚 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 553  Solved: 307[Submit][Status] Description Oh those picky N (1 <= N <= 50,000) cows! They are so picky that each one will only be milked over some preci

【差分】BZOJ 1651 [Usaco2006 Feb]Stall Reservations 专用牛棚

显而易见的是我们要求的时间段重叠最多的次数. 用线段树也可以,不过既然是学习和练习前缀和与差分那么就用差分的思想. 在num[a]的位置+1 在num[y+1]位置-1 表示[a,b]区间有一个时间段. 最后统计一下num[i]最多的次数(i是1~n) /************************************************************** Problem: 1651 User: LYFer Language: C++ Result: Accepted Ti

bzoj1651[Usaco2006 Feb]Stall Reservations 专用牛棚*

bzoj1651[Usaco2006 Feb]Stall Reservations 专用牛棚 题意: 有N头牛,每头牛有个喝水时间段,这段时间它将专用一个棚.现在给出每头牛的喝水时间段,问至少要多少个棚才能满足它们的要求.n≤50000,时刻≤1000000. 题解: 时间段左端点对应的sum元素++,右端点+1对应的sum元素--,最后从左到右加一遍就可以得到每个时刻的喝水牛数,找个最大值就可以了. 代码: 1 #include <cstdio> 2 #include <cstring

BZOJ 1652: [Usaco2006 Feb]Treats for the Cows( dp )

dp( L , R ) = max( dp( L + 1 , R ) + V_L * ( n - R + L ) , dp( L , R - 1 ) + V_R * ( n - R + L ) ) 边界 : dp( i , i ) = V[ i ] * n -------------------------------------------------------------------------------------------- #include<cstdio> #include&l

BZOJ 1652: [Usaco2006 Feb]Treats for the Cows

题目 1652: [Usaco2006 Feb]Treats for the Cows Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 234  Solved: 185[Submit][Status] Description FJ has purchased N (1 <= N <= 2000) yummy treats for the cows who get money for giving vast amounts of milk. FJ se

Stall Reservations(POJ 3190 贪心+优先队列)

Stall Reservations Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4434   Accepted: 1588   Special Judge Description Oh those picky N (1 <= N <= 50,000) cows! They are so picky that each one will only be milked over some precise time in

Stall Reservations (poj 3190 贪心)

Language: Default Stall Reservations Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3394   Accepted: 1215   Special Judge Description Oh those picky N (1 <= N <= 50,000) cows! They are so picky that each one will only be milked over so

BZOJ 1653 [Usaco2006 Feb]Backward Digit Sums ——搜索

[题目分析] 劳逸结合好了. 杨辉三角+暴搜. [代码] #include <cstdio> #include <cstring> #include <cmath> #include <cstdlib> #include <map> #include <set> #include <queue> #include <string> #include <iostream> #include <a