Page 5 of 6

Posted: Thu Apr 13, 2006 5:20 am
by smilitude
I dont know, why i used long long, i tried to figure out all the possibilites to get ac.

Posted: Thu Apr 13, 2006 9:45 am
by little joey
You can't read and write long longs with "%d", you should use "%lld" instead.
Alternatively replace all your long longs by ints.
Either way, you'll get accepted.

Posted: Sat Apr 15, 2006 9:53 am
by smilitude
shame on me! :oops:

Posted: Tue Jun 13, 2006 10:09 am
by asif_rahman0
hi similitude

your can simplify ur qsort().

Code: Select all

qsort(num,n,sizeof(int),(int(*)(const void *,const void *)) comp);

int comp(const int *i,const int *j){return *i-*j;}

i think its much easier to code.
:)

Posted: Thu Aug 10, 2006 10:05 pm
by vinit_iiita

hi freinds i got re twice i cannot figure out why plz help me...

Code: Select all

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
    int n;
    int j=1;
    while (cin>>n && n!=0)
    {
          int a[n];
          for (int i=0;i<n;i++) 
          cin>>a[i];
          
          vector<long long int> v;
          for (int i=0;i<n;i++)
          {
              for (int k=i+1;k<n;k++)
              {
                  if (a[i]!=a[k])
                  {int t=a[i]+a[k];
                   v.push_back(t);
                   }
                  }
                  }
                  sort(v.begin(),v.end());
         
             
          int nq;
          cin>>nq;
          cout<<"Case "<<j<<":"<<endl;
          for (int i=0;i<nq;i++)
          {
              long long int q;
              cin>>q;
              int k=0;
               while (v[k]<q)
               {
                     k++;
               }
           
            if ((v[k]-q)<=(q-v[k-1]) || k==0)
            cout<<"Closest sum to "<<q<<" is "<<v[k]<<"."<<endl;
            else
            cout<<"Closest sum to "<<q<<" is "<<v[k-1]<<"."<<endl;
           
               }
               j++;
               }          
              return 0;
              }
              
              
          
          
thanx in advance..[/b]

Posted: Fri Aug 17, 2007 5:12 am
by WingletE
Got WA too.
Could anybody help me, please?
I've passed all the test cases I found...

Code: Select all

removed after AC

Re: 10487 - Closest Sums

Posted: Tue Jul 01, 2008 9:33 pm
by smilitude
for this input

Code: Select all

3
3
5
5
1
10
the output should be

Code: Select all

Case 1:
Closest sum to 10 is 10.
not this

Code: Select all

Case 1:
Closest sum to 10 is 8.
and after correcting that your code will fail for this

Code: Select all

3
5
6
5
1
20
which should be

Code: Select all

Case 1:
Closest sum to 20 is 11.
:) Best wishes!

EDIT :: even if your program outputs 8 for the first input, you'll get AC! :P that means, all the inputs will be distinct, so there will be no such inputs!

Re: 10487 - Closest Sums

Posted: Sun Jul 20, 2008 8:34 pm
by alamgir kabir

Code: Select all

code removed after acc

Re: 10487 - Closest Sums

Posted: Fri Jul 25, 2008 10:27 am
by newton
I tried all test case but and output is okey but WA.
Why? I need some more input.
here is my code:

Code: Select all

#include <stdio.h>
#include <algorithm>
#define INF 1<<29
#define MAX 1001


using namespace std;

long int array[MAX];
long int sum[MAX*(MAX-1)/2];

long int BSearch(long int N,long int key){
	long int beg = 0;
	long int end = N;
	long int mid = (beg + end)/2;
	
	while(end >= beg){
		if(sum[mid] == key)
			break;
		if(sum[mid] < key)
			beg = mid + 1;
		else
			end = mid - 1;
		mid = (beg + end)/2;
	}				
	return mid;	
}

int main(){
	long int N,Q,query,i,j,value,c = 1;
	//freopen("in.txt","rt",stdin);
	
	while(scanf("%ld",&N)==1 && N){
		for(i = 0; i< N; i++)
			scanf("%ld",&array[i]);
		int k = 0;
		for(i = 0; i < N; i++){
			for(j = i + 1; j < N; j++)
				sum[k++] = array[i] + array[j];
		}
		sort(sum,sum+k);
		
		scanf("%ld",&Q);
		printf("Case %ld:\n",c++);
		for(i = 0; i < Q; i++){
			scanf("%ld",&query);				
			value = BSearch(k-1,query);			
			printf("Closest sum to %ld is %ld.\n",query,sum[value]);
		}
	}
	return 0;
}

Re: 10487 - Closest Sums

Posted: Thu Jul 31, 2008 8:56 am
by linux
BSearch function in your code is not correct. Think about it & judge this case:

Code: Select all

3
1
7
10
1
16
Try to think about correctness of your algorithms and segments first when writing programs. Hence, generating Input/Output set for yourself is easier than others.
Hope it helps.

Re: 10487 - Closest Sums

Posted: Thu Aug 14, 2008 10:41 pm
by newton
For that case what would be the output?

Code: Select all

3
1
2
4
1
4
is it 3 or 5?

Re: 10487 - Closest Sums

Posted: Mon Aug 18, 2008 2:29 am
by kbr_iut
My AC gives

Code: Select all

Case 1:
Closest sum to 4 is 3.
uvatoolkit gives

Code: Select all

Case 1:
Closest sum to 4 is 5.
that means both of them r correct.dont b tensed about that.

note: If u need output for any input set go to this link.
http://uvatoolkit.com/
it generates output for any input set of uva problems.
hope it helps

Re: 10487 - Closest Sums

Posted: Mon Aug 18, 2008 8:44 am
by newton
I am sorry.
There was a line in problem specification that no input will be given for wich there is duplicate answer.
Thank you.

Re: 10487 - Closest Sums

Posted: Mon Oct 06, 2008 7:54 pm
by scor_k
i m getting always rte. please ....help me.please,,please


#include<stdio.h>
#include<math.h>
#define X 1000000
int main()
{

long int n,m,arr[X],brr[X],i,j,p,t,s,a,b,sum,sub,crr[X],l,q,r,cas=0;

while(1)
{
scanf("%ld",&n);

cas++;

if(n==0)
break;


for(i=0;i<n;i++)
{

scanf("%ld",&arr);
}

scanf("%ld",&m);

for(j=0;j<m;j++)
{
scanf("%ld",&brr[j]);
}

t=0;


for(a=0;a<n;a++)
{
for(b=a+1;b<n;b++)
{

if(arr[a]!=arr)
sum=arr[a]+arr;

crr[t]=sum;
t++;

}


}

printf("Case %ld:\n",cas);

for(l=0;l<m;l++)
{
s=X;
for(q=0;q<t;q++)
{
sub=(crr[q]-brr[l]);

if(sub<0)
{

sub=sub*(-1);
}


if(s>sub)
{
s=sub;
r=crr[q];
}

}

printf("Closest sum to %ld is %ld.\n",brr[l],r);

}


}
return 0;
}

Re: 10487 - Closest Sums

Posted: Mon Oct 06, 2008 9:22 pm
by mf
It doesn't seem to me like a good idea to keep many-megabyte-long arrays on the stack. Make them static, global, or allocate memory for them from the heap (with malloc).