首页 > 代码库 > bzoj3969 [WF2013]Low Power

bzoj3969 [WF2013]Low Power

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

【题解】

二分答案x,贪心选取,如果选取了i个,有j对,那么要满足i<=2*j*k(不然有数就没位置放了)

技术分享
# include <stdio.h>
# include <string.h>
# include <iostream>
# include <algorithm>
// # include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
const int M = 2e6 + 10;
const int mod = 1e9+7;

# define RG register
# define ST static

int n, k, N; 
int a[M]; 

inline bool chk(int x) { 
    int times = 0;
    for (int i=1; i<=N; ++i) { 
        if(i-1 > 2*times*k) return false; 
        if(i <= N && a[i+1] - a[i] <= x) ++i, ++times; 
        if(times == n) return 1; 
    }
    return false;
}
         
int main() {
    cin >> n >> k; N = 2*n*k;
    for (int i=1; i<=N; ++i) scanf("%d", a+i);
    sort(a+1, a+N+1);
    int l = a[2] - a[1], r = 1e9, ans;
    while(1) {
        if(r-l <= 3) {
            for (int i=l; i<=r; ++i) {
                if(chk(i)) {
                    ans = i;
                    break;
                }
            }
            break;
        }
        int mid = l+r>>1;
        if(chk(mid)) r = mid;
        else l = mid;
    }
    cout << ans << endl; 
    return 0;
}
View Code

 

bzoj3969 [WF2013]Low Power