• bzoj 3969: [WF2013]Low Power 二分


    3969: [WF2013]Low Power

    Time Limit: 20 Sec  Memory Limit: 256 MB

    题目连接

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

    Description

    有n个机器,每个机器有2个芯片,每个芯片可以放k个电池。
    每个芯片能量是k个电池的能量的最小值。
    两个芯片的能量之差越小,这个机器就工作的越好。
    现在有2nk个电池,已知它们的能量,我们要把它们放在n个机器上的芯片上,
    使得所有机器的能量之差的最大值最小。

    Input

    第一行,两个正整数,n和k。
    第二行,2nk个整数,表示每个电池的能量。

    Output

    一行一个整数,表示所有机器的能量之差的最大值最小是多少。

    Sample Input

    2 3
    1 2 3 4 5 6 7 8 9 10 11 12

    Sample Output

    1

    HINT

     2nk <= 10^6, 1 <= pi <= 10^9。

    题意

    题解:

    排序之后二分答案

    check 是否前2ik个数里面至少有i个对满足题意的数

    代码:

    //qscqesze
    #include <cstdio>
    #include <cmath>
    #include <cstring>
    #include <ctime>
    #include <iostream>
    #include <algorithm>
    #include <set>
    #include <vector>
    #include <sstream>
    #include <queue>
    #include <typeinfo>
    #include <fstream>
    #include <map>
    #include <stack>
    typedef long long ll;
    using namespace std;
    //freopen("D.in","r",stdin);
    //freopen("D.out","w",stdout);
    #define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
    #define maxn 1000001
    #define mod 10007
    #define eps 1e-9
    int Num;
    char CH[20];
    //const int inf=0x7fffffff;   //нчоч╢С
    const int inf=0x3f3f3f3f;
    /*
    
    inline void P(int x)
    {
        Num=0;if(!x){putchar('0');puts("");return;}
        while(x>0)CH[++Num]=x%10,x/=10;
        while(Num)putchar(CH[Num--]+48);
        puts("");
    }
    */
    inline ll read()
    {
        ll x=0,f=1;char ch=getchar();
        while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
        while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
        return x*f;
    }
    inline void P(int x)
    {
        Num=0;if(!x){putchar('0');puts("");return;}
        while(x>0)CH[++Num]=x%10,x/=10;
        while(Num)putchar(CH[Num--]+48);
        puts("");
    }
    //**************************************************************************************
    
    int n,m,k;
    int a[maxn];
    int l,r,mid;
    bool check(int x)
    {
        for(int p=1,q=0;q<n;++p)
        {
            if(p-1>q*2*k)
                return false;
            if(a[p+1]-a[p]<=x)
                ++p,++q;
        }
        return true;
    }
    int main()
    {
        n=read(),k=read();
        m=2*n*k;
        l=0,r=0;
        for(int i=1;i<=m;i++)
        {
            a[i]=read();
            r=max(r,a[i]);
        }
        sort(a+1,a+m+1);
        while(l<r)
        {
            mid=(l+r)>>1;
            if(check(mid))
                r=mid;
            else   
                l=mid+1;
        }
        printf("%d
    ",l);
    }
  • 相关阅读:
    XMPP框架下微信项目总结(5)花名册获取(好友列表)
    XMPP框架下微信项目总结(4)重新连接服务器
    XMPP框架下微信项目总结(3)获取点子名片信息(个人资料)更新电子名片
    XMPP框架下微信项目总结(2)授权登陆/注销/注册/打印日志
    XMPP框架下微信项目总结(1)环境配置
    比较 http连接 vs socket连接
    js判断变量是否为undefined
    sql查询优化的方法
    Jquery学习之插件开发
    Jquery学习之Jquery插件
  • 原文地址:https://www.cnblogs.com/qscqesze/p/4526408.html
Copyright © 2020-2023  润新知