D - Binomial Coefficients
Time limit : 2sec / Memory limit : 256MB
Score : <var>400</var> points
Problem Statement
Let <var>comb(n,r)</var> be the number of ways to choose <var>r</var> objects from among <var>n</var> objects, disregarding order. From <var>n</var> non-negative integers <var>a1,a2,…,an</var>, select two numbers <var>ai>aj</var> so that <var>comb(ai,aj)</var> is maximized. If there are multiple pairs that maximize the value, any of them is accepted.
Constraints
- <var>2≤n≤105</var>
- <var>0≤ai≤109</var>
- <var>a1,a2,…,an</var> are pairwise distinct.
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
<var>n</var> <var>a1</var> <var>a2</var> <var>…</var> <var>an</var>
Output
Print <var>ai</var> and <var>aj</var> 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;
}