nyoj-586-疯牛|poj-2456-Aggressive cows
http://acm.nyist.net/JudgeOnline/problem.php?pid=586
http://poj.org/problem?id=2456
解题思路:最大化最小值二分答案即可
1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4
5 int a[];
6 int n, c, i;
7
8 int cmp(const void *a, const void *b){
9 return *(int *)a - *(int *)b;
}
bool check(int x){
int cnt = , temp = a[];
for(i = ; i < n; i++){
if(a[i] - temp > x){
cnt++;
temp = a[i];
if(cnt >= c){//如果能装下c头牛
return true;
}
}
}
return false;
}
void solve(){
//二分搜索最大的距离
int l = , r = a[n - ] - a[];
while(l <= r){
int mid = (l + r) >> ;
if(check(mid)){
l = mid + ;
}
else{
r = mid - ;
}
}
printf("%d\n", l);
}
int main(){
while(scanf("%d %d", &n, &c) != EOF){
for(i = ; i < n; i++){
scanf("%d", &a[i]);
}
qsort(a, n, sizeof(a[]), cmp);
solve();
}
return ;
49 }
nyoj-586-疯牛|poj-2456-Aggressive cows的更多相关文章
- 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 Aggressive cows (二分 基础)
Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7924 Accepted: 3959 D ...
- POJ 2456 Aggressive cows (二分)
题目传送门 POJ 2456 Description Farmer John has built a new long barn, with N (2 <= N <= 100,000) s ...
- [ACM] poj 2456 Aggressive cows (二分查找)
Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5436 Accepted: 2720 D ...
- POJ 2456 Aggressive cows
Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11192 Accepted: 5492 ...
- 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 贪心+二分
Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 25944 Accepted: 11982 ...
- POJ 2456 Aggressive cows(贪心 + 二分)
原题链接:Aggressive cows 题目大意:农夫 建造了一座很长的畜栏,它包括 个隔间,这些小隔间依次编号为. 但是, 的 头牛们并不喜欢这种布局,而且几头牛放在一个隔间里,他们就要发生争 ...
随机推荐
- c++指针参数是如何传递内存的
参数策略 如果函数的参数是一个指针,不要指望用该指针去动态申请内存.如下: void GetMemory(char *p, int num) { p = (char *)malloc(sizeof(c ...
- JSP | 基础 | 中文字符的传递
设置传递格式: request.setCharacterEncoding("utf-8"); 中文字符解码: import java.net.URLDecoder; String ...
- (洛谷 P1429 平面最近点对(加强版) || 洛谷 P1257 || Quoit Design HDU - 1007 ) && Raid POJ - 3714
这个讲的好: https://phoenixzhao.github.io/%E6%B1%82%E6%9C%80%E8%BF%91%E5%AF%B9%E7%9A%84%E4%B8%89%E7%A7%8D ...
- re 模块的重新整理
RE模块 import re 的常用操作 查找 1. findall : ret = re.findall('\d+'.'sjkhk172按实际花费928') #正则表达式,待匹配的字符串,flag ...
- 关于pycharm的一个imoprt的随笔
近来发现pycharm一个方便的地方,from 文件夹名.组件名 import 方法(引入和父级目录同级文件夹下的模块),在pycharm中这种引用是能够成功的,但是在python自带的IDLE中不能 ...
- TDH-kafka
一.kafka原理:1.broker:一个kafka实例,多个broker组成kafka集群: 2.topic:逻辑概念,同一类数据的集合,可以看做一张表: 3.producer:将数据写入topic ...
- Bingding模型
public abstract class Binding : IDefaultCommunicationTimeouts public virtual IChannelListener<TCh ...
- Spring Boot常用配置
概述 本文主要写了下Spring Boot的一些常用配置. Spring Boot基本配置 入口类: Spring Boot通常有一个名为*Application的入口类,入口类里面有一个main方法 ...
- Java编程简介
作者:CHAITANYA SINGH 来源:https://www.koofun.com//pro/kfpostsdetail?kfpostsid=3 JAVA由Sun Microsystems In ...
- linux安装redis官方教程
官方链接:http://redis.io/download Download, extract and compile Redis with: $ wget http://download.redis ...