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

triker
New poster
Posts: 5
Joined: Tue Apr 19, 2005 9:56 am

### Please Help! 299 WA

i searched the forum but i can't find what's wrong with my code

Code: Select all

``````#include <iostream>

using namespace std;

int main()
{
int testCase, nCase=0;
int length;
int temp;

cin >> testCase;

while(nCase < testCase)
{
cin >> length;
int train[length];

for(int i=0;i<length;i++)
{
cin >> train[i];
}

int x=0;
int swap=0;
for(int i=0;i<length;i++,x++)
{
for(int j=0;j<length-x;j++)
{
if(train[j]>train[j+1])
{
temp = train[j];
train[j]=train[j+1];
train[j+1]=temp;
swap++;
}
}
}
cout << "Optimal train swapping takes " << swap << " swaps." << endl;
nCase++;
}

return 0;
}``````

sohel
Guru
Posts: 856
Joined: Thu Jan 30, 2003 5:50 am
Location: New York

### ooopsiii daisy..

assign the initial value of x as 1 and not 0..

ie
x = 1; // correct
x = 0; // incorrect.

triker
New poster
Posts: 5
Joined: Tue Apr 19, 2005 9:56 am
thanks i missed a very little mistake Jemerson
Learning poster
Posts: 59
Joined: Mon Feb 02, 2004 11:19 pm
Contact:
Hi everyone, ive posted in all 299 related topics about one interesting thing that bored me a lot, and gave me some WA'S! For sure there are EMPTY lines between the number of test cases and the first train length, dont know about between each test case, but it is also valid an attention on this too.
UFCG Brazil - Computer Science graduate student
http://acm.uva.es/problemset/usersnew.php?user=54806 ... and going up!

Jemerson
Learning poster
Posts: 59
Joined: Mon Feb 02, 2004 11:19 pm
Contact:
Hi everyone, ive posted in all 299 related topics about one interesting thing that bored me a lot, and gave me some WA'S! For sure there are EMPTY lines between the number of test cases and the first train length, dont know about between each test case, but it is also valid an attention on this too.
UFCG Brazil - Computer Science graduate student
http://acm.uva.es/problemset/usersnew.php?user=54806 ... and going up!

Artikali
Learning poster
Posts: 68
Joined: Wed Sep 21, 2005 5:27 pm

### 299 problem compile error

Please help me compile error, but in VC++.NEt compiled OK
here is my code:

Code: Select all

``````#include <stdio.h>
#include <iostream>
using namespace std;
int bubblesort(int *a,int tsize)
{
int k=0;
for (int i=tsize-1;i>0;i--)
for (int j=0;j<i;j++)
if (a[j]>a[j+1]) {
swap(a[j],a[j+1]);
k++;
}
return k;
}
int main(){
int t,n, a,p;

scanf("%d",&t);
for (int i=0; i<t;i++)
{
scanf("%ld",&n);
for (int j=0;j<n;j++) scanf("%ld",&a[j]);
p=bubblesort(a,n);
printf("Optimal train swapping takes %d swaps.\n",p);
}
return 0;
}``````
Thanks

chunyi81
A great helper
Posts: 293
Joined: Sat Jun 21, 2003 4:19 am
Location: Singapore
You need to #include <algorithm> if you want to use the swap function.

These are the errors I got from compiling your program:
p299.cpp: In function `int bubblesort(int *, int)':
p299.cpp:10: implicit declaration of function `int swap(...)'

Visual C++ .NET automatically includes the necessary headers for you but for g++ compilier in the UVA, it doesn't. You need to explicitly include the header libraries you need.

saifposinc
New poster
Posts: 1
Joined: Sun Oct 30, 2005 12:44 pm
Contact:

### "911" : Problem with 299 (freaking judge shows WA) What's the problem with my code ?
Why show WA ?
Plz help!!

Here's the code :

#include<iostream>
#define MAX 50
using namespace std;
int srt(int a[],int n);
main(){
int i,j,num,x,a[MAX];
cin>>x;
for(j=0;j<x;j++){
cin>>num;
for(i=0;i<num;i++)
cin>>a[i];
cout<<"Optimal train swapping takes "<<srt(a,num)<<" swaps\n";
}
return 0;
}
int srt(int a[],int n){
int i,j,c=0;
for(i=0;i<n-1;i++){
for(j=0;j<n-1;j++){
if(a[j]>a[j+1]){
int temp;
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
c++;
}
}
}
return c;
}[/b]

Jan
Guru
Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm
Location: Dhaka, Bangladesh
Contact:
Change this line...

Your code...

Code: Select all

``cout<<"Optimal train swapping takes "<<srt(a,num)<<" swaps\n"; ``
Should be..

Code: Select all

``cout<<"Optimal train swapping takes "<<srt(a,num)<<" swaps.\n"; ``
Missing the full stop.. Ami ekhono shopno dekhi...
HomePage

worm3959
New poster
Posts: 4
Joined: Tue Nov 29, 2005 5:49 am
Contact:

### risky?

saifposinc wrote:Change this line. But is that not a bit risky?

wes
New poster
Posts: 4
Joined: Sat Feb 04, 2006 3:05 pm

### 299. Compile error. why? This is OK whith g++.

Why I'm getting CE, with this code??

Code: Select all

``````/* @JUDGE_ID: ******* 299 C++ */
#include <iostream>
using namespace std;

int Swaps(int L, int carriages[]){
int count(0);

for(int i=L-1; i>0; i--){
for(int j = i; j>0; j--){
if(carriages[j] < carriages[j-1]){
int temp = carriages[j-1];
carriages[j-1] = carriages[j];
carriages[j] = temp;
count++;
}
}
}
for(int i=0; i<L-1; i++){
for(int j = i; j<L-1; j++){
if(carriages[j] > carriages[j+1]){
int temp = carriages[j+1];
carriages[j+1] = carriages[j];
carriages[j] = temp;
count++;
}
}
}
return count;
}

int main () {
int tests;
cin >> tests;

while(tests != 0){
int L, swaps;
int train;
cin >> L;
for(int i = 0,; i < L; i++)
cin >> train[i];
swaps = Swaps(L, train);
cout << "Optimal train swapping takes " << swaps <<  " swaps." << endl;
tests--;
}
return 0;
}

``````
Thanks in advance!
Last edited by wes on Sun Feb 05, 2006 1:45 am, edited 1 time in total.

mamun
A great helper
Posts: 286
Joined: Mon Oct 03, 2005 1:54 pm
Location: Bangladesh
Contact:
wes wrote:This is OK whith g++
Don't tell me you passed any compilation with this line (in main())

Code: Select all

``for(int i = 0,; i < L; i++)``

wes
New poster
Posts: 4
Joined: Sat Feb 04, 2006 3:05 pm
mamun wrote:
wes wrote:This is OK whith g++
Don't tell me you passed any compilation with this line (in main())

Code: Select all

``for(int i = 0,; i < L; i++)``
i really don't know how but...
:~\$ g++ -o 299 299.cpp
:~\$ ./299 <299.in
Optimal train swapping takes 1 swaps.
Optimal train swapping takes 6 swaps.
Optimal train swapping takes 1 swaps.
thanks!

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

### 299 wa help!plz

hey guys ...
i dont no i get wa plz help here's my code ...
is there a better optimised code ...

Code: Select all

``````DELETED
``````
Last edited by yogeshgo05 on Wed Mar 01, 2006 2:47 am, edited 1 time in total.

TISARKER
Learning poster
Posts: 88
Joined: Tue Oct 12, 2004 6:45 pm
Location: Bangladesh
Contact:

### Re: 299 wa help!plz

yogeshgo05
You did not swap value after comparing. This is only and only your mistake. if(a[j]>a[j+1)then
1.increase counter
2.Swap a[j] and a[j+1]
Hope u have understood
Mr. Arithmetic logic Unit