AgPro

导航

P1064

#include <iostream>
#include <cmath>
//#include <fstream>
using namespace std;

//ifstream in("in.txt");
//#define cin in

const int MAXN = 10001;
int n,k;
int a[MAXN],v[2*MAXN];

int compare_ints( const void* a, const void* b ) 
{   
	int* arg1 = (int*) a;   
	int* arg2 = (int*) b;   
	if( *arg1 < *arg2 ) 
		return -1;   
	else if( *arg1 == *arg2 )
		return 0;   
	else 
		return 1; 
}  

int main()
{
	int suma,sumx,maxval;
	int i,j;
	double tmpin;


	cin >> n >> k;
	
	suma = 0;
	for ( i=1; i<=n; i++ )
	{
		cin >> tmpin;
		a[i] = (int)(tmpin*100);
		suma += a[i];
	}

	v[0] = 0;
	for ( i=1; i<=n; i++ )
	{
		v[2*i-1] = (int)floor((double)a[i]/suma*k);//根据百分比*k,找到可能的ni值,要么上取整,要么下取整
		v[2*i] = (int)ceil((double)a[i]/suma*k);
	}
	for ( i=1; i<=n; i++ )
	{
		if (v[2*i-1])
			v[2*i-1] = (int)floor((double)a[i]/v[2*i-1]);//根据ni值,计算val值
		if (v[2*i])
			v[2*i] = (int)floor((double)a[i]/v[2*i]);
	}
	
	qsort(v,2*n+1,sizeof(int),compare_ints);//所有可能val值从小大排序

	maxval = 0;
	for ( i=2*n; i>=1; i-- )
	{
		if ( v[i] == v[i-1] )
			continue;
		sumx = 0;
		for ( j=1; j<=n; j++ )
			sumx += (int)(a[j]/v[i]);
		if ( sumx >= k )//找到最大的val值,使得n1+n2+n3..+nn==k
		{
			maxval = v[i];
			break;
		}
	}
	cout.setf(ios::fixed);
	cout.setf(ios::showpoint);
	cout.precision(2);
	cout << maxval/100.0 << endl;

	return 0;
}

posted on 2010-07-09 17:20  AgPro  阅读(288)  评论(0编辑  收藏  举报