10801 - Lift Hopping

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

Moderator: Board moderators

User avatar
Abednego
A great helper
Posts: 281
Joined: Tue Sep 10, 2002 5:14 am
Location: Mountain View, CA, USA
Contact:

Post by Abednego » Sun Jan 30, 2005 7:36 pm

You can read a line of input with getline() and then parse the line using a stringstream.
The following program reads lines of input, adds all the numbers on that line and outputs the sum.

Code: Select all

#include <iostream>
#include <sstream>
using namespace std;
int main()
{
    string line;
    while( getline( cin, line ) )
    {
        istringstream in( line );
        int sum = 0, x;
        while( in >> x ) sum += x;
        printf( "%d\n", sum );
    }
    return 0;
}
If only I had as much free time as I did in college...

Zuberul
New poster
Posts: 28
Joined: Sun Oct 24, 2004 9:46 pm
Location: dhaka
Contact:

Post by Zuberul » Sun Jan 30, 2005 9:37 pm

get the lines by string & learn how to use the C library function strtok()
that will help.

sqm
New poster
Posts: 3
Joined: Sun Jan 30, 2005 2:18 pm
Location: Bialystok, POLAND

Post by sqm » Mon Jan 31, 2005 12:54 pm

Thank you all. I think strtok() is easier and faster way to read.

I've written code which in my opinion is good (I got good answers on my computer) and I submitted it but I got Runtime Error (SIGSEGV).
I think the problem is the way I read data because I deleted all additional code and send only part that should read input. I got SIGSEGV again. Does anybody know what's wrong with it:

char string[255], token[]=" ", *res;
for(int i=0,a,b;i<=e;i++){
gets(string);
res=strtok(string, token);
while(1){
a=atoi(res);
res=strtok(NULL, token);
if(res==NULL) break;
b=atoi(res);
//...
}
}

abductor
New poster
Posts: 8
Joined: Sat Jan 18, 2003 6:43 pm
Location: Canada

need more tricky I/O for 10800 plz

Post by abductor » Thu Feb 03, 2005 11:41 pm

could u provide me with some more tricky I/O for 10800, I keep getting WA and I can't see y
thanx a lot

abductor
New poster
Posts: 8
Joined: Sat Jan 18, 2003 6:43 pm
Location: Canada

10801 - Lift Hopping

Post by abductor » Fri Feb 04, 2005 4:17 pm

could u provide me with some more tricky I/O for 10801, I keep getting WA and I can't see y
thanx a lot

michaelh
New poster
Posts: 2
Joined: Thu Feb 03, 2005 9:04 pm

Post by michaelh » Fri Feb 04, 2005 5:02 pm

me too!

I tried every tricky input i could think of. Including k=0, non stopping elevators, taking elevators up- and downward, ... I'm using a modified dijkstra to add the time when changing elevators. And my program is correct for all example input and previously posted examples in this board.


Michael

User avatar
Abednego
A great helper
Posts: 281
Joined: Tue Sep 10, 2002 5:14 am
Location: Mountain View, CA, USA
Contact:

Post by Abednego » Fri Feb 04, 2005 6:43 pm

The most common mistake people make is to not handle this input correctly:

Code: Select all

3 1
100 10 100
0 1
0 1
0 1
PS. Also, it would be easier if you could keep all the posts for one problem in one thread.
If only I had as much free time as I did in college...

abductor
New poster
Posts: 8
Joined: Sat Jan 18, 2003 6:43 pm
Location: Canada

Post by abductor » Fri Feb 04, 2005 8:51 pm

I am pretty sure its not the input problem, unless of course input has some blank lines in it coz what I do is:
while( cin >> n >> t )
{
////
.
.
.
/////

for( i = 0; i < n; i++ )
{
cin >> speed;
}

// read end of the line character and get to the next line
getline(cin, s);

// read line by line
for( i = 0; i < n; i++ )
{
// read the whole line
getline(cin, s);

.
.
//////////////////////////////////

I think I am missing something else...

abductor
New poster
Posts: 8
Joined: Sat Jan 18, 2003 6:43 pm
Location: Canada

Post by abductor » Tue Feb 08, 2005 11:37 pm

anyone can help? any more test cases? please?
thanx

michaelh
New poster
Posts: 2
Joined: Thu Feb 03, 2005 9:04 pm

Post by michaelh » Wed Feb 09, 2005 12:54 am

You should test whether your graph code can store all edges. That was my mistake and now i got accepted.

Guest
New poster
Posts: 39
Joined: Wed May 19, 2004 5:52 pm
Location: Dhaka, Bangladesh
Contact:

Post by Guest » Sun Apr 03, 2005 8:08 pm

I've used Dijkstra's algorithm, but always getting WA.
Are there empty lines possibly filled with spaces in arbitrary positions in judge's input?
Please help :(

User avatar
Abednego
A great helper
Posts: 281
Joined: Tue Sep 10, 2002 5:14 am
Location: Mountain View, CA, USA
Contact:

Post by Abednego » Thu Apr 14, 2005 7:17 am

Each line in the input contains at least one integer. Make sure you handle the ground floor correctly. Does your code work when no elevator stops there, when the wrong elevator stops there, when all elevators stop there?
If only I had as much free time as I did in college...

Guest
New poster
Posts: 39
Joined: Wed May 19, 2004 5:52 pm
Location: Dhaka, Bangladesh
Contact:

Post by Guest » Thu Apr 14, 2005 8:07 pm

Sorry for I forgot to edit my earlier post, I've got AC several days ago.. :D
..Thanks for the reply, anyway.

hts
New poster
Posts: 8
Joined: Sat Feb 22, 2003 2:56 am

About I/O

Post by hts » Sun May 01, 2005 8:38 am

It is really anoying to read this kind of input in C, I did this:

Code: Select all

c = 0;
while(c != '\n' && scanf("%d%c", &j, &c) == 2)
	acesso[i][j] = 1;
acesso[i][j] = 1; 
But I got WA, I used Dijkstra as well... I think there are spaces left after the last number in each line.

User avatar
Abednego
A great helper
Posts: 281
Joined: Tue Sep 10, 2002 5:14 am
Location: Mountain View, CA, USA
Contact:

Post by Abednego » Sun May 01, 2005 8:48 am

Use gets() or fgets() and then parse the line using scanf. Also, there are no spaces at the end of any line in the judge's input.
If only I had as much free time as I did in college...

Post Reply

Return to “Volume 108 (10800-10899)”