Connection Status:
Competition Arena > SortishDiv2
SRM 636 · 2014-08-25 · by Errichto · Brute Force
Class Name: SortishDiv2
Return Type: int
Method Name: ways
Arg Types: (int, vector<int>)
Problem Statement

Problem Statement

Everyone likes some sequences more than others. Every person has their own function which tells them how good a sequence is. For example, for some people this function could simply be the count of negative numbers in the sequence.


Jezalb's most favorite sequences are ones that are sorted in increasing order. When he sees a sequence S, he immediately calculates the number of pairs of indexes i < j such that S[i] < S[j]. He calls this number the "sortedness" of S.


This morning Jezalb entered a classroom and saw a permutation of 1 through N on the blackboard. He quickly calculated its sortedness. He then left the classroom and forgot the permutation. He only remembered the sortedness he computed. You are given this value in a int sortedness.


Later that day Jezalb reentered the classroom and saw a sequence on the blackboard. The sequence was a permutation of 1 through N, but with some elements erased. You are given this sequence as a int[] seq with N elements. Some of the elements in seq may be 0, which indicates an erased number.


Jezalb thinks that the sequence seq may have been obtained by erasing some elements of the sequence he saw during his first visit to the classroom. He would like to restore the erased elements.


You are given the int sortedness and the int[] seq. Return the number of ways in which he can fill in the missing elements into seq in such a way that the sortedness of the obtained permutation will be exactly sortedness.

Constraints

  • sortedness will be between 0 and 1,000,000,000, inclusive.
  • seq will contain between 1 and 100 elements, inclusive.
  • Elements in seq will be between 0 and number of elements in seq, inclusive.
  • Positive elements in seq will be distinct.
  • Number of elements equal to 0 in seq will be between 0 and 5, inclusive.
Examples
0)
5
{4, 0, 0, 2, 0}
Returns: 2

There are six ways to fill in the missing elements. Out of those six permutations, only two have sortedness 5: {4, 1, 5, 2, 3} and {4, 3, 1, 2, 5}.

1)
4
{0, 0, 0, 0}
Returns: 5

All 5 possible ways are: {1, 3, 4, 2}, {1, 4, 2, 3}, {2, 1, 4, 3}, {2, 3, 1, 4}, {3, 1, 2, 4}.

2)
2
{1, 3, 2}
Returns: 1

There are no gaps and sortedness is indeed equal to 2.

3)
2
{1, 2, 0, 5, 0, 0}
Returns: 0

Regardless of how he fills in the gaps, the sortedness of the resulting permutation will always be greater than 2.

4)
2405
{4,62,10,33,86,58,9,49,68,84,30,88,90,67,59,0,19,25,12,72,44,85,51,5,13,98,94,91,24,47,27,95,100,77,15,92,0,70,55,31,28,81,75,39,34,74,2,89,45,63,36,64,43,93,29,50,7,54,0,82,71,66,97,53,23,38,69,52,48,21,26,17,20,57,37,61,11,73,60,78,18,79,0,80,16,83,56,35,0,32,6,96,1,99,46,76,22,87,3,41}
Returns: 1

100 5

5)
7
{0,0,0,0,0}
Returns: 15

5 5

Submissions are judged against all 94 archived test cases, of which 6 are shown here. Case numbers match the judge’s.

Coding Area

Language: C++17 · define a public class SortishDiv2 with a public method int ways(int sortedness, vector<int> seq) · 94 test cases · 2 s / 256 MB per case

Submitting as anonymous