299 - Train Swapping

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

Moderator: Board moderators

Rene
New poster
Posts: 13
Joined: Mon May 05, 2003 4:40 am
Location: Shanghai,China

Post by Rene » Sun May 11, 2003 11:00 am

2 is the right answer?
why?
i think 3 is the right answer. And my code get the answer 3.

Hisoka
Experienced poster
Posts: 120
Joined: Wed Mar 05, 2003 10:40 am
Location: Indonesia

Post by Hisoka » Sun May 11, 2003 12:17 pm

I know your program will produce 3 for this input. and the right output is 2.
this is that proses:

1 3 4 2 5

proses 1 : 1 3 2 4 5
proses 2 : 1 2 3 4 5

end of proses.

GOOD LUCK.......

Rene
New poster
Posts: 13
Joined: Mon May 05, 2003 4:40 am
Location: Shanghai,China

Post by Rene » Tue May 13, 2003 5:19 am

oh,i see.
and i have changed a new way to slove this problem and got accepted.
Thanx. :D

User avatar
kenny1har
New poster
Posts: 7
Joined: Fri May 09, 2003 2:30 am

299 - can anyone help me

Post by kenny1har » Thu May 22, 2003 3:53 pm

I don't understand why my program is wrong
please give any comments....


[pascal]
program p229 (input,output);
type
aset=array[1..50] of integer;
var
a:aset;
n,nn,train,j,k:integer;

function bsort(var a:aset) :integer;
var
j,k,temp,n:integer;
begin
n:=0;
for j:=nn downto 2 do
for k:=1 to nn-1 do
if a[k] > a[k+1] then begin
n:=n+1;
temp:=a[k];
a[k]:=a[k+1];
a[k+1]:=temp;
end;
bsort:=n;
end;

begin
while not eof(input) do begin
readln(n);
if eof(input) then exit;
for j:=1 to n do begin
readln(nn);
if eof(input) then exit;
for k:=1 to nn do begin
read(train);
if eof(input) then exit;
a[k]:=train;
end;
readln;
writeln('Optimal train swapping takes ',bsort(a),' swaps.');
end;
end;
end.
[/pascal]

User avatar
cytse
Learning poster
Posts: 67
Joined: Mon Sep 16, 2002 2:47 pm
Location: Hong Kong
Contact:

Post by cytse » Thu May 22, 2003 5:09 pm

i think there is no need to check eof after reading each number.

in fact, i got AC with your program after all statements related to eof(input) are removed.

User avatar
kenny1har
New poster
Posts: 7
Joined: Fri May 09, 2003 2:30 am

thanks cytse, it works !!!!

Post by kenny1har » Fri May 23, 2003 1:59 pm

thanks for your comments.

jai166
New poster
Posts: 10
Joined: Mon May 12, 2003 11:10 am

Why 299 WA?

Post by jai166 » Mon Jul 28, 2003 11:33 am

Why 299 WA? I can't figure it out!!!
[c]/* 299 */
#include<stdio.h>
#define MAX 95
void main(void)
{
unsigned int maxnum,i,j,pass,maxtimes,times,n[MAX],temp;

scanf("%u",&maxtimes);
for(;maxtimes;maxtimes--){
scanf("%d",&maxnum);
for(i=0;i<maxnum;i++)scanf("%u",&n);
times=0,pass=1;
for(j=maxnum-1;j&&pass;j--)
for(pass=0,i=0;i<j;i++)
if(n>n[i+1]){
times++,pass=1;
temp=n[i+1];n[i+1]=n;n=temp;
}
printf("Optimal train swapping takes %u swaps.\n",times);
}
}[/c]
Last edited by jai166 on Tue Jul 29, 2003 9:36 am, edited 2 times in total.

User avatar
shamim
A great helper
Posts: 498
Joined: Mon Dec 30, 2002 10:10 am
Location: Bozeman, Montana, USA

Post by shamim » Mon Jul 28, 2003 12:29 pm


unsigned int maxnum,i,j,pass,maxtimes,times,n[MAX],temp;

scanf("%d",&maxtimes);
you have declared maxtimes to be of type unsigned int, but your are using '%d'.

jai166
New poster
Posts: 10
Joined: Mon May 12, 2003 11:10 am

Sorry, Time Limit Exceeded...

Post by jai166 » Tue Jul 29, 2003 9:45 am

I've changed %d to %u, but the result cganged to "Time Limit Exceeded". I use "Bubble Sort" and save how much times it count!
I know bubble sort isn't a powerful sort, but a friend of mine use this way to solve it in the past, and he solved then. But when I transfered his codes yesterday, the result is "Time Limit Exceeded"
Does someone know how to figure the times but use less time? Could you help me?Thank you!!!

Observer
Guru
Posts: 570
Joined: Sat May 10, 2003 4:20 am
Location: Hong Kong

Post by Observer » Tue Jul 29, 2003 10:36 am

BubbleSort is efficient enough for this question.

Of course, if you really want to optimize your code, you may use MergeSort :D
7th Contest of Newbies
Date: December 31st, 2011 (Saturday)
Time: 12:00 - 16:00 (UTC)
URL: http://uva.onlinejudge.org

Joseph Kurniawan
Experienced poster
Posts: 136
Joined: Tue Apr 01, 2003 6:59 am
Location: Jakarta, Indonesia

Post by Joseph Kurniawan » Tue Jul 29, 2003 4:26 pm

Well, I think first you should locate the train with number n. Hopefully by locating the trains, the time will be reduced. Example:
5 trains:
3 4 1 5 2
We expect the train will be 1 2 3 4 5.
1. We want to arrange the train 1 (currently in pos 3) become:
3 4 1 5 2 -> 3 1 4 5 2 -> 1 3 4 5 2
2. Now train no 2 (currently in the last position):
1 3 4 5 2 -> 1 3 4 2 5 -> 1 3 2 4 5 -> 1 2 3 4 5
Well, since the trains are already in order, we don't have to proceed.
Hope it helps !! :wink: :wink:
[/c]

jai166
New poster
Posts: 10
Joined: Mon May 12, 2003 11:10 am

It's been Accepted!!

Post by jai166 » Thu Jul 31, 2003 11:23 am

Well, I just changed "unsigned int" to "int" ,
"scanf("%u",&maxtimes);"
into
"while(scanf("%d",&maxtimes)==1)"

Then, it accepted! :D

boyeric
New poster
Posts: 6
Joined: Sun Jun 20, 2004 5:08 pm

WA 299..... somebody help???

Post by boyeric » Fri Jun 25, 2004 9:35 pm

I thought it should be an easy one, but i don't know why i didn't get through.....
i used the bubble sort and count the steps of sorting, is this algorithm all right for problem 299?? Thanks in advance! here is my code: (in Java)

[java]
import java.util.*;

class Main{
public static void main(String [] args){
String in;
int c, i, size, j, k, t, n;
c = Integer.parseInt(Main.readLine());
StringTokenizer st;
int[] a;
for (i=0 ; i<c ; i++){
if (i!=0)System.out.println();
n=0;
size = Integer.parseInt(Main.readLine());
a = new int [size];
in = Main.readLine();
st = new StringTokenizer(in);
for (j=0 ; j<size; j++){
a[j] = Integer.parseInt(st.nextToken());
}
for (j=0 ; j<size-1 ; j++){
for (k=0 ; k<size-1-j ; k++){
if (a[k]>a[k+1]){
t = a[k];
a[k] = a[k+1];
a[k+1] = t;
n++;
}
}
}
System.out.print("Optical train swapping takes " + n + " swaps.");
}
}

static String readLine(){return token( "\n\r" );}


/* read token from stdIn with standard delims */
static String token( ){return token( " \n\r\t" );}


/* read token from stdIn with custom delims */
/* returns null for end of file or any exceptions */
static String token( String delim ){
char c = delim.charAt(0);
StringBuffer s = new StringBuffer("");
try{
while( delim.indexOf( (int) c ) != -1 && c != 65535 )
c = (char) System.in.read();
while( delim.indexOf( (int) c ) == -1 && c != 65535 ){
s.append( (char) c );
c = (char) System.in.read();
}
}catch( Exception e ){ return (null); }
if( s.toString().equals("") ) return null;
return s.toString();
}

}[/java]

minskcity
Experienced poster
Posts: 199
Joined: Tue May 14, 2002 10:23 am
Location: Vancouver

Post by minskcity » Mon Jun 28, 2004 2:04 am

Your algorithm is good for this problems, I got AC using the same approach. Check your code for reading the input one more time, try to replace the core of your code with something like:[cpp] while(flag){
flag = false;
for(long i = 0; i < n - 1; i++)
if(data > data[i + 1]){
swap(data, data[i + 1]);
ans++;
flag = true;
}
}[/cpp]yes, it's is slower in some cases, but it's accepted :wink: It's better to write more robust then faster code before you get AC.
Hope this will help. :-?

Robert
New poster
Posts: 2
Joined: Sun Jul 04, 2004 2:39 pm
Location: Germany

Post by Robert » Sat Jul 10, 2004 11:57 am

Did you allready correct the following mistake:

[java]System.out.print("Optical train swapping takes " + n + " swaps."); [/java]

It's not the "Optical train swapping " that you are looking for - but the
"Optimal"...

Robert

Post Reply

Return to “Volume 2 (200-299)”