POJ 2456 Aggressive cows (二分 基础)
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 7924 | Accepted: 3959 |
Description
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
* Lines 2..N+1: Line i+1 contains an integer stall location, xi
Output
Sample Input
5 3
1
2
8
4
9
Sample Output
3
Hint
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.
Source
题目链接:
id=2456">http://poj.org/problem?
id=2456
题目大意:一个数轴上n个点,每一个点一个整数值,有c个物品。要放在这些点的某几个上,求怎么放能够使随意两个物品间距离的最小值最大,求这个最大值
题目分析:最小值最大,典型二分题。二分距离的值推断
#include <cstdio>
#include <algorithm>
using namespace std;
int const INF = 0x3fffffff;
int const MAX = 1e5 + 5;
int d[MAX];
int n, c; int cal(int m)
{
int ans = 0, now = d[0];
for(int i = 1; i < n; )
{
while(d[i] < now + m)
i ++;
ans ++;
now = d[i];
}
return ans;
} int main()
{
scanf("%d %d", &n, &c);
for(int i = 0; i < n; i++)
scanf("%d", &d[i]);
sort(d, d + n);
int r = d[n - 1] - d[0], l = 0, ans = 0;
while(l <= r)
{
int m = (l + r) / 2;
int num = cal(m);
if(num >= c)
{
ans = m;
l = m + 1;
}
else
r = m - 1;
}
printf("%d\n", ans);
}
POJ 2456 Aggressive cows (二分 基础)的更多相关文章
- POJ 2456 Aggressive cows(二分答案)
Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 22674 Accepted: 10636 Des ...
- POJ - 2456 Aggressive cows 二分 最大化最小值
Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 18099 Accepted: 8619 ...
- poj 2456 Aggressive cows 二分 题解《挑战程序设计竞赛》
地址 http://poj.org/problem?id=2456 解法 使用二分逐个尝试间隔距离 能否满足要求 检验是否满足要求的函数 使用的思想是贪心 第一个点放一头牛 后面大于等于尝试的距离才放 ...
- [poj 2456] Aggressive cows 二分
Description Farmer John has built a new long barn, with N (2 <= N <= 100,000) stalls. The stal ...
- [POJ] 2456 Aggressive cows (二分查找)
题目地址:http://poj.org/problem?id=2456 最大化最小值问题.二分牛之间的间距,然后验证. #include<cstdio> #include<iostr ...
- POJ 2456 Aggressive cows ( 二分 && 贪心 )
题意 : 农夫 John 建造了一座很长的畜栏,它包括N (2 <= N <= 100,000)个隔间,这些小隔间依次编号为x1,...,xN (0 <= xi <= 1e9) ...
- poj 2456 Aggressive cows && nyoj 疯牛 最大化最小值 二分
poj 2456 Aggressive cows && nyoj 疯牛 最大化最小值 二分 题目链接: nyoj : http://acm.nyist.net/JudgeOnline/ ...
- 二分搜索 POJ 2456 Aggressive cows
题目传送门 /* 二分搜索:搜索安排最近牛的距离不小于d */ #include <cstdio> #include <algorithm> #include <cmat ...
- POJ 2456 Agressive cows(二分)
POJ 2456 Agressive cows 农夫 John 建造了一座很长的畜栏,它包括N (2≤N≤100,000)个隔间,这 些小隔间的位置为x0,...,xN-1 (0≤xi≤1,000,0 ...
随机推荐
- Dependency Injection in ASP.NET Web API 2
What is Dependency Injection? A dependency is any object that another object requires. For example, ...
- link 和 runtime-link,搭配shared 和 static(转)
原文转自 http://blog.csdn.net/yasi_xi/article/details/8660549 参考: http://bbs.sjtu.edu.cn/bbscon,board,C, ...
- Selenium2+python自动化(unittest)
# coding:utf-8from selenium import webdriverimport unittestimport timeclass Bolg(unittest.TestCase): ...
- malloc和free函数的使用
#include <stdio.h> #include <stdlib.h> int main() { int *p,t; p = (int *)malloc(40*sizeo ...
- LeetCode OJ--Word Break II ***@
https://oj.leetcode.com/problems/word-break-ii/ class Solution { public: unordered_set<string> ...
- Java平台下的gitignore文件
*.bak*.txt*.vm.gitignore#svn.svn/# built application files*.apk*.ap_ # files for the dex VM*.dex # J ...
- (5)php数组
定义数组 $arr=array('篮球','自行车','海贼王'); 打印指定数组 echo $arr[0]; 打印全部数组 print_r($arr); 改变数组的值 $arr[0]='足球'; 赋 ...
- 页面css代码
博主原来的页面css代码 (这个是原来的那种效果,差不多弄出来会是这种效果http://www.cnblogs.com/thmyl/) /*simplememory*/ #google_ad_c1, ...
- 关于npm无法安装依赖包以及安装包缓慢的解决方法
因为npm的服务器在国外,导致我们使用npm安装第三方包缓慢.而且有的第三方包是被墙的. 因此,作为墙内人,必须解决这个问题,否则开发起来实在是太坑了! 推荐大家使用淘宝的镜像(cnpm),它以每10 ...
- Akka之Actor生命周期
我们首先来看一下官方给出的Actor的声明周期的图: 在上图中,Actor系统中的路径代表一个地方,其可能会被活着的Actor占据.最初路径都是空的.在调用actorOf()时,将会为指定的路径分配根 ...