• 【BZOJ】1651: [Usaco2006 Feb]Stall Reservations 专用牛棚(线段树/前缀和 + 差分)


    http://www.lydsy.com/JudgeOnline/problem.php?id=1651

    很奇妙。。

    我们发现,每一时刻的重叠数选最大的就是答案。。。。

    orz

    那么我们可以线段树维护每个点的次数。。。

    然后就ok了。。

    第二种做法:用前缀和来维护即可。。。

    线段树:

    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <string>
    #include <iostream>
    #include <algorithm>
    #include <queue>
    using namespace std;
    #define rep(i, n) for(int i=0; i<(n); ++i)
    #define for1(i,a,n) for(int i=(a);i<=(n);++i)
    #define for2(i,a,n) for(int i=(a);i<(n);++i)
    #define for3(i,a,n) for(int i=(a);i>=(n);--i)
    #define for4(i,a,n) for(int i=(a);i>(n);--i)
    #define CC(i,a) memset(i,a,sizeof(i))
    #define read(a) a=getint()
    #define print(a) printf("%d", a)
    #define dbg(x) cout << #x << " = " << x << endl
    #define printarr(a, n, m) rep(aaa, n) { rep(bbb, m) cout << a[aaa][bbb]; cout << endl; }
    inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }
    inline const int max(const int &a, const int &b) { return a>b?a:b; }
    inline const int min(const int &a, const int &b) { return a<b?a:b; }
    #define lc x<<1
    #define rc x<<1|1
    #define lson l, m, lc
    #define rson m+1, r, rc
    #define MID (l+r)>>1
    struct nod { int mx, tag; }t[4000005];
    int n;
    void pushdown(int x) {
    	int g=t[x].tag; t[x].tag=0;
    	t[lc].mx+=g; t[lc].tag+=g;
    	t[rc].mx+=g; t[rc].tag+=g;
    }
    void pushup(int x) { t[x].mx=max(t[lc].mx, t[rc].mx); }
    void update(int l, int r, int x, int L, int R) {
    	if(L<=l && r<=R) { ++t[x].mx; ++t[x].tag; return; }
    	pushdown(x);
    	int m=MID;
    	if(L<=m) update(lson, L, R); if(m<R) update(rson, L, R);
    	pushup(x);
    }
    int main() {
    	read(n);
    	for1(i, 1, n) {
    		int l=getint(), r=getint();
    		update(1, 1000000, 1, l, r);
    	}
    	print(t[1].mx);
    	return 0;
    }
    

    前缀和:

    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <string>
    #include <iostream>
    #include <algorithm>
    #include <queue>
    using namespace std;
    #define rep(i, n) for(int i=0; i<(n); ++i)
    #define for1(i,a,n) for(int i=(a);i<=(n);++i)
    #define for2(i,a,n) for(int i=(a);i<(n);++i)
    #define for3(i,a,n) for(int i=(a);i>=(n);--i)
    #define for4(i,a,n) for(int i=(a);i>(n);--i)
    #define CC(i,a) memset(i,a,sizeof(i))
    #define read(a) a=getint()
    #define print(a) printf("%d", a)
    #define dbg(x) cout << #x << " = " << x << endl
    #define printarr(a, n, m) rep(aaa, n) { rep(bbb, m) cout << a[aaa][bbb]; cout << endl; }
    inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }
    inline const int max(const int &a, const int &b) { return a>b?a:b; }
    inline const int min(const int &a, const int &b) { return a<b?a:b; }
    const int M=1000005;
    int n, t[M], sum, mx, ans;
    int main() {
    	read(n);
    	for1(i, 1, n) {
    		int x=getint(), y=getint();
    		++t[x]; --t[y+1]; mx=max(mx, y);
    	}
    	for1(i, 1, mx) {
    		sum+=t[i];
    		if(sum>ans) ans=sum;
    	}
    	print(ans);
    	return 0;
    }
    

    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


    OUTPUT DETAILS:

    Here's a graphical schedule for this output:

    Time 1 2 3 4 5 6 7 8 9 10
    Stall 1 c1>>>>>>>>>>>>>>>>>>>>>>>>>>>
    Stall 2 .. c2>>>>>> c4>>>>>>>>> .. ..
    Stall 3 .. .. c3>>>>>>>>> .. .. .. ..
    Stall 4 .. .. .. c5>>>>>>>>> .. .. ..

    Other outputs using the same number of stalls are possible.

    HINT

    不妨试下这个数据,对于按结束点SORT,再GREEDY的做法 1 3 5 7 6 9 10 11 8 12 4 13 正确的输出应该是3

    Source

  • 相关阅读:
    修改linux资源限制
    Windows 建立链接
    Java & ant环境变量配置
    Linux 配置IP
    Spring-MVC 访问静态资源
    Maven 安装与配置
    OIM同步OID(OID-Connector 9.0.4.12)
    Connector|OIM向IBM TDS推送账号(LDAP3)
    TopFreeTheme精选免费模板【20130704】
    30+简约和平铺的WordPress复古主题
  • 原文地址:https://www.cnblogs.com/iwtwiioi/p/3957041.html
Copyright © 2020-2023  润新知