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

c++AC。参考《挑战程序设计竞赛》143页,二分搜索方法实现。

Posted by 15642244402 at 2018-12-13 19:07:28
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

static const int INF = 1000000000;

bool valid(int d, const vector<int>& A, int c) {
	int s = 0, e;
	for (int i = 1; i < c; ++i) {
		e = s + 1;
		while (e < A.size() && A[e] - A[s] < d)
			++e;
		if (e == A.size()) return false;
		s = e;
	}
	return true;
}

int main() {

	int n, c;
	cin >> n >> c;
	vector<int> A(n);
	for (int i = 0; i < n; ++i)
		cin >> A[i];
	sort(A.begin(), A.end());

	int m, l = 1, r = INF;
	while (l < r) {
		m = (l + r) >> 1;
		if (valid(m, A, c)) l = m;
		else r = m - 1;
	}

	cout << l << 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