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

AC 代码,C++不要用cin

Posted by houzhe at 2022-02-01 09:43:04 on Problem 3579
#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:
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