Cod sursa(job #476876)

Utilizator c_adelinaCristescu Adelina c_adelina Data 12 august 2010 15:54:13
Problema Loto Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.94 kb
#include <cstdio>
#include <algorithm>
#define N1 1000000
#define N2 1000
using namespace std;

struct nod
{
	int nr;
	int val;
} s[1000002];

int cmp(const nod &x,const nod &y)
{return x.val<y.val;}


int main()
{
	int c,n,i,j,z,m=0,sum,v[101];
	freopen("loto.in","r",stdin);
	freopen("loto.out","w",stdout);
	scanf("%d %d",&n,&sum);
	for (i=1;i<=n;++i)
	{
		scanf("%d",&v[i]);
		for (j=1;j<=i;++j)
			for (z=1;z<=j;++z)
				++m,s[m].nr=z*N1+j*N2+i,s[m].val=v[i]+v[j]+v[z];
	}
	sort(s+1,s+m+1,cmp);
	n=sum/2+1;
	for (i=1;i<=m && s[i].val<n;++i)
	{
		c=-1;
		int x=i,y=m;
		for  (z=(x+y)/2;x<=y;z=(x+y)/2)
			if (s[z].val<sum-s[i].val)
				x=z+1; else
					if (s[z].val>sum-s[i].val)
						y=z-1; else
							c=z,x=y+1;
		if (c>0)
		{
			printf("%d %d %d %d %d %d",s[i].nr/N1,s[i].nr%N1/N2,s[i].nr%N1%N2,s[c].nr/N1,s[c].nr%N1/N2,s[c].nr%N1%N2);
			break;
		}
	}
	if (c<0) printf("-1");
	
			
		
	
	return 0;}