11057 - Exact Sum

All about problems in Volume 110. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 11057 - Exact Sum

Post by brianfry713 »

After each test case you must print a blank line.
Check input and AC output for thousands of problems on uDebug!
The-PHx
New poster
Posts: 6
Joined: Sat Sep 28, 2013 10:32 pm

Re: 11057 - Exact Sum

Post by The-PHx »

why WA here ?!?
thanks in advance :))

Code: Select all

Accepted :D 
Last edited by The-PHx on Thu Oct 03, 2013 4:08 pm, edited 2 times in total.
brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 11057 - Exact Sum

Post by brianfry713 »

Input:

Code: Select all

2
40 40
80

5
10 2 6 8 4
10

4
4 6 2 8
10

3
1000000 100000 1
1100000
AC output:

Code: Select all

Peter should buy books whose prices are 40 and 40.

Peter should buy books whose prices are 4 and 6.

Peter should buy books whose prices are 4 and 6.

Peter should buy books whose prices are 100000 and 1000000.

Check input and AC output for thousands of problems on uDebug!
?????? ????
New poster
Posts: 7
Joined: Tue Apr 03, 2012 2:34 pm
Location: Manikgonj, Bangladesh

Re: 11057 - Exact Sum

Post by ?????? ???? »

Code: Select all

Remove
Last edited by ?????? ???? on Tue Nov 26, 2013 6:43 pm, edited 1 time in total.
brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 11057 - Exact Sum

Post by brianfry713 »

Try input:

Code: Select all

3
1000000 1 1
2
Check input and AC output for thousands of problems on uDebug!
TLEorWA
New poster
Posts: 12
Joined: Sat Mar 01, 2014 12:56 pm

Re: 11057 - Exact Sum

Post by TLEorWA »

why WA? :(
help, here is my code:

http://pastebin.com/CM4rxcwq

i tried several inputs & outputs are ok,but still WA.

:(
brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 11057 - Exact Sum

Post by brianfry713 »

Try input:

Code: Select all

6
40 40 1 79 39 41
80
Check input and AC output for thousands of problems on uDebug!
Mrsuit
New poster
Posts: 14
Joined: Mon May 05, 2014 8:41 pm

Re: 11057 - Exact Sum

Post by Mrsuit »

I'm gettint WA, and it works for every input that i put.
Thank you.

Code: Select all

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Iterator;
import java.util.PriorityQueue;
import java.util.Queue;


public class Main {
public static void main(String[] args) throws IOException {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
String line;
int i=0;
int j=0;
//Clear the pp


while ((line=bf.readLine())!=null){ // i=test cases
	//list1= prices of the books
	//j= the amount of the sum
	i=Integer.parseInt(line);
	String[] list=new String[i];
	line=bf.readLine();
	list=line.split(" ");
	line=bf.readLine();
	j=Integer.parseInt(line);
	line=bf.readLine();
	int[] list2= new int [i];
	for (int k = 0; k < list.length; k++) {
		list2[k]=Integer.parseInt(list[k]);	
	}
	Arrays.sort(list2);
	
	//list2: numbers sorted 
	
	
	int min=0;
	int max=1000001;
	int f=0;
	int y=0;
	int x=0;
	for (int k2 = 0; k2 < list2.length; k2++) {
		for (int l = k2+1; l < list2.length; l++) {
			f=list2[k2]+list2[l];
			if (f==j){
				x=list2[k2];
				y=list2[l];
				if (Math.abs(min-max)>Math.abs(x-y)){
					min=x;
					max=y;
				}
				}
	}
	}
	if (max!=1000001){
		System.out.println("Peter should buy books whose prices are" +" " + min + " " + "and" +" " + max );
		System.out.println();
	}
	else {
		System.out.println("Peter should buy books whose prices are" +" " + 0 + " " + "and" +" " + 0);
		System.out.println();
	}
	
		
	pp.clear();
}
		
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	

}
}
brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 11057 - Exact Sum

Post by brianfry713 »

That code doesn't compile:
Main.java:66: error: cannot find symbol
pp.clear();
^
symbol: variable pp
location: class Main
1 error

You're also missing the . at the end of each line.
Check input and AC output for thousands of problems on uDebug!
Mrsuit
New poster
Posts: 14
Joined: Mon May 05, 2014 8:41 pm

Re: 11057 - Exact Sum

Post by Mrsuit »

brianfry713 wrote:That code doesn't compile:
Main.java:66: error: cannot find symbol
pp.clear();
^
symbol: variable pp
location: class Main
1 error

You're also missing the . at the end of each line.
I fixed thoses things, but i'm still getting WA.

Code: Select all

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Iterator;
import java.util.PriorityQueue;
import java.util.Queue;


public class Main {
public static void main(String[] args) throws IOException {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
String line;
StringBuilder sb = new StringBuilder();
int i=0;
int j=0;
while ((line=bf.readLine())!=null && !line.trim().equals("")){ // i=test cases
   //list1= prices of the books
   //j= the amount of the sum
   i=Integer.parseInt(line);
   String[] list=new String[i];
   line=bf.readLine();
   list=line.split(" ");
   line=bf.readLine();
   j=Integer.parseInt(line);
   line=bf.readLine();
   int[] list2= new int [i];
   for (int k = 0; k < list.length; k++) {
      list2[k]=Integer.parseInt(list[k]);   
   }
   Arrays.sort(list2);
   
   //list2: numbers sorted 
   
   
   int min=0;
   int max=1000001;
   int f=0;
   int y=0;
   int x=0;
   for (int k2 = 0; k2 < list2.length; k2++) {
      for (int l = k2+1; l < list2.length; l++) {
         f=list2[k2]+list2[l];
         if (f==j){
            x=list2[k2];
            y=list2[l];
            if (Math.abs(min-max)>Math.abs(x-y)){
               min=x;
               max=y;
            }
            }
   }
   }
   
   if (max!=1000001){
      sb.append("Peter should buy books whose prices are" +" " + min + " " + "and" +" " + max +"."+"\n\n" );
         }
   else {
      sb.append("Peter should buy books whose prices are" +" " + 0 + " " + "and" +" " + 0+"."+"\n\n");
     
   }
   
  }
System.out.println(sb.substring(0,sb.length()-2));
}
      
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   

}
brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 11057 - Exact Sum

Post by brianfry713 »

After each test case (including the last one) you must print a blank line.
Check input and AC output for thousands of problems on uDebug!
Mrsuit
New poster
Posts: 14
Joined: Mon May 05, 2014 8:41 pm

Re: 11057 - Exact Sum

Post by Mrsuit »

brianfry713 wrote:After each test case (including the last one) you must print a blank line.

Lol, finally got AC, thank you so much! :)
uDebug
A great helper
Posts: 475
Joined: Tue Jul 24, 2012 4:23 pm

Re: 11057 - Exact Sum

Post by uDebug »

Don't forget to input the period (".") at the end of the sentence.
Check input and AC output for over 7,500 problems on uDebug!

Find us on Facebook. Follow us on Twitter.
tausiftt5238
New poster
Posts: 2
Joined: Thu Aug 14, 2014 2:25 pm

Re: 11057 - Exact Sum

Post by tausiftt5238 »

Code: Select all

#include <stdio.h>
#include <stdlib.h>
int compare(const void *a, const void *b)
{
	return ( *(int*)a - *(int*)b );
}
int main()
{
	int in[100000];
	int N,i,j,result,flag = 0;
	while(scanf("%d",&N)==1)
	{
		if(flag == 1)
			printf("\n");
		flag = 1;
		for(i=0;i<N;i++)
		{
			scanf("%d",&in[i]);
		}
		qsort(in,N,sizeof(int),compare);
		scanf("%d",&result);
		for(i=(N/2);i<N;i++)
		{
			for(j=i-1;j>=0;j--)
			{
				if(in[i]+in[j] == result)break;
			}
			if(in[i]+in[j] == result)break;
		}
		printf("Peter should buy books whose prices are %d and %d.\n",in[j],in[i]);
	}
	return 0;
}
got wa :\ i was expecting TLE..
anyway...
anyone have any idea why ?
lighted
Guru
Posts: 587
Joined: Wed Jun 11, 2014 9:56 pm
Location: Kyrgyzstan, Bishkek

Re: 11057 - Exact Sum

Post by lighted »

After each test case you must print a blank line.
Try this

Code: Select all

5
1 2 3 4 5
3
got wa :\ i was expecting TLE..
It seems that judges input are weak. I changed two lines in your code and got accepted but it was wrong solution. :D
With current judge's input - solutions with O(N * N) is accepted. My solution is in O(N * logN)
A person who sees the good in things has good thoughts. And he who has good thoughts receives pleasure from life... Bediuzzaman
Post Reply

Return to “Volume 110 (11000-11099)”