Cod sursa(job #248072)

Utilizator ssergiussSergiu-Ioan Ungur ssergiuss Data 24 ianuarie 2009 19:48:03
Problema Dezastru Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.71 kb
#include<algorithm>
using namespace std;
int n,k,x,st[26];
double s,a[26];
int main(){
    freopen("dezastru.in","r",stdin);
    freopen("dezastru.out","w",stdout);
    int i,vf,aux;
    double p;
    scanf("%d%d",&n,&k);
    for(i=1; i<=n; ++i)
		scanf("%lf",&a[i]);
	sort(a+1,a+n+1);
	for(st[vf=1]=1; vf; ){
		if(vf==k){
            p=1;
            for(i=1; i<=k; ++i)
                p*=a[st[i]];
            s+=p;
            ++x;}
		aux=st[vf];
		if(vf==k){
			if(st[vf]<n)
				++st[vf];
			else if(st[vf]==n){
				--vf;
				while(st[vf]+1>n-k+vf)
					--vf;
				++st[vf];}}
		else if(aux+1<=n-k+vf+1)
			st[++vf]=aux+1;
		else if(aux+1>n-k+vf+1)
			--vf;}
	printf("%lf",s/x);
	return 0;}