[POJ2456]Aggressive cows

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

二分+贪心

这是个求最小值最大的问题,我们二分从0到inf的数d,作为两头牛放置的距离不小于d,然后贪心判断。

首先要对x从小到大进行排序,接下来固定x[0]处必有一头牛,然后间距不小于d的时候,可以放置,一直放置,直到所有牛舍均被遍历O(n)。

如果牛被完全放置,那么返回true,并且向右确定边界,反之向左确定。

ac代码(32ms):

 1 #include <cstdio>
 2
 3 const int maxn = 100010;
 4 const int INF = 1 << 30;
 5 int n, m;
 6 int x[maxn];
 7 int ll[maxn>>1], rr[maxn>>1];
 8
 9 inline bool scan_d(int &x) {
10         char in;bool IsN=false;
11         in=getchar();
12         if(in==EOF) return false;
13         while(in!=‘-‘&&(in<‘0‘||in>‘9‘)) in=getchar();
14         if(in==‘-‘){IsN=true;x=0;}
15         else x=in-‘0‘;
16         while(in=getchar(),in>=‘0‘&&in<=‘9‘) {
17                 x*=10,x+=in-‘0‘;
18         }
19         if(IsN) x=-x;
20         return true;
21 }
22
23 inline void printf_d(int a) {
24     if(a > 9) {
25         printf_d(a / 10);
26     }
27     putchar(a % 10 + ‘0‘);
28 }
29
30 void merge(int *x, int p, int m, int q) {
31     int n1 = m - p + 1;
32     int n2 = q - m;
33     int i = 0, j = 0;
34     for(int ii = 0; ii < n1; ii++) ll[ii] = x[p+ii];
35     for(int ii = 0; ii < n2; ii++) rr[ii] = x[m+ii+1];
36     while(i < n1 && j < n2) {
37         if(ll[i] <= rr[j]) x[p++] = ll[i++];
38         else x[p++] = rr[j++];
39     }
40     while(i < n1) x[p++] = ll[i++];
41     while(j < n2) x[p++] = rr[j++];
42 }
43
44 void mergesort(int *x, int p, int q) {
45     if(p < q) {
46         int m = (p + q) >> 1;
47         mergesort(x, p, m);
48         mergesort(x, m+1, q);
49         merge(x, p, m, q);
50     }
51 }
52
53 bool ok(int d) {
54     int cow = 1;
55     int tmp = x[0];
56     for(int i = 1; i < n; i++) {
57         if(x[i] - tmp >= d) {
58             cow++;
59             tmp = x[i];
60         }
61     }
62     if(cow >= m) {
63         return true;
64     }
65     return false;
66 }
67
68 int main() {
69     // freopen("in", "r", stdin);
70     while(scan_d(n) && scan_d(m)) {
71         for(int i = 0; i < n; i++) {
72             scan_d(x[i]);
73         }
74         mergesort(x, 0, n-1);
75         int ll = 0, rr = INF;
76         while(rr - ll > 1) {
77             int mm = (ll + rr) >> 1;
78             if(ok(mm)) {
79                 ll = mm;
80             }
81             else {
82                 rr = mm;
83             }
84         }
85         printf_d(ll);
86         putchar(‘\n‘);
87     }
88 }
时间: 2024-08-13 00:48:33

[POJ2456]Aggressive cows的相关文章

poj2456 Aggressive cows(二分查找)

https://vjudge.net/problem/POJ-2456 二分,从最大长度开始,不断折半试,如果牛全放下了,就是可行,修改下界,否则改上届. 1 #include<iostream> 2 #include<cstdio> 3 #include<queue> 4 #include<cstring> 5 #include<algorithm> 6 #include<cmath> 7 #include<map> 8

POJ2456 Aggressive cows(二分+贪心)

假设C(d)为满足所有牛之间的距离都不小于d.先对牛舍的位置排序,然后二分枚举d,寻找满足条件的d. #include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> #include<algorithm> #include<queue> #include<set> #include<map> #include<vector&g

POJ2456 Aggressive cows (二分)

题目链接: http://poj.org/problem?id=2456 题意: 有n个牛舍,位置为xi,c头牛,把每头牛放在与其相邻牛的距离尽量远的牛舍,即最大化相邻两头牛之间的距离,求这个最大距离. 分析: 二分答案,然后O(N)的复杂度判断符不符合. 代码如下: #include <iostream> #include <cstring> #include <cstdio> #include <algorithm> using namespace st

【POJ - 2456】Aggressive cows(二分)

Aggressive cows 直接上中文了 Descriptions 农夫 John 建造了一座很长的畜栏,它包括N (2 <= N <= 100,000)个隔间,这些小隔间依次编号为x1,...,xN (0 <= xi <= 1,000,000,000). 但是,John的X (2 <= X <= N)头牛们并不喜欢这种布局,而且几头牛放在一个隔间里,他们就要发生争斗.为了不让牛互相伤害.John决定自己给牛分配隔间,使任意两头牛之间的最小距离尽可能的大,那么,这个

最大化最小值 Aggressive cows

Aggressive cows http://poj.org/problem?id=2456 N间小屋,M头牛,使得牛跟牛之间的距离最远,以防止牛打架. 2<=N<=100000 2<=M<=N 0 <=xi<=109 ////////////////////////////////////////////////////////////// C(d):=可以安排牛的位置使得任意两头牛的间距都不小于d 使用二分搜索法解决: //参考文献:挑战程序设计大赛(第二版)/**

BZOJ 1734: [Usaco2005 feb]Aggressive cows 愤怒的牛( 二分答案 )

最小最大...又是经典的二分答案做法.. -------------------------------------------------------------------------- #include<cstdio> #include<cstring> #include<algorithm> #include<iostream> #define rep( i , n ) for( int i = 0 ; i < n ; ++i ) #defin

Aggressive cows

Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Description Farmer John has built a new long barn, with N (2 <= N <= 100,000) stalls. The stalls are located along a straight line at positions x1,...,xN

POJ 2456 Aggressive cows (二分 基础)

Aggressive cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7924   Accepted: 3959 Description Farmer John has built a new long barn, with N (2 <= N <= 100,000) stalls. The stalls are located along a straight line at positions x1,...

POJ 2456 Aggressive cows(二分搜索最大化最小值)

Aggressive cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6372   Accepted: 3181 Description Farmer John has built a new long barn, with N (2 <= N <= 100,000) stalls. The stalls are located along a straight line at positions x1,...