D - Binomial Coefficients
Time limit : 2sec / Memory limit : 256MB
Score : 400 points
Problem Statement
Let comb(n,r) be the number of ways to choose r objects from among n objects, disregarding order. From n non-negative integers a1,a2,…,an, select two numbers ai>aj so that comb(ai,aj) is maximized. If there are multiple pairs that maximize the value, any of them is accepted.
Constraints
- 2≤n≤105
- 0≤ai≤109
- a1,a2,…,an are pairwise distinct.
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
n a1 a2 … an
Output
Print ai and aj that you selected, with a space in between.
Sample Input 1
5 6 9 4 2 11
Sample Output 1
11 6
题意:给定一些个数,求出comb(a[i],a[j])的最大值,其中a[j]>a[i]
思路:
组合数存在性质:comb(n + 1, m) > comb(n, m)
证明如下:comb(n + 1, m) = comb(n, m) + comb(n, m − 1) 杨辉三角形
那么必定有,n=maxelement(Array),m-> maxelement(Array)/2。即Comb(n,m)为最大值
时间复杂度:O(n) or O(nlogn)
#include<bits/stdc++.h>
#define PI acos(-1.0)
#define pb push_back
#define F first
#define S second
using namespace std;
typedef long long ll;
const int N=2e5+5;
const int MOD=1e9+7;
ll a[N];
int main(void){
// ios::sync_with_stdio(false);
// cin.tie(0);cout.tie(0);
int n;
cin >> n;
for(int i=1;i<=n;++i) cin >> a[i];
sort(a+1,a+1+n);//写了个stl,sort偷懒,也可以O(n)扫一遍找最大值
ll ans,dis=1e18;
for(int i=1;i<=n-1;i++){
if(a[n]%2==0){//偶数的中位数是a[n]/2
if(abs(a[i]-a[n]/2)<dis) ans=a[i],dis=abs(a[i]-a[n]/2);
}
else{奇数的中位数是a[n]/2+1或者是a[n]/2
if(abs(a[i]-a[n]/2)<dis) ans=a[i],dis=abs(a[i]-a[n]/2);
if(abs(a[i]-a[n]/2-1)<dis) ans=a[i],dis=abs(a[i]-a[n]/2-1);
}
}
cout << a[n] <<" "<<ans<<endl;
return 0;
}