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 |
c++AC。参考《挑战程序设计竞赛》143页,二分搜索方法实现。#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: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator