#P15595. [ICPC 2020 Jakarta R] Shopping Changes
[ICPC 2020 Jakarta R] Shopping Changes
题目描述
Felix and his friends are having a shopping frenzy today. From a recent cash transaction, he received a change with banknotes. Felix would like to slip the banknotes he received into his wallet without changing their order.
For example, let’s assume that Felix received a change with banknotes in the following order: . Supposed there are banknotes in Felix’s wallet in the following order: , then there are four possible ways to slip the change into Felix’s wallet.
- Slip the change before the banknote. After slipping the change, the banknotes in his wallet are in the following order: .
- Slip the change between the and banknotes. After slipping the change, the banknotes in his wallet are in the following order: .
- Slip the change between the and banknotes. After slipping the change, the banknotes in his wallet are in the following order: .
- Slip the change after the banknote. After slipping the change, the banknotes in his wallet are in the following order: .
Being a tidy person, Felix would like the banknotes in his wallet to be as sorted as possible. Therefore, he would like to slip the change in a way that minimizes the inversion count of his wallet after the slip. The inversion count of a wallet is the number of pairs of banknotes in the wallet such that:
- banknote is closer to the front than banknote , and
- banknote has strictly more value than banknote .
Feeling a bit generous today, instead of keeping the change, Felix would like to give them to one of his friends and slip the change into their wallet. The friend has a wallet containing banknotes which values are respectively from the front-most to the back-most of the wallet. Felix would like to give the change to his friend that can minimize the inversion count of their wallet after slipping the change. Therefore, for each of his friends, Felix needs to count the minimum inversion count of their wallet if he slips the change into their wallet.
输入格式
Input begins with a line containing two integers: () representing the number of banknotes in the change and the number of Felix’s friends, respectively. The next line contains integers: () representing the value of the banknotes in the change. The next lines each begin with an integer () representing the number of banknotes in the friend’s wallet, followed by integers: () representing the value of the banknotes in the wallet. The sum of all is not more than .
输出格式
For each friend in the same order as input, output in a line an integer representing the minimum inversion count of their wallet if Felix slips the change into their wallet.
3 3
5 6 7
6 2 3 4 8 9 10
2 100 99
3 5 6 7
0
1
1
3 2
7 6 5
6 2 3 4 8 9 10
6 10 9 8 4 3 2
3
27
提示
Explanation for the sample input/output #1
For the friend, by slipping the change between the and banknotes, the banknotes in the wallet are in the following order: . Therefore, the inversion count of the wallet is .
For the friend, by slipping the change before the banknote, the banknotes in the wallet are in the following order: . Therefore, the inversion count of the wallet is . There is no way to achieve a smaller inversion count.
For the friend, by slipping the change between the and banknotes, or between the and banknotes, the inversion count of the wallet is . There is no way to achieve a smaller inversion count.