UVa : 11525 (Permutation)


1. for example, suppose k = 5
we know the total number of permutation is, factorial (5) = 120
120 / k = 24
that means,
first 24 permutation will be like this, 1****
next 24 permutation will be like this, 2****
next 24 permutation will be like this, 3****
so forth

2. if k = 5 and N = 60, what would be the output ?
we know, first position will be 3
in first 24 permutation format will be 1****
here **** means, there are 4! permutation of rest of the number, 2 to 5

next 24 permutation format will be 2****
here **** means, there are 4! permutation of rest of the number: 1, 3, 4, 5

and so on

3. see the sample input
there are k numbers
take the first example,
3
2 1 0

N = (2 * 2!) + (1 * 1!) + (0 * 0!)

0 means first round
1 means 2nd round
2 means 3rd round

given, 2 1 0
2 means 3rd round
so, first number = 3
1 means 2nd round
so 2nd number = 2
0 means 1st round
so, 3rd number = 1

output : 3 2 1

take last example,
4
1 2 1 0
numbers are 1 to 4 = 1 2 3 4
1 means 2nd round
so, 1st number = 2
numbers are = 1 3 4
2 means 3rd round
so, 2nd number = 4
numbers are = 1 3
1 means 2nd round
so, 3rd number = 3
number are = 1
0 means 1st round
so, 4th number = 1

output : 2 4 3 1

Solution:

// http://uva.onlinejudge.org/external/115/11525.html
// Runtime : 2.400s
// Tag : math, gotcha

// @BEGIN_OF_SOURCE_CODE

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <cctype>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <sstream>
#include <cmath>
#include <bitset>
#include <utility>
#include <set>
#include <numeric>
#define INT_MAX 2147483647
#define INT_MIN -2147483647
#define pi acos(-1.0)
#define N 1000000
#define LL unsigned long long
using namespace std;


int main ()
{
    int testCase;
    scanf ("%d", &testCase);

    while ( testCase-- ) {
        int k;
        scanf ("%d", &k);

        vector <int> sequence;

        for ( int i = 1; i <= k; i++ )
            sequence.push_back (i);

        bool space = false;
        int input;

        for ( int i = 0; i < k; i++ ) {
            scanf ("%d", &input);
            if ( space ) printf (" "); space = true;
            printf ("%d", sequence [input]);
            sequence.erase (sequence.begin () + input);
        }

        printf ("\n");
    }

	return 0;
}

// @END_OF_SOURCE_CODE

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s