11057 - Exact Sum
Moderator: Board moderators
-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
Re: 11057 - Exact Sum
After each test case you must print a blank line.
Check input and AC output for thousands of problems on uDebug!
Re: 11057 - Exact Sum
Last edited by The-PHx on Thu Oct 03, 2013 4:08 pm, edited 2 times in total.
-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
Re: 11057 - Exact Sum
Input:AC output:
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
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
Code: Select all
Remove
Last edited by ?????? ???? on Tue Nov 26, 2013 6:43 pm, edited 1 time in total.
-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
Re: 11057 - Exact Sum
Try input:
Code: Select all
3
1000000 1 1
2
Check input and AC output for thousands of problems on uDebug!
Re: 11057 - Exact Sum
why WA?
help, here is my code:
http://pastebin.com/CM4rxcwq
i tried several inputs & outputs are ok,but still WA.


help, here is my code:
http://pastebin.com/CM4rxcwq
i tried several inputs & outputs are ok,but still WA.

-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
Re: 11057 - Exact Sum
Try input:
Code: Select all
6
40 40 1 79 39 41
80
Check input and AC output for thousands of problems on uDebug!
Re: 11057 - Exact Sum
I'm gettint WA, and it works for every input that i put.
Thank you.
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();
}
}
}
-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
Re: 11057 - Exact Sum
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.
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!
Re: 11057 - Exact Sum
I fixed thoses things, but i'm still getting WA.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.
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));
}
}
-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
Re: 11057 - Exact Sum
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!
Re: 11057 - Exact Sum
brianfry713 wrote:After each test case (including the last one) you must print a blank line.
Lol, finally got AC, thank you so much!

-
- New poster
- Posts: 2
- Joined: Thu Aug 14, 2014 2:25 pm
Re: 11057 - Exact Sum
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;
}
anyway...
anyone have any idea why ?
Re: 11057 - Exact Sum
Try thisAfter each test case you must print a blank line.
Code: Select all
5
1 2 3 4 5
3
It seems that judges input are weak. I changed two lines in your code and got accepted but it was wrong solution.got wa :\ i was expecting TLE..

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