Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

Binary Search on Max Length 125MS

Posted by __ea at 2016-11-06 13:34:45 on Problem 2456
#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:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator