POJ —— 二分
来源:互联网 发布:3d场景软件 编辑:程序博客网 时间:2024/05/20 15:41
Aggressive cows
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 4898 Accepted: 2415
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?
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
* 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 312849
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.
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
题意是有n间牛棚,在a数组中告诉你他们的位置,有M头牛,你要把他们放得尽量远,输出最小间隔的数量。
思路就是我去对最小距离d二分。calc(d)表示安排牛的位置使得最近的两头牛都小于d,也就是所有的两牛间距小于d。
#include <cstdio>#include <cmath>#include <algorithm>#include <iostream>#include <cstring>#include <map>#include <string>#include <stack>#include <cctype>#include <vector>#include <queue>#include <set>#include <utility>#include <cassert>using namespace std;///#define Online_Judge#define outstars cout << "***********************" << endl;#define clr(a,b) memset(a,b,sizeof(a))#define lson l , mid , rt << 1#define rson mid + 1 , r , rt << 1 | 1#define mk make_pair#define FOR(i , x , n) for(int i = (x) ; i < (n) ; i++)#define FORR(i , x , n) for(int i = (x) ; i <= (n) ; i++)#define REP(i , x , n) for(int i = (x) ; i > (n) ; i--)#define REPP(i ,x , n) for(int i = (x) ; i >= (n) ; i--)const int MAXN = 100000 + 5;const int MAXS = 10000 + 50;const int sigma_size = 26;const long long LLMAX = 0x7fffffffffffffffLL;const long long LLMIN = 0x8000000000000000LL;const int INF = 0x7fffffff;const int IMIN = 0x80000000;const int inf = 1 << 30;#define eps 1e-8const long long MOD = 1000000000 + 7;const int mod = 100000;typedef long long LL;const double PI = acos(-1.0);typedef double D;typedef pair<int , int> pii;#define Bug(s) cout << "s = " << s << endl;///#pragma comment(linker, "/STACK:102400000,102400000")int n , m ,a[MAXN];bool calc(int d){ int las = 0; for(int i = 1 ; i < m ; i++) { int cur = las + 1; while(cur < n && a[cur] - a[las] < d) { cur++; } if(cur == n)return false; las = cur; } return true;}void solve(){ sort(a , a + n); int L = 0 , R = inf; while(R - L > 1) { int mid = (L + R) >> 1; if(calc(mid))L = mid; else R = mid; } printf("%d\n" , L);}int main(){ while(~scanf("%d%d" , &n , &m)) { for(int i = 0 ; i < n ; i++) { scanf("%d" , &a[i]); } solve(); } return 0;}
- POJ —— 二分
- POJ 1064 —— 二分
- 二分图匹配——POJ 3057
- POJ 3273 Monthly Expense——二分
- 二分——Poj 3273 Monthly Expense
- 二分——POJ 3579 Median
- POJ 3104 Drying ——二分
- POJ 3258(二分——最大化最小值)
- POJ 3258(二分——最大化最小值)
- poj 2296 (2—SAT+二分)
- POJ-2536—Gopher II(二分图)
- USACO 4.2.2 && POJ 1274 —— 二分图匹配
- POJ 2239 —— 二分图最大匹配
- POJ 2318 —— TOYS(叉积+二分)
- 二分图最大匹配——POJ 1469
- 二分图最小点覆盖数——POJ 3041
- 二分图最小路径覆盖——POJ 3020
- 二分图最大独立集——POJ 2771
- Android导入自定义的jar包时出现 E/AndroidRuntime(486): java.lang.NoClassDefFoundError错误
- 前端开发 调试工具 Chrome Workspace
- 顺序表+单链表+双链表
- 一个有趣的面试题
- 如何在技术面试中脱颖而出(上)
- POJ —— 二分
- WMI技术介绍和应用——查询正在运行的进程信息
- 博客开通
- Java 各种编译错误
- php 实现网页得分业显示
- vc异步套接字
- Java 7 Fork/Join 并行计算框架概览
- 数组
- Library API reference