Bookmark and Share

free counters
Back to the Training Home

Lesson: 6
Problem: 299
Title: Train Swapping
Difficulty Level: 1
Date: Dec 15, 2008
Site: UVA

Very long story for an easy problem. For this case, i think you better see the input/output specification with Sample test data first, guess the problem, then read the problem description.

If you do so, probably you get the problem. Do you know bubble sort ? i think so. Then you are very close to a solution.

This is a bubble sort algorithm:

for(int i = 0; i<N; i++) {
for(int j = i+1; j<N; j++) {
if(A[i] > A[j] ) {
Swap(A[i],A[j]);
}
}
}

for this problem you do not need to swap the values, just have to count the number of swaps for sorting an array. To do this, change only one line...
Remove Swap(A[i],A[j]);
add count++;

finally print the count with proper format ( careful about the output format)

Back to the Training Home