| ||||||||||
| 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 | |||||||||
AC 代码,C++不要用cin#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <string>
#include <sstream>
#include <math.h>
#include <string.h>
#include <algorithm>
#include <numeric>
#include <deque>
#include <climits>
using namespace std;
typedef long long ll;
// const double M_PI = acos(-1.0);
// const double E = 2.71828182845904523536029;
int c2(int num) {
return num* (num - 1) / 2;
}
int calcIndex(vector<int>& v, int val) {
int index = 0;
for (int i = 0; i < (v.size() - 1); i++) {
int testVal = val + v[i];
vector<int>::iterator iter = upper_bound(v.begin() + i, v.end(), testVal);
int num = iter - (v.begin() + i) - 1;
index += num;
}
return index - 1;
}
int main() {
int n;
while (scanf("%d", &n) != EOF) {
vector<int> num(n);
for (int i = 0; i < n; i++) {
scanf("%d", &num[i]);
}
int medianIndex = (c2(n) - 1) / 2;
sort(num.begin(), num.end());
int l = 0, r = num.back() - num[0];
int ans = 0x7fffffff;
while (l <= r) {
int m = (l + r) / 2;
int index = calcIndex(num, m);
if (index < medianIndex) {
l = m + 1;
}
else {
ans = min(ans, m);
r = m - 1;
}
}
cout << ans << endl;
}
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator