POJ 2456 Aggressive cows 二分 贪心

  题目链接: http://poj.org/problem?id=2456

  题目描述: 给你N个位置, 让你在这些位置中放M个元素, 使得其中最小的距离最大化

  解题思路: 最小值最大化的问题一般都是二分吧, 要掌握好边界条件, 要写好OK函数.....

  代码: 

#include <iostream>
#include <cstdio>
#include <string>
#include <vector>
#include <cstring>
#include <iterator>
#include <cmath>
#include <algorithm>
#include <stack>
#include <deque>
#include <map>
#include <set>
#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
#define mem0(a) memset(a,0,sizeof(a))
#define sca(x) scanf("%d",&x)
#define de printf("=======\n")
typedef long long ll;
using namespace std;
const int INF = 1e9+5;
const int maxn = 1e5+10;
int x[maxn];
int n, m;

int ok( int num ) {
    int temp = m-1;
    int cnt = 0;
    for( int i = 1; i < n; i++ ) {
        if( x[i]-x[cnt] >= num ) {
            temp--;
            cnt = i;
        }
    }
    if( temp <= 0 ) return 1;
    else return 0;
}

void solve() {
    int low = 0;
    int high = INF;
    while( high - low > 1 ) {
        int m = (low+high) >> 1;
        if( ok(m) ) {
            low = m;
        }
        else {
            high = m;
        }
    }
    printf( "%d\n", low );
}

int main() {
    while( scanf( "%d%d", &n, &m ) == 2 ) {
        for( int i = 0; i < n; i++ ) {
            sca( x[i] );
        }
        sort(x, x+n);
        solve();
    }
    return 0;
}
View Code

  思考: 这题没啥的吧, 今天去面试了...... 很难受, 像死一样,进入复试和老板谈话的时候我感觉整个世界都在旋转一样, 倒不是因为紧张, 是感觉自己实在是很虚, 学校的老师会看你的潜力, 而公司的老板只会看你现在是不是有价值, stupid, 还有一些面试的技巧需要我去学习, 去看看明天的橘子娱乐吧.....我一定要加油啊!!!

posted on 2017-09-06 21:12  FriskyPuppy  阅读(163)  评论(0编辑  收藏  举报

导航