Page 5 of 6
Posted: Wed Mar 01, 2006 2:26 am
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.....

Posted: Wed Mar 01, 2006 2:50 am
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

### help 299 WA

Posted: Thu Jan 25, 2007 1:52 pm
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;
}

Posted: Mon Jan 29, 2007 9:54 pm

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

### thank you

Posted: Wed Feb 07, 2007 4:31 pm
thank you very much

i got AC

thanks

but i think that it should give me presentation error not wrong answer

### 299-WA!!!

Posted: Fri Jun 06, 2008 12:05 pm
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???

### Re: 299-WA!!!

Posted: Fri Jun 06, 2008 12:31 pm
Search the board first. Don't open duplicate threads.

Posted: Sat Jul 31, 2010 9:21 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;
}

Posted: Sat Jul 31, 2010 4:07 pm
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....

Posted: Mon Aug 02, 2010 2:32 pm
i got AC

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

Posted: Tue Aug 27, 2013 2:11 pm

Posted: Tue Aug 27, 2013 10:38 pm
From uhunt:
dibery> Inzamam_hstu, just count the number of inversions. You don't need to swap.

Posted: Thu Nov 28, 2013 4:45 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;
}

Posted: Tue Dec 03, 2013 2:13 am
That is AC code

Posted: Tue Feb 11, 2014 7:21 pm
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();
}
}``````