zoukankan      html  css  js  c++  java
  • luogu2564 [SCOI2009]生日礼物

    排序枚举左端点,则右端点必定不降

    #include <algorithm>
    #include <iostream>
    #include <cstring>
    #include <cstdio>
    using namespace std;
    struct Node{
    	int pos, val;
    }nd[1000005];
    int n, k, cnt[63], uu, vv=0, rig=0, re=0, ans=0x3f3f3f3f;
    void rn(int &x){
    	x = 0;
    	char ch=getchar();
    	while(ch<'0' || ch>'9')	ch = getchar();
    	while(ch>='0' && ch<='9'){
    		x = x * 10 + ch - '0';
    		ch = getchar();
    	}
    }
    bool cmp(Node x, Node y){
    	return x.pos<y.pos;
    }
    int main(){
    	rn(n); rn(k);
    	for(int i=1; i<=k; i++){
    		rn(uu);
    		for(int j=1; j<=uu; j++){
    			rn(nd[++vv].pos);
    			nd[vv].val = i;
    		}
    	}
    	sort(nd+1, nd+1+vv, cmp);
    	for(int i=1; i<=n; i++){
    		while(re<k && rig<n){
    			rig++;
    			if(++cnt[nd[rig].val]==1)
    				re++;
    		}
    		if(re==k)
    			ans = min(ans, nd[rig].pos-nd[i].pos);
    		if(--cnt[nd[i].val]==0)	re--;
    	}
    	cout<<ans<<endl;
    	return 0;
    }
    

    当然也可以用滑动窗口的思想做,就是比较慢

    #include <algorithm>
    #include <iostream>
    #include <cstring>
    #include <cstdio>
    using namespace std;
    struct Node{
        int pos, val;
    }nd[1000005];
    int n, k, cnt[63], uu, vv=0, lev=1, rig=0, a[1000005];
    void rn(int &x){
        x = 0;
        char ch=getchar();
        while(ch<'0' || ch>'9')	ch = getchar();
        while(ch>='0' && ch<='9'){
            x = x * 10 + ch - '0';
            ch = getchar();
        }
    }
    bool cmp(Node x, Node y){
        return x.pos<y.pos;
    }
    bool check(int lim){
        memset(cnt, 0, sizeof(cnt));
        lev = 1, rig = 0;
        int re=0;
        for(int i=1; i<=n; i++){
            while(lev<=rig && nd[a[lev]].pos<nd[i].pos-lim){
                if(--cnt[nd[a[lev]].val]==0)	re--;
                lev++;
            }
            a[++rig] = i;
            if(++cnt[nd[i].val]==1)	re++;
            if(re==k)	return true;
        }
        return false;
    }
    int main(){
        rn(n); rn(k);
        for(int i=1; i<=k; i++){
            rn(uu);
            for(int j=1; j<=uu; j++){
                rn(nd[++vv].pos);
                nd[vv].val = i;
            }
        }
        sort(nd+1, nd+1+vv, cmp);
        int l=0, r=nd[vv].pos, mid, ans=0;
        while(l<=r){
            mid = (l + r) >> 1;
            if(check(mid)){
                ans = mid;
                r = mid - 1;
            }
            else	l = mid + 1;
        }
        cout<<ans<<endl;
        return 0;
    }
    
  • 相关阅读:
    windows设置通过NFS连接到Linux共享数据
    MYSQL安装相关知识
    Linux下Eclipse里用gdb调试JNI里C/C++
    linux上安装Eclipse
    vs2010常用快捷键
    .NET Framework 2.0/3.0/3.5 以 v90 平台工具集为目标。请确保在计算机上安装了 Visual Studio 2008
    C++ ->error LNK1123
    Python 函数
    Python安装工具
    Web接口测试理论知识分享
  • 原文地址:https://www.cnblogs.com/poorpool/p/8191145.html
Copyright © 2011-2022 走看看