## 299 - Train Swapping

Moderator: Board moderators

yogeshgo05
New poster
Posts: 47
Joined: Sun Nov 27, 2005 12:43 pm
hi tisarkar...
should we swap..because we should consider number of swaps needed
because the o/p says number of swaps only...

if i have a wrong conception plz help me out .... thanks a lot man for the

still i get wa i dont no y plz help.....
yogeshgo05
New poster
Posts: 47
Joined: Sun Nov 27, 2005 12:43 pm
HI tiskar..
thanks man i got it ac but i still do not know y we need to swap manually.
because number of swaps is required is found as an o/p......plz help
Mr.south
New poster
Posts: 11
Joined: Thu Jan 25, 2007 1:45 pm

### help 299 WA

this is my source code, i don't know where gets wrong. it always give me WA. somebody help me, please. i have considered the 0<= L <=50.

#include <stdio.h>

int train[50] = {0};

int bubble_sort(int length);
void swap(int *a, int*b);

int main()
{
int i, j, L;
int N, optimal;

scanf("%d", &N);
for(i=0; i<N; i++)
{
scanf("%d", &L);
if(L>=1)
{
for(j=0; j<L; j++)
{
scanf("%d", &train[j]);
}
optimal = bubble_sort(L);
printf("Optimal train swapping takes %d swaps\n", optimal);
for(j=0; j < 50; j++)
{
train[j] = 0;
}
}
else
{
printf("Optimal train swapping takes 0 swaps\n");
}
}

return 0;
}

int bubble_sort(int length)
{
int counter = 0;
int i, j;

for(i=0; i<length-1; i++)
{
for(j=i+1; j<length;j++)
{
if(train > train[j])
{
swap(&train, &train[j]);
counter++;
}
}
}

return counter;
}

void swap(int *a, int *b)
{
int temp;

temp = *a;
*a = *b;
*b = temp;
}
Debashis Maitra
Learning poster
Posts: 62
Joined: Sun Jul 09, 2006 8:31 am
Location: University of Dhaka
Contact:

Code: Select all

``````........
printf("Optimal train swapping takes %d swaps\n", optimal);
..................
printf("Optimal train swapping takes 0 swaps\n");
``````
To

Code: Select all

``````........
printf("Optimal train swapping takes %d swaps.\n", optimal);
..................
printf("Optimal train swapping takes 0 swaps.\n");
``````
Because problem says there should be full stop before the end of the line

Best of luck
Akash chhoyar swopno
Dream to touch the sky
Mr.south
New poster
Posts: 11
Joined: Thu Jan 25, 2007 1:45 pm

### thank you

thank you very much

i got AC

thanks

but i think that it should give me presentation error not wrong answer
scorpio
New poster
Posts: 2
Joined: Fri Jun 06, 2008 11:48 am
Contact:

### 299-WA!!!

i cant find out any mistake in my code..
here is the code...

#include<stdio.h>
int main()
{
int a[1000],i,p,flag,c,n,b=0,count,t;
scanf("%d",&c);
while(scanf("%d",&n)==1)
{
for(i=0;i<n;i++)
scanf("%d",&a);
count=0;
for(i=1;i<n;i++)
{
flag=0;
p=0;
while(p<n-i)
{
if(a[p]>a[p+1])
{
t=a[p];
a[p]=a[p+1];
a[p+1]=t;
flag=1;
count++;
}
p++;
}
if(flag==0)
break;
}
b++;
if(count>0)
printf("Optimal train swapping takes %d swaps.\n",count);
if(count==0)
printf("Optimal train swapping takes 0 swap.\n");
if(b==c)
break;
}
return 0;
}

can anybody tell me my mistake???

Jan
Guru
Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm
Contact:

### Re: 299-WA!!!

Search the board first. Don't open duplicate threads.
Ami ekhono shopno dekhi...
HomePage
New poster
Posts: 44
Joined: Thu Jul 22, 2010 9:42 am

#include<iostream>

using namespace std;

int main()
{
//freopen("trainswapping.txt","r",stdin);
//freopen("trainswappingoutput.txt","w",stdout);

unsigned long int i,c,j,ks,num;
char val[10],t;
cin>>ks;
while(ks)
{
cin>>num;

for(i=0;i<num;i++)
{
cin>>t;
val=t-48;

}

c=0;
for(i=1;i<num;i++)
{
for(j=0;j<num-i;j++)
{
if(val[j]>val[j+1]);
{
t=val[j];
val[j]=val[j+1];
val[j+1]=t;
c++;
}
}
}
/*for(i=0;i<num;i++){val+=48;
cout<<val;}
cout<<endl;*/

cout<<"Optimal train swapping takes "<<c<<" swaps."<<endl;
ks--;
}
return 0;
}
shaon_cse_cu08
New poster
Posts: 50
Joined: Tue May 25, 2010 9:10 am
Contact:

Try to mantain the condition's given in the problem.... "0<=L<=50"

Dont Open new Tread.... Just Search for the problem in board index....
I'll keep holding on...Until the walls come tumbling down...And freedom is all around .....
New poster
Posts: 44
Joined: Thu Jul 22, 2010 9:42 am

i got AC
KaziInzamamMahmud
New poster
Posts: 9
Joined: Sun Jul 21, 2013 2:34 pm

### Re: 299 Train Swapping ( TIME LIMIT )

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

From uhunt:
dibery> Inzamam_hstu, just count the number of inversions. You don't need to swap.
Check input and AC output for thousands of problems on uDebug!
reza_cse08
New poster
Posts: 8
Joined: Sun Nov 17, 2013 9:55 pm

/************************************299_train_swaping.cpp*************************************/
#include<iostream>
#include<cstdio>
using namespace std;

int main()
{
int n, m, a[1000]={0}, i, j, k;
cin>>n;
while(n--)
{
scanf("%d",&k);
for(i = 0;i<k;i++)
{
scanf("%d",&a);
}
m =0;
for(i =0;i<k;i++)
{
for(j=i+1;j<k;j++)
{
if(a>a[j])
{
int tt = a;
a = a[j];
a[j] = tt;
m++;
}
}
}
printf("Optimal train swapping takes %d swaps.\n",m);
}

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

That is AC code
Check input and AC output for thousands of problems on uDebug!
piyukr
New poster
Posts: 17
Joined: Sun Jan 26, 2014 10:35 am

This gives the correct answer to all the test cases. Also, it works fine if the length of the train is 0. But on submission I got RE .Can someone help me know the possible reason for the RE.

Code: Select all

``````
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;

class Main {

public static void main(String[] args) throws IOException {
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
while (t-- > 0) {
String[] s = null;
int count = 0;
int i = 0;
int a[] = new int[size];
if(size > 0)
while (i < size) {
a[i] = Integer.parseInt(s[i++]);
}
int j, temp;
for (i = 1; i < size; i++) {
j = i;
while (j > 0 && (a[j] < a[j - 1])) {
temp = a[j];
a[j] = a[j - 1];
a[j - 1] = temp;
count++;
j--;
}
}
bw.write("Optimal train swapping takes " + count + " swaps\n");
}
bw.close();
}
}``````