B. The first task

Codeforces
IDCF10199B
Time5000ms
Memory1024MB
Difficulty
English · Original
Formal · Original
After being accepted as an intern in one of the biggest companies, Yasser's first task was to come up with a strategy to encrypt a huge amount of data into some permutation of integers. Here is the definition of a permutation that can be accepted by Yasser's supervisor: His supervisor gave him a week to finish the task. After days of hard work he finally managed to finish the task at the night of the deadline, he was so happy and looking forward to seeing how much his supervisor will be impressed by his hard work. Since Yasser was very tired, he fell asleep without turning off his computer, and while he was asleep dreaming about all the nice words that people are going to say about him, his little brother was messing up his result. In the morning, he discovered this disaster and found out that his brother has changed the resulting sequence and that he may have added some arbitrary integers to it or deleted some integers from it and the sequence may not be a valid permutation anymore. Yasser now can't remember how the original permutation was like and what its length was. There is no time for him to repeat the work, but instead of punishing his brother for what he did, he decided to apply the minimum number of operations on the current sequence to make it a valid permutation again. Each operation is either inserting or removing a number. This way he may be able to survive the meeting without being a joke and after the meeting he will have time to reconstruct the original sequence. The first line of the input contains the number of test cases $T$. Each test case starts with a line that contains a single integer $N$ the length of the current sequence, where $1 <= N <= 10^5$. The following line contains $N$ space-separated integers representing the sequence. Each integer is between 1 and $10^9$ inclusive. For each test case output a single line contains the minimum number of operations. Please note that Yasser can make a permutation of any length even of length 1. The only thing that matters is to reach a valid permutation accepted by his supervisor by using the minimum number of operations. ## Input The first line of the input contains the number of test cases $T$. Each test case starts with a line that contains a single integer $N$ the length of the current sequence, where $1 <= N <= 10^5$.The following line contains $N$ space-separated integers representing the sequence. Each integer is between 1 and $10^9$ inclusive. ## Output For each test case output a single line contains the minimum number of operations. [samples] ## Note Please note that Yasser can make a permutation of any length even of length 1. The only thing that matters is to reach a valid permutation accepted by his supervisor by using the minimum number of operations.
**Definitions** Let $ T \in \mathbb{Z} $ be the number of test cases. For each test case $ k \in \{1, \dots, T\} $: - Let $ n_k \in \mathbb{Z} $ denote the length of the current sequence. - Let $ A_k = (a_{k,1}, a_{k,2}, \dots, a_{k,n_k}) $ be a sequence of integers, where $ 1 \le a_{k,i} \le 10^9 $. **Constraints** 1. $ 1 \le T \le 10^5 $ 2. $ 1 \le n_k \le 10^5 $ for each $ k $ 3. $ 1 \le a_{k,i} \le 10^9 $ for all $ i \in \{1, \dots, n_k\} $ **Objective** For each test case $ k $, find the minimum number of insertions or deletions required to transform $ A_k $ into a permutation of $ \{1, 2, \dots, m\} $ for some $ m \in \mathbb{Z}^+ $. This minimum is: $$ \min_{m \in \mathbb{Z}^+} \left( n_k - |\{ x \in A_k \mid 1 \le x \le m \}| + |m - |\{ x \in A_k \mid 1 \le x \le m \}| | \right) $$ Equivalently, let $ c $ be the count of distinct integers in $ A_k $ that lie in $ [1, n_k] $. Then the minimum operations are: $$ n_k - c $$ *(since the optimal $ m $ is the number of distinct valid elements in $ [1, n_k] $, and we must delete all invalid/extra elements and insert the missing ones in $ \{1, \dots, m\} $, totaling $ n_k - c $)*
API Response (JSON)
{
  "problem": {
    "name": "B. The first task",
    "description": {
      "content": "After being accepted as an intern in one of the biggest companies, Yasser's first task was to come up with a strategy to encrypt a huge amount of data into some permutation of integers. Here is the de",
      "description_type": "Markdown"
    },
    "platform": "Codeforces",
    "limit": {
      "time_limit": 5000,
      "memory_limit": 1048576
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "CF10199B"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "After being accepted as an intern in one of the biggest companies, Yasser's first task was to come up with a strategy to encrypt a huge amount of data into some permutation of integers. Here is the de...",
      "is_translate": false,
      "language": "English"
    },
    {
      "statement_type": "Markdown",
      "content": "**Definitions**  \nLet $ T \\in \\mathbb{Z} $ be the number of test cases.  \nFor each test case $ k \\in \\{1, \\dots, T\\} $:  \n- Let $ n_k \\in \\mathbb{Z} $ denote the length of the current sequence.  \n- Le...",
      "is_translate": false,
      "language": "Formal"
    }
  ]
}
Full JSON Raw Segments