A. Game Shopping

Codeforces
IDCF1009A
Time1000ms
Memory256MB
Difficulty
implementation
English · Original
Chinese · Translation
Formal · Original
Maxim wants to buy some games at the local game shop. There are $n$ games in the shop, the $i$\-th game costs $c_i$. Maxim has a wallet which can be represented as an array of integers. His wallet contains $m$ bills, the $j$\-th bill has value $a_j$. Games in the shop are ordered from left to right, Maxim tries to buy _every_ game in that order. When Maxim stands at the position $i$ in the shop, he takes the first bill from his wallet (if his wallet is empty then he proceeds to the next position immediately) and tries to buy the $i$\-th game using this bill. After Maxim tried to buy the $n$\-th game, he leaves the shop. Maxim buys the $i$\-th game if and only if the value of the first bill (which he takes) from his wallet is greater or equal to the cost of the $i$\-th game. If he successfully buys the $i$\-th game, the first bill from his wallet disappears and the next bill becomes first. Otherwise Maxim leaves the first bill in his wallet **(this bill still remains the first one)** and proceeds to the next game. For example, for array $c = [2, 4, 5, 2, 4]$ and array $a = [5, 3, 4, 6]$ the following process takes place: Maxim buys the first game using the first bill (its value is $5$), the bill disappears, after that the second bill (with value $3$) becomes the first one in Maxim's wallet, then Maxim doesn't buy the second game because $c_2 > a_2$, the same with the third game, then he buys the fourth game using the bill of value $a_2$ (the third bill becomes the first one in Maxim's wallet) and buys the fifth game using the bill of value $a_3$. Your task is to get the number of games Maxim will buy. ## Input The first line of the input contains two integers $n$ and $m$ ($1 \le n, m \le 1000$) — the number of games and the number of bills in Maxim's wallet. The second line of the input contains $n$ integers $c_1, c_2, \dots, c_n$ ($1 \le c_i \le 1000$), where $c_i$ is the cost of the $i$\-th game. The third line of the input contains $m$ integers $a_1, a_2, \dots, a_m$ ($1 \le a_j \le 1000$), where $a_j$ is the value of the $j$\-th bill from the Maxim's wallet. ## Output Print a single integer — the number of games Maxim will buy. [samples] ## Note The first example is described in the problem statement. In the second example Maxim cannot buy any game because the value of the first bill in his wallet is smaller than the cost of any game in the shop. In the third example the values of the bills in Maxim's wallet are large enough to buy any game he encounter until he runs out of bills in his wallet.
Maxim 想要在当地的商店购买一些游戏。商店里有 $n$ 款游戏,第 $i$ 款游戏的价格为 $c_i$。 Maxim 的钱包可以用一个整数数组表示。他的钱包中有 $m$ 张钞票,第 $j$ 张钞票的面值为 $a_j$。 商店里的游戏从左到右排列,Maxim 会按此顺序尝试购买 _每一款_ 游戏。 当 Maxim 站在第 $i$ 个位置时,他会从钱包中取出第一张钞票(如果钱包为空,则直接进入下一个位置),并尝试使用这张钞票购买第 $i$ 款游戏。在尝试购买完第 $n$ 款游戏后,Maxim 离开商店。 Maxim 能成功购买第 $i$ 款游戏,当且仅当他从钱包中取出的第一张钞票的面值大于或等于第 $i$ 款游戏的价格。如果他成功购买了第 $i$ 款游戏,则钱包中的第一张钞票被移除,下一张钞票成为新的第一张。否则,Maxim 保留这张钞票在钱包中(它仍然是第一张),并继续前往下一款游戏。 例如,对于数组 $c = [ 2, 4, 5, 2, 4 ]$ 和数组 $a = [ 5, 3, 4, 6 ]$,过程如下:Maxim 使用第一张钞票(面值为 $5$)购买第一款游戏,该钞票被移除,之后第二张钞票(面值为 $3$)成为钱包中的第一张;接着,Maxim 无法购买第二款游戏,因为 $c_2 > a_2$,第三款游戏同理;然后他使用面值为 $a_2$ 的钞票(此时第三张钞票成为第一张)购买第四款游戏,并使用面值为 $a_3$ 的钞票购买第五款游戏。 你的任务是计算 Maxim 将购买多少款游戏。 输入的第一行包含两个整数 $n$ 和 $m$($1 lt.eq n, m lt.eq 1000$)——游戏的数量和 Maxim 钱包中钞票的数量。 第二行包含 $n$ 个整数 $c_1, c_2, dots.h, c_n$($1 lt.eq c_i lt.eq 1000$),其中 $c_i$ 是第 $i$ 款游戏的价格。 第三行包含 $m$ 个整数 $a_1, a_2, dots.h, a_m$($1 lt.eq a_j lt.eq 1000$),其中 $a_j$ 是 Maxim 钱包中第 $j$ 张钞票的面值。 请输出一个整数——Maxim 将购买的游戏数量。 第一个例子已在题目描述中给出。 在第二个例子中,Maxim 无法购买任何游戏,因为钱包中第一张钞票的面值小于商店中任何一款游戏的价格。 在第三个例子中,Maxim 钱包中的钞票面值足够大,足以购买他遇到的每一款游戏,直到钱包中的钞票用完。 ## Input 输入的第一行包含两个整数 $n$ 和 $m$($1 lt.eq n, m lt.eq 1000$)——游戏的数量和 Maxim 钱包中钞票的数量。第二行包含 $n$ 个整数 $c_1, c_2, dots.h, c_n$($1 lt.eq c_i lt.eq 1000$),其中 $c_i$ 是第 $i$ 款游戏的价格。第三行包含 $m$ 个整数 $a_1, a_2, dots.h, a_m$($1 lt.eq a_j lt.eq 1000$),其中 $a_j$ 是 Maxim 钱包中第 $j$ 张钞票的面值。 ## Output 请输出一个整数——Maxim 将购买的游戏数量。 [samples] ## Note 第一个例子已在题目描述中给出。 在第二个例子中,Maxim 无法购买任何游戏,因为钱包中第一张钞票的面值小于商店中任何一款游戏的价格。 在第三个例子中,Maxim 钱包中的钞票面值足够大,足以购买他遇到的每一款游戏,直到钱包中的钞票用完。
**Definitions** Let $ n, m \in \mathbb{Z}^+ $ denote the number of games and the number of bills, respectively. Let $ C = (c_1, c_2, \dots, c_n) $ be the sequence of game costs, where $ c_i \in \mathbb{Z}^+ $. Let $ A = (a_1, a_2, \dots, a_m) $ be the sequence of bill values, where $ a_j \in \mathbb{Z}^+ $. **Constraints** 1. $ 1 \leq n, m \leq 1000 $ 2. $ 1 \leq c_i \leq 1000 $ for all $ i \in \{1, \dots, n\} $ 3. $ 1 \leq a_j \leq 1000 $ for all $ j \in \{1, \dots, m\} $ **Objective** Simulate the process: - Initialize a pointer $ j = 1 $ for the current first bill in wallet $ A $. - For each game $ i = 1 $ to $ n $: - If $ j > m $, skip the game (no bills left). - Else, if $ a_j \geq c_i $, then increment the purchase count and increment $ j $ (consume the bill). - Else, do nothing (retain the bill). - Output the total number of games purchased. Let $ p $ be the number of purchased games. Then: $$ p = \left| \left\{ i \in \{1, \dots, n\} \mid \exists j \in \{1, \dots, m\}, \text{ such that } a_j \geq c_i \text{ and } j \text{ is the smallest unused index at step } i \right\} \right| $$ Equivalently, simulate greedily: $$ p = \sum_{i=1}^{n} \mathbb{I}\left( j \leq m \text{ and } a_j \geq c_i \right), \quad \text{where } j \text{ is incremented only when } a_j \geq c_i $$
Samples
Input #1
5 4
2 4 5 2 4
5 3 4 6
Output #1
3
Input #2
5 2
20 40 50 20 40
19 20
Output #2
0
Input #3
6 4
4 8 15 16 23 42
1000 1000 1000 1000
Output #3
4
API Response (JSON)
{
  "problem": {
    "name": "A. Game Shopping",
    "description": {
      "content": "Maxim wants to buy some games at the local game shop. There are $n$ games in the shop, the $i$\\-th game costs $c_i$. Maxim has a wallet which can be represented as an array of integers. His wallet co",
      "description_type": "Markdown"
    },
    "platform": "Codeforces",
    "limit": {
      "time_limit": 1000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "CF1009A"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "Maxim wants to buy some games at the local game shop. There are $n$ games in the shop, the $i$\\-th game costs $c_i$.\n\nMaxim has a wallet which can be represented as an array of integers. His wallet co...",
      "is_translate": false,
      "language": "English"
    },
    {
      "statement_type": "Markdown",
      "content": "Maxim 想要在当地的商店购买一些游戏。商店里有 $n$ 款游戏,第 $i$ 款游戏的价格为 $c_i$。\n\nMaxim 的钱包可以用一个整数数组表示。他的钱包中有 $m$ 张钞票,第 $j$ 张钞票的面值为 $a_j$。\n\n商店里的游戏从左到右排列,Maxim 会按此顺序尝试购买 _每一款_ 游戏。\n\n当 Maxim 站在第 $i$ 个位置时,他会从钱包中取出第一张钞票(如果钱包为空,则直接进...",
      "is_translate": true,
      "language": "Chinese"
    },
    {
      "statement_type": "Markdown",
      "content": "**Definitions**  \nLet $ n, m \\in \\mathbb{Z}^+ $ denote the number of games and the number of bills, respectively.  \nLet $ C = (c_1, c_2, \\dots, c_n) $ be the sequence of game costs, where $ c_i \\in \\m...",
      "is_translate": false,
      "language": "Formal"
    }
  ]
}
Full JSON Raw Segments