码迷,mamicode.com
首页 > 其他好文 > 详细

POJ2456 Aggressive cows

时间:2016-08-12 17:54:59      阅读:135      评论:0      收藏:0      [点我收藏+]

标签:

  Aggressive cows

  二分,关键是转化为二分!

技术分享
 1 #include <cstdio>
 2 #include <algorithm>
 3 const int maxn = 1000000005;
 4 const int maxN = 100005;
 5 
 6 int N, C;
 7 int a[maxN];
 8 using namespace std;
 9 bool judge(int x) {
10     int s = 0;
11     for (int i = 1; i < C;i++) {
12         int ctr = s+1;
13         while (ctr < N && (a[ctr]-a[s] < x)) {
14             ctr++;
15         }
16         if (ctr==N) {
17             return false;
18         }
19         s = ctr;
20     }
21     return true;
22 }
23 int main(void)
24 {
25     scanf("%d%d", &N, &C);
26     for (int i = 0; i < N; i++) {
27         scanf("%d", &a[i]);
28     }
29     sort(a, a+N);
30     int l = 0, u = maxn, mid;
31     for (;(u-l)>1;){
32         mid = (l+u)/2;
33         if (judge(mid)) {
34             l = mid;
35         } else {
36             u = mid;
37         }
38     }
39     printf("%d\n", l);
40     return 0;
41 }
View Code

 

POJ2456 Aggressive cows

标签:

原文地址:http://www.cnblogs.com/zhaoyu1995/p/5765817.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!