POJ 2456 Aggressive cows
来源:互联网 发布:endless legend mac 编辑:程序博客网 时间:2024/06/03 16:38
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
OutputLine 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.
Source
USACO 2005 February Gold
通過二分實現最大化最小值。
思路通過二分的方式來不斷壓縮最小值的可取區間,然後通過貪心的思想去檢查。
#include<iostream>#include<cstring>#include<algorithm>using namespace std;#define INF 0x3f3f3f3fint N, M;int x[100005];bool C(int d){ int last = 0; for (int i = 1; i < M; i++) { int crt = last + 1; while (crt < N && (x[crt] - x[last]) < d) { crt++; } if (crt == N) return false; last = crt; } return true;}void solve(){ sort(x, x + N); int lb = 0, ub = INF; while ((ub - lb) > 1) { int mid = (lb + ub) / 2; if (C(mid)) lb = mid; else ub = mid; } cout << lb << endl;}int main(){ while (cin >> N >> M) { for (int i = 0; i < N; i++) { cin >> x[i]; } solve(); } return 0;}
- Poj 2456 Aggressive cows
- POJ 2456 Aggressive cows
- poj 2456 Aggressive cows
- poj 2456 Aggressive cows
- POJ-2456-Aggressive cows
- POJ 2456 Aggressive cows
- POJ-2456 Aggressive cows
- poj 2456 Aggressive cows
- POJ 2456 Aggressive cows
- POJ 2456 Aggressive cows
- poj 2456 Aggressive cows
- POJ 2456 Aggressive cows
- POJ 2456 Aggressive cows
- POJ-2456 Aggressive cows
- POJ-2456Aggressive cows
- POJ 2456 - Aggressive cows
- POJ 2456 Aggressive cows
- poj 2456 Aggressive cows
- 【C++】【LeetCode】105. Construct Binary Tree from Preorder and Inorder Traversal
- Javascript设计模式-18-状态模式
- React Native入门(十)之导航组件React Navigation(2)TabNavigator和DrawerNavigator
- 解决easyui和bootstrap兼容问题
- mysql性能优化(利用索引优化查询速度)
- POJ 2456 Aggressive cows
- 盒子的计算方式
- D3.js-基础知识
- 逻辑架构和物理架构
- 标准W3C盒子模型和IE盒子模型(错了无数次)
- Linux任务计划
- python
- HDU
- win10上安装socket.io