Connection Status:
Competition Arena > TaroCards
SRM 613 · 2013-12-22 · by Witaliy · Dynamic Programming
Class Name: TaroCards
Return Type: long
Method Name: getNumber
Arg Types: (vector<int>, vector<int>, int)
Problem Statement

Problem Statement

Cat Taro has N cards. Exactly two integers are written on each card. You are given two int[]s first and second, each with N elements. For each i, the element first[i] represents the first integer on the i-th card, and the element second[i] represents the second integer on the i-th card.

It is known that for each x from 1 to N, inclusive, there is exactly one card with the first integer equal to x. In other words, all elements of first represent a permutation of integers from 1 to N, inclusive. On the other hand, second may contain duplicates, but all elements of second are only between 1 and 10, inclusive.

You are also given an int K. Taro wants to choose some subset of the cards (possibly none or all of them) in such a way that the total number of different integers written on the cards is less than or equal to K. Return the total number of ways to do that.

Constraints

  • first will contain between 1 and 50 elements, inclusive.
  • first and second will contain the same number of elements.
  • first will represent a permutation of integers between 1 and N, inclusive, where N is the number of elements in first.
  • Each element of second will be between 1 and 10, inclusive.
  • K will be between 1 and 2N, inclusive, where N is the number of elements in first.
Examples
0)
{1, 2}
{2, 3}
2
Returns: 3

In this case, there are four subsets of cards: None of the cards. The number of different integers is 0. Only the first card. The number of different integers is 2. Only the second card. The number of different integers is 2. Both the first and the second card. The number of different integers is 3. However, the last subset has too many different integers. Thus, the answer is 3.

1)
{3, 1, 2}
{1, 1, 1}
3
Returns: 8
2)
{4, 2, 1, 3}
{1, 2, 3, 4}
5
Returns: 16
3)
{1, 2, 3, 4, 5, 6, 7}
{2, 1, 10, 9, 3, 2, 2}
3
Returns: 17
4)
{1}
{2}
1
Returns: 1

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

Coding Area

Language: C++17 · define a public class TaroCards with a public method long long getNumber(vector<int> first, vector<int> second, int K) · 70 test cases · 2 s / 256 MB per case

Submitting as anonymous