Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
Binary Search on Max Length 125MS#include <iostream> #include <cstdio> #include <algorithm> #include <cstdlib> using namespace std; int arr[100001], N, C; bool pos(int len) { int prev = arr[1], cow = 1; for (int i = 2; i <= N && cow < C; i++) if (arr[i] - prev >= len) prev = arr[i], cow++; return cow == C; } int main () { scanf("%d", &N); scanf("%d", &C); for (int i = 1; i <= N; i++) scanf("%d", &arr[i]); sort(arr, arr+1+N); // Binary Search int high = arr[N], low = 0; while(high - low > 1) { int middle = (high + low)/2; pos(middle)? low=middle : high=middle; } cout << low << endl; return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator