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

yogeshgo05
New poster
Posts: 47
Joined: Sun Nov 27, 2005 12:43 pm

Post by yogeshgo05 »

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
reply... :lol:


still i get wa i dont no y plz help.....
yogeshgo05
New poster
Posts: 47
Joined: Sun Nov 27, 2005 12:43 pm

Post by yogeshgo05 »

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

Post by Mr.south »

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:

Post by Debashis Maitra »

change your output

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

remove your code after AC
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

Post by Mr.south »

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
Location: Dhaka,Bangladesh
Contact:

299-WA!!!

Post by scorpio »

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??? :(

thnx in advance
Jan
Guru
Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm
Location: Dhaka, Bangladesh
Contact:

Re: 299-WA!!!

Post by Jan »

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

299 why worng answer? please help me

Post by @mjad »

#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:

Re: 299 why worng answer? please help me

Post by shaon_cse_cu08 »

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.... :roll:
I'll keep holding on...Until the walls come tumbling down...And freedom is all around ..... :x
@mjad
New poster
Posts: 44
Joined: Thu Jul 22, 2010 9:42 am

Re: 299 why worng answer? please help me

Post by @mjad »

thanks for your reply
i got AC :D
brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 299 why worng answer? please help me

Post by brianfry713 »

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 why worng answer? please help me

Post by reza_cse08 »

/************************************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

Re: 299 why worng answer? please help me

Post by brianfry713 »

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

Re: 299 why worng answer? please help me

Post by piyukr »

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.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

class Main {

    public static void main(String[] args) throws IOException {
	BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
	BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
	int t = Integer.parseInt(br.readLine());
	while (t-- > 0) {
	    String[] s = null;
	    int size = Integer.parseInt(br.readLine());
	    int count = 0;
	    int i = 0;
	    int a[] = new int[size];
	    if(size > 0)
		s = br.readLine().trim().split(" +");
	    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();
    }
}
Post Reply

Return to “Volume 2 (200-299)”