@shreyk5,
Your code would be illegal in standard C++: you can't declare "standard" array sizes at run time. For example, a line like
int a[n];
is not valid. Nor is it necessary, because you are actually given a maximum size for arrays as one of the constraints. This is only allowed as a non-standard extension by some compilers because some languages (like the C99 version of C, or for allocatable or automatic arrays in Fortran) do permit VLAs (variable-length arrays).
Your comments aren't helpful, I'm afraid - nor is your
indentation nor your
naming of variables (a,b,c,...). No idea what your pos[] array was intended for, but nothing in it is ever used.
The sequence given in the codechef example is not very representative - you should make up a few of your own. Make sure that you look at all the "special cases" - e.g. no zeroes.
Why don't you add a few lines to isValid() to print out which permutations you are actually using. Then you may be able to debug.
As the competition is no longer live and it's relegated to a "practice" exercise, here's a version using vectors. It is
probably doing something akin to your code, or, at least, your intention, but I don't understand your code enough to judge.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
|
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
//======================================================================
bool isGood( int N, int K, const vector<int> &p ) // Check permutation for "good"ness
{
int num = 0;
for ( int i = 1; i < N; i++ )
{
if ( p[i] > p[i-1] )
{
num++;
if ( num > K ) return false;
}
}
return ( num == K );
}
//======================================================================
int process( int N, int K, vector<int> &a ) // Process a single sequence
{
vector<bool> notUsed( N + 1, true ); // Not using the [0] index here
vector<int> freeValue, freePosition;
// Find the available slots and values
for ( int i = 0; i < N; i++ )
{
if ( a[i] ) notUsed[a[i]] = false;
else freePosition.push_back( i );
}
for ( int i = 1; i <= N; i++ )
{
if ( notUsed[i] ) freeValue.push_back( i );
}
// Special case with no free slots - just check original sequence
if ( !freePosition.size() ) return (int)isGood( N, K, a );
// Now create all permutations and check them
int nGood = 0;
do
{
for ( int i = 0; i < freePosition.size(); i++ ) a[freePosition[i]] = freeValue[i];
if ( isGood( N, K, a ) ) nGood++;
} while ( next_permutation( freeValue.begin(), freeValue.end() ) );
return nGood;
}
//======================================================================
int main()
{
int T; // Number of tests
int N, K; // Length of sequence; number in order
cin >> T;
while( T-- )
{
cin >> N >> K;
vector<int> a( N );
for ( int i = 0; i < N; i++ ) cin >> a[i];
cout << process( N, K, a ) << '\n';
}
}
//======================================================================
|