bzoj 3969 WF2013 Low Power [贪心] [二分答案]

来源:互联网 发布:苹果网络不可用 编辑:程序博客网 时间:2024/05/29 18:50

3969: [WF2013]Low Power

Time Limit: 10 Sec Memory Limit: 256 MB
Submit: 365 Solved: 171

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。


也就是说有n*k*2个电池放进每个机器两个电池组,使得每个机器每个电池组的最小值之差的最大值最小。
那么二分+check即可。
最小的一定是排序后相邻两个值,因为如果不是这两个的话,答案不会更优。
可以不用直接模拟,只需要找到当前的电池位置之后看剩下的电池数量能不能满足剩下的cnt个机器。

#include<iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<cmath>#include<vector>#include<queue>#include<stack>#include<map>#include<set>#include<string>#include<iomanip>#include<ctime>#include<climits>#include<cctype>#include<algorithm>#ifdef WIN32#define AUTO "%I64d"#else#define AUTO "%lld"#endifusing namespace std;#define smax(x,tmp) x=max((x),(tmp))#define smin(x,tmp) x=min((x),(tmp))#define maxx(x1,x2,x3) max(max(x1,x2),x3)#define minn(x1,x2,x3) min(min(x1,x2),x3)const int INF=0x3f3f3f3f;const int maxn = 1000005;int n,k;int battery[maxn];/*bool used[maxn];struct Node{    int cur[2],cnt;}node[maxn];inline bool check(int lim){    memset(node,0,sizeof(node));    memset(used,0,sizeof(used));    int pos = 0;    for(int i=1;i<=(n*k<<1);i++)        if(battery[i+1]-battery[i]<=lim)        {            Node &t = node[++pos];            t.cur[0] = battery[i]; t.cur[1] = battery[i+1];            t.cnt = 2;            used[i] = used[i+1] = true;            i++;            if(pos==n) break;        }    if(pos^n) return false;    pos = 1; // set for batteries    for(int i=1;i<=n;i++) // set for the machines        while(node[i].cnt ^ k)        {            int id = 0;            if(node[i].cur[1] < node[i].cur[0]) id ^= 1;            while(pos<=(n*k<<1) && used[pos]) pos++;            if(pos == (n*k<<1) + 1) return false; // lack of batteries            if(node[i].cur[id] > battery[pos]) return false; // non profiter pas plus            node[i].cur[id] = battery[pos]; node[i].cnt++;            pos++;        }    return true;}*/inline bool check(int lim){    int cnt = n;    for(int i=1;cnt && i<=(n*k<<1);i++) // si les tout machine sur battery, break        if(i^(n*k<<1) && battery[i+1]-battery[i]<=lim)        {            if((n*k<<1)-i+1 < (cnt*k<<1)) return false; // C'est no necessaire par la comparison en array            cnt--; i++;        }    return !cnt; // si non de profiter}int work(){    int l = battery[2] - battery[1] , r = battery[n*k<<1];    while(l<r)    {        int m = (l+r)>>1;        bool tmp = check(m);        if(tmp) r = m;        else l = m + 1;    }    return l;}int main(){#ifndef ONLINE_JUDGE    freopen("battery.in","r",stdin);    freopen("battery.out","w",stdout);#endif    scanf("%d%d",&n,&k);    for(int i=1;i<=(n*k<<1);i++) scanf("%d",battery+i);    sort(battery+1,battery+(n*k<<1)+1);    int ans = work();    printf("%d",ans);    return 0;}
0 0
原创粉丝点击