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

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

Time Limit: 10 Sec  Memory Limit: 64 MB
Submit: 876  Solved: 496
[Submit][Status][Discuss]

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 interval A..B (1 <= A <= B <= 1,000,000), which includes both times A and B. Obviously, FJ must create a reservation system to determine which stall each cow can be assigned for her milking time. Of course, no cow will share such a private moment with other cows. Help FJ by determining: * The minimum number of stalls required in the barn so that each cow can have her private milking period * An assignment of cows to these stalls over time

有N头牛,每头牛有个喝水时间,这段时间它将专用一个Stall 现在给出每头牛的喝水时间段,问至少要多少个Stall才能满足它们的要求

Input

* Line 1: A single integer, N

* Lines 2..N+1: Line i+1 describes cow i's milking interval with two space-separated integers.

Output

* Line 1: The minimum number of stalls the barn must have.

* Lines 2..N+1: Line i+1 describes the stall to which cow i will be assigned for her milking period.

Sample Input

5
1 10
2 4
3 6
5 8
4 7

Sample Output

4

这道题,有两种做法,一种是线段树,一种是差分序列

然而我都没交对。。。。。

粘俩代码,谁也别喷我。。。。

#include<cstdio>
#define M 1000010
inline int read()
{
    int x=0;char ch=getchar();
    while(ch<'0'||ch>'9') ch=getchar();
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x;
}
inline int max(int a,int b){return a>b?a:b;}
struct ljn{int l,r,maxx,lazy;}tr[4*M];
int n,x,y;
void make(int l,int r,int p)
{
    tr[p].l=l,tr[p].r=r,tr[p].lazy=0;tr[p].maxx=0;
    if(l==r) return;
    int mid=(l+r)>>1;
    make(l,mid,p<<1);
    make(mid+1,r,p<<1|1);
}
void pd(int p)
{
    int lz=tr[p].lazy;
    tr[p<<1].lazy+=lz;
    tr[p<<1|1].lazy+=lz;
    tr[p<<1].maxx+=lz;
    tr[p<<1|1].maxx+=lz;
    tr[p].lazy=0;
}
void xg(int l,int r,int c,int p)
{
    if(tr[p].l==l&&tr[p].r==r)
    {
        tr[p].lazy+=c;
        tr[p].maxx+=c;
        return;
    }
    pd(p);
    int mid=(tr[p].l+tr[p].r)>>1;
    if(mid>=r) xg(l,r,c,p<<1);
    else if(mid<l) xg(l,r,c,p<<1|1);
    else 
    {
        xg(l,mid,c,p<<1);
        xg(mid+1,r,c,p<<1|1);
    }
    tr[p].maxx=max(tr[p<<1].maxx,tr[p<<1|1].maxx);
}
int main()
{
    n=read();
    make(1,1000000,1);
    for(int i=0;i<n;i++)
    {
        x=read();y=read();
        xg(x,y,1,1);
    }
    printf("%d\n",tr[1].maxx);
}


下面这个diao一点


#include<cstdio>
#include<algorithm>
#define fo(i,a,b) for(i=a;i<=b;i++)
using namespace std;
const int maxd=1000000+10;
int f[maxd];
int i,j,k,l,t,n,m,ans;
int main(){
    scanf("%d",&n);
    fo(i,1,n){
        scanf("%d%d",&j,&k);
        f[j]++;f[k+1]--;
    }
    fo(i,1,1000001){
        f[i]+=f[i-1];
        ans=max(ans,f[i]);
    }
    printf("%d\n",ans);
}


1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看REAdMe.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看REAdMe.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看READme.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值