10664 - Luggage

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

Moderator: Board moderators

grirus
New poster
Posts: 12
Joined: Sat Mar 13, 2004 6:15 pm
Location: Sampa - Brazil
Contact:

10664 - Luggage

Post by grirus »

Someone can help me.... this is my code and i recept wa???? any critical input ???

thankx
[java]
import java.io.*;
import java.util.*;

public class Main {

static String Readln(int maxLg){
byte [] lin = new byte[maxLg];
int lg = 0, car = -1;
try{
while(lg < maxLg){
car = System.in.read();
if((car<0) || (car == '\n')) break;
lin[lg++] +=car;
}
} catch(IOException e){return null;}

if(car<0 && lg==0) return null;

return(new String(lin,0,lg));

}

static void ordenar(int [] v, int size){
int i,j, max, aux;

for(i=0;i<size;i++){
max = i;
for(j=i+1;j<size;j++)
if(v[j] > v[max]) max = j;
aux = v;
v=v[max];
v[max]=aux;
}

}

public static void main(String[] args) throws IOException{
String linha;

while((linha = Readln(1000))!=null){
long sum = 0;
StringTokenizer dados = new StringTokenizer(linha);
int [] valores = new int[20];
int i = 0;

while(dados.hasMoreTokens()){
valores[i++] = Integer.parseInt(dados.nextToken());
sum+= valores[i-1];
}

if(sum%2==1 || (i==1 && sum!=0)){
System.out.println("NO");
}
else if(sum==0){
System.out.println("YES");
}

else{
ordenar(valores,i);
sum/=2;
if(valores[0]>sum){
System.out.println("NO");
}
else if(valores[0]==sum){
System.out.println("YES");
}
else{
int k;
long result = valores[0];
for(k=1;k<i;k++){
result+=valores[k];
if(result==sum){
System.out.println("YES");
break;
}
else if(result>sum){
result-=valores[k];
}
}
if(k==i){
System.out.println("NO");
}
}
}
}


}
}
[/java]
thanx

grirus
New poster
Posts: 12
Joined: Sat Mar 13, 2004 6:15 pm
Location: Sampa - Brazil
Contact:

...

Post by grirus »

ow mans;... sorry but my input is wrong.......



sorry
thanx

windows2k
Experienced poster
Posts: 136
Joined: Sat Apr 05, 2003 3:29 pm
Location: Taiwan

Re: ...

Post by windows2k »

grirus wrote:ow mans;... sorry but my input is wrong.......



sorry
Hello, I tried to solve the problem.
But get WA all the time.
Could you give me some input/output?
Thx :D

shuniu
New poster
Posts: 34
Joined: Thu Oct 16, 2003 6:15 pm

Post by shuniu »

Input:

Code: Select all

16
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
1 1
2
1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 10
1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10
1 2 3 5 7 11 13 17 23
1 2 3 5 7 11 13 17 23 29
1 2 3 5 7 11 13 17 23 29 31
1 2 3 5 7 11 13 17 23 29 31 37
10 20 30 40 50 50
1 15 25 35 45 55
1 2 3 4 5 6 7 8 9 10 11 12 13 91
1 1 2 3 4 5 6 7 8 9 10 11 12 13 90
1 2 3 4 5 6 7 8 9 10 11 12 13 42 49
1 2 3 4 5 6 7 8 9 10 11 12 13 93
Output:

Code: Select all

YES
NO
YES
NO
YES
YES
YES
NO
YES
NO
YES
NO
YES
YES
YES
NO

grirus
New poster
Posts: 12
Joined: Sat Mar 13, 2004 6:15 pm
Location: Sampa - Brazil
Contact:

Post by grirus »

hey man.. try this..... are simple but interesting :-)

Input:

9 8 7 6 5 4 3 2 1
9 8 7 6 4 3 2 1
4 6
6 4 4 2


output:

NO
YES
NO
YES
thanx

Eduard
Experienced poster
Posts: 183
Joined: Fri Sep 26, 2003 2:54 pm
Location: Armenia,Yerevan

Post by Eduard »

And be carefule for this kind of input.(There is such, becouse I got WA from first time and only after fixing my program for this kind of test case I got AC).
Input

Code: Select all

     1
               <-----Blank line    
Output

Code: Select all

    NO
someone who like to solve informatic problems.
http://acm.uva.es/cgi-bin/OnlineJudge?AuthorInfo:29650

cytmike
Learning poster
Posts: 95
Joined: Mon Apr 26, 2004 1:23 pm
Location: Hong Kong and United States
Contact:

Post by cytmike »

I wanna ask how can I read an unknown number of numbers on the same line in c++?
Thanks!

anupam
A great helper
Posts: 405
Joined: Wed Aug 28, 2002 6:45 pm
Contact:

Post by anupam »

Would you please describe a better algorithm for partitioning into 2 part or m parts?
"Everything should be made simple, but not always simpler"

Eduard
Experienced poster
Posts: 183
Joined: Fri Sep 26, 2003 2:54 pm
Location: Armenia,Yerevan

Post by Eduard »

To Cytmike.
You must read while end of line and remember them on one array.
This is how to read them by Pascal and remember that numbers on array(a[]).
[pascal]
n:=0;
while not eoln do
begin
n:=n+1;
read(a[n]);
end;
readln;
[/pascal]
someone who like to solve informatic problems.
http://acm.uva.es/cgi-bin/OnlineJudge?AuthorInfo:29650

cytmike
Learning poster
Posts: 95
Joined: Mon Apr 26, 2004 1:23 pm
Location: Hong Kong and United States
Contact:

Post by cytmike »

Eduard wrote:To Cytmike.
You must read while end of line and remember them on one array.
This is how to read them by Pascal and remember that numbers on array(a[]).
[pascal]
n:=0;
while not eoln do
begin
n:=n+1;
read(a[n]);
end;
readln;
[/pascal]
Thank you for your help.
But how can I do the "not eoln" function in c++? :roll:

GBY
New poster
Posts: 4
Joined: Wed Apr 07, 2004 6:34 am
Contact:

Post by GBY »

Using cin.getline then sscanf to read numbers from the char array.

cytmike
Learning poster
Posts: 95
Joined: Mon Apr 26, 2004 1:23 pm
Location: Hong Kong and United States
Contact:

Post by cytmike »

GBY wrote:Using cin.getline then sscanf to read numbers from the char array.
oh...
i'm still not clear how to use that
can somebody post a code like the above in pascal explaining that?
:cry:

cypressx
New poster
Posts: 41
Joined: Sun Jul 04, 2004 12:16 pm

Code

Post by cypressx »

Code: Select all

[/cpp]
#include <sstream>
#include <iostream>
#include <string>
using namespace std;
#define maxn 20*200

int a[maxn];
int m; // the size of the array a;
string s; // here will be the line saved

void parse() {
    istringstream iss(s);
    int num;
    m = 0;
    while(iss>>num)  a[m++]=num;
}    
int main() {
     int n; cin >> n;
     for(int i=0;i<n;i++) {
          getline(cin,s);
          parse();
          ....
      }
}

After this code you have all the elements from the line in the array a[];
I hope I helped .

ImLazy
Experienced poster
Posts: 215
Joined: Sat Jul 10, 2004 4:31 pm
Location: Shanghai, China

Post by ImLazy »

Eduard wrote:Input

Code: Select all

1
               <-----Blank line
Output

Code: Select all

NO
Why NO for empty line. If there is nothing to load, two boots certainly load the same weight, 0.

By the way, what means "1
I stay home. Don't call me out.

ImLazy
Experienced poster
Posts: 215
Joined: Sat Jul 10, 2004 4:31 pm
Location: Shanghai, China

Post by ImLazy »

For Eduard:
My program's output for an empty line is "YES", and I got AC.
I stay home. Don't call me out.

Post Reply

Return to “Volume 106 (10600-10699)”