ACM-二分贪心E-05
2017-05-03 16:01
239 查看
(欢迎阅读我的博客,如发现错误或有建议请评论留言,谢谢。)
题目如下:
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 (0 <= xi <= 1,000,000,000).
His C (2 <= C <= N) cows don't like this barn layout and become aggressive towards each other once put into a stall. To prevent the cows from hurting each other, FJ want to assign the cows to the stalls, such that the minimum distance between any two of them
is as large as possible. What is the largest minimum distance?
Input
* Line 1: Two space-separated integers: N and C
* Lines 2..N+1: Line i+1 contains an integer stall location, xi
Output
* Line 1: One integer: the largest minimum distance
Sample Input
Sample Output
Hint
OUTPUT DETAILS:
FJ can put his 3 cows in the stalls at positions 1, 4 and 8, resulting in a minimum distance of 3.
Huge input data,scanf is recommended.
题目思路:
给你N个牛舍,C头牛,让牛之间的距离尽可能的大,输出在这种情况下的牛之间的最小距离(最小值的最大)。
代码如下:
题目如下:
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 (0 <= xi <= 1,000,000,000).
His C (2 <= C <= N) cows don't like this barn layout and become aggressive towards each other once put into a stall. To prevent the cows from hurting each other, FJ want to assign the cows to the stalls, such that the minimum distance between any two of them
is as large as possible. What is the largest minimum distance?
Input
* Line 1: Two space-separated integers: N and C
* Lines 2..N+1: Line i+1 contains an integer stall location, xi
Output
* Line 1: One integer: the largest minimum distance
Sample Input
5 3 1 2 8 4 9
Sample Output
3
Hint
OUTPUT DETAILS:
FJ can put his 3 cows in the stalls at positions 1, 4 and 8, resulting in a minimum distance of 3.
Huge input data,scanf is recommended.
题目思路:
给你N个牛舍,C头牛,让牛之间的距离尽可能的大,输出在这种情况下的牛之间的最小距离(最小值的最大)。
代码如下:
#include<stdio.h> #include<algorithm> using namespace std; int n,c,i; long a[100002]; bool fun(int mid) { int cnt=1; int m=a[1]; for(i=1;i<=n;i++) { if(a[i]-m>=mid) { cnt++; m=a[i]; } if(cnt>=c) return 1; } return 0; } int main() { int mid,low,high; scanf("%d%d",&n,&c); for(i=1;i<=n;i++) scanf("%d",&a[i]); sort(a+1,a+n+1); low=0; high=a[n]; while(low+1<high) { mid=(low+high)/2; if(fun(mid)) low=mid; else high=mid; } printf("%d\n",low); }
相关文章推荐
- 2015-2016 ACM-ICPC, NEERC, Southern Subregional Contest F. Gourmet and Banquet(贪心+二分)
- ACM-二分贪心B-02
- ACM-二分贪心D-04
- ACM-二分贪心F-06
- HDU 5515 (ACM 2015 沈阳) Game of Flying Circus [贪心+二分]
- ACM-二分贪心R-18
- ACM-二分贪心T-20
- POJ 1727 Advanced Causal Measurements (ACM) (二分+贪心)
- ACM-二分贪心U-21
- 二分贪心05
- ACM-二分贪心X-24
- 二分贪心--05
- 2016-2017 ACM-ICPC CHINA-Final Ice Cream Tower 二分+贪心
- POJ 1727 Advanced Causal Measurements (ACM)(二分+贪心)
- ACM 贪心 STL multiset 二分查找 hdu4268
- 二分贪心 E - 05
- 2017上海金马五校 购买装备 贪心+二分Check
- 二分+倍增思想 “玲珑杯”ACM比赛 Round #13/B
- UVALive 3177-贪心+二分
- 【HDU5515】【2015ACM/ICPC亚洲区沈阳站】 Game of Flying Circus 二分