DC poj 1064 Cable master
来源:互联网 发布:c语言自学要多久 编辑:程序博客网 时间:2024/06/14 08:19
题意是说有N条绳子,长度分别为Li,现在要从这N条绳子中产生K条长度相同的绳子,求这个最长长度。
算法是二分查找值算法,用一个函数C(x)判断是不是满足某种约定,然后根据判断结果不断更新这个区间
,最后得到这个最长长度,用二分法的话思想很简单,实现起来很容易。
#include<iostream>#include<algorithm>#define INF 9999999using namespace std;int N, M;int x[100001];//judge if it isbool C(int d){int pre = 0;for (int i = 1; i < M; i++){int cur = pre + 1;while (cur < N&&x[cur] - x[pre] < d){cur++;}if (cur == N){return false;}pre = cur;}return true;}int main(){while (cin >> N >> M){//inputfor (int i = 0; i < N; i++){cin >> x[i];}//sort the array xsort(x, x + N);int lb = 0, ub = INF;for (int i = 0; i < 100; i++){int mid = (lb + ub) / 2;if (C(mid)){lb = mid;}else{ub = mid;}}cout << lb << endl;}return 0;}
0 0
- DC poj 1064 Cable master
- poj 1064 Cable master
- poj 1064 Cable master
- poj 1064 Cable master
- POJ 1064 Cable master
- poj 1064 Cable master
- POJ-1064-Cable master
- poj 1064--Cable master
- POJ 1064 Cable master
- POJ 1064 Cable master
- poj 1064 Cable master
- poj 1064 Cable master
- POJ(1064)cable master
- POJ 1064 Cable master
- cable master POJ 1064
- POJ 1064 Cable master
- POJ 1064 Cable Master
- POJ-1064 Cable master
- 杭电 HDU 1164 Eddy's research I
- 库函数API和C代码中嵌入汇编代码两种方式使用同一个系统调用
- java8 lambda表达式
- Java之美[从菜鸟到高手演变]之设计模式三
- 中文开发者技术文档(Cocos,Cocos2d-X,Cocos2d-JS)
- DC poj 1064 Cable master
- “斐波那契查找”真的比“二分查找”快么?
- pat_1009
- 堆栈详解
- 1.基本语言
- 内核驱动学习(一)
- 《Accelerate C++》读书笔记(三)
- Spring 注解学习
- 你利用好网络这个工具了吗?