Saurabh Saxena Saurabh Saxena - 3 months ago 14
C++ Question

Codechef is rejecting my solution

I am newbie on codechef and i was trying to solve the following question however my code runs fine on my machine, i also tested it with some cases.

Question is as follows :-

In Byteland it is always the military officer's main worry to order his soldiers on parade correctly. Luckily, ordering soldiers is not really such a problem. If a platoon consists of n men, all of them have different rank (from 1 - lowest to n - highest) and on parade they should be lined up from left to right in increasing order of rank.

Sounds simple, doesn't it? Well, Sgt Johnny thought the same, until one day he was faced with a new command. He soon discovered that his elite commandos preferred to do the fighting, and leave the thinking to their superiors. So, when at the first rollcall the soldiers lined up in fairly random order it was not because of their lack of discipline, but simply because they couldn't work out how to form a line in correct order of ranks. Sgt Johnny was not at all amused, particularly as he soon found that none of the soldiers even remembered his own rank. Over the years of service every soldier had only learned which of the other soldiers were his superiors. But Sgt Johnny was not a man to give up easily when faced with a true military challenge. After a moment's thought a solution of brilliant simplicity struck him and he issued the following order: "men, starting from the left, one by one, do: (step forward; go left until there is no superior to the left of you; get back in line).". This did indeed get the men sorted in a few minutes. The problem was solved... for the time being.

The next day, the soldiers came in exactly the same order as the day before, and had to be rearranged using the same method. History repeated. After some weeks, Sgt Johnny managed to force each of his soldiers to remember how many men he passed when going left, and thus make the sorting process even faster.

If you know how many positions each man has to walk to the left, can you try to find out what order of ranks the soldiers initially line up in?

Input

The first line of input contains an integer t<=50, the number of test cases. It is followed by t test cases, each consisting of 2 lines. The first line contains a single integer n (1<=n<=200000). The second line contains n space separated integers wi, denoting how far the i-th soldier in line must walk to the left when applying Sgt Johnny's algorithm.

Output

For each test case, output a single line consisting of n space separated integers - the ranks of the soldiers, given from left to right in their initial arrangement.

Example

Input:
2
3
0 1 0
5
0 1 2 0 1

Output:
2 1 3
3 2 1 5 4
Warning: large Input/Output data, be careful with certain languages

#include <iostream>
#include <string.h>

using namespace std;

int main ()
{
int t,n;
cin >> t;

while(t>0){

cin >> n;
int array[n+1];
int stepsmoved,i;

for(i = 1; i <= n; i++){
array[i] = i;
}


for(i = 1; i <=n; i++){
cin >> stepsmoved;
if(stepsmoved == 0){}
else{
int x;
x = array[i];
for (int j = i; j> i- stepsmoved; j--){
array[j] = array[j-1];
}
array[i-stepsmoved] = x;
}

}

for(i = 1; i <= n; i++){
cout<<array[i]<<" ";
}
cout<<endl;
t--;
}

return 0;
}


So is there something logically or syntactically wrong?

Answer

The order of 'unwinding' the sorting is relevant. Here is the code that demonstrates the statement above (the ranks are 1-based, the 1 - is highest, 10 - is lowest, array indices are 0-based):

#include <stdio.h>

void dump(int *a) {
    int i;
    for (i = 0; i < 10; i++)
      printf("%d ", a[i]);
      printf("\n");
}

int main() {
    int array[10] = {0}, steps[10] = {0};
    int i,j;
    srand(0);

    // Assign ranks in random order
    for (i = 0; i < 10;) {
        j = rand() % 10;
        if (!array[j])
        array[j] = ++i;
    }

    dump(array);

    // Sort according to the Sgt Johnny's initial idea
    for (i = 1; i < 10; i++) {
        for (j = 0; array[j] < array[i]; j++);
        if (j < i) {
            int k, temp = array[i];
            for (k = i; k > j; k--) {
                array[k] = array[k-1];
                steps[temp-1]++;
            }
            array[j] = temp;
            dump(array);
        }
    }
    printf("Steps:\n");
    dump(steps);
    printf("\n");

    // reconstruct the origina order
#if 1
    for (i = 10-1; i >= 0; i--)
#else
    for (i = 0; i < 10; i++)
#endif
    {
        int s = steps[array[i]-1];
        for (j = i; s; s--, j++) {
            int temp = array[j];
            array[j] = array[j+1];
            array[j+1] = temp;
        }
        dump(array);
    }
}

If the reconstruction is done in reverse order, then we get a sequence that matches original:

8 7 5 1 10 4 2 3 9 6 
7 8 5 1 10 4 2 3 9 6 
5 7 8 1 10 4 2 3 9 6 
1 5 7 8 10 4 2 3 9 6 
1 4 5 7 8 10 2 3 9 6 
1 2 4 5 7 8 10 3 9 6 
1 2 3 4 5 7 8 10 9 6 
1 2 3 4 5 7 8 9 10 6 
1 2 3 4 5 6 7 8 9 10 
Steps:
3 5 5 4 2 4 1 0 1 0 

1 2 3 4 5 6 7 8 9 10 
1 2 3 4 5 6 7 8 10 9 
1 2 3 4 5 6 7 8 10 9 
1 2 3 4 5 6 8 7 10 9 
1 2 3 4 5 8 7 10 9 6 
1 2 3 4 8 7 5 10 9 6 
1 2 3 8 7 5 10 4 9 6 
1 2 8 7 5 10 4 3 9 6 
1 8 7 5 10 4 2 3 9 6 
8 7 5 1 10 4 2 3 9 6 

Otherwise, the reconstructed order does not match the original:

8 7 5 1 10 4 2 3 9 6 
7 8 5 1 10 4 2 3 9 6 
5 7 8 1 10 4 2 3 9 6 
1 5 7 8 10 4 2 3 9 6 
1 4 5 7 8 10 2 3 9 6 
1 2 4 5 7 8 10 3 9 6 
1 2 3 4 5 7 8 10 9 6 
1 2 3 4 5 7 8 9 10 6 
1 2 3 4 5 6 7 8 9 10 
Steps:
3 5 5 4 2 4 1 0 1 0 

2 3 4 1 5 6 7 8 9 10 
2 4 1 5 6 7 3 8 9 10 
2 4 5 6 7 1 3 8 9 10 
2 4 5 7 1 3 8 6 9 10 
2 4 5 7 3 8 6 1 9 10 
2 4 5 7 3 8 6 1 9 10 
2 4 5 7 3 8 1 9 10 0 
2 4 5 7 3 8 1 10 9 0 
2 4 5 7 3 8 1 10 0 9 
2 4 5 7 3 8 1 10 0 6 
Comments