File size: 3,210 Bytes
1f3a244
 
 
 
1
2
3
4
5
{"type": "session", "harness": "opencode", "id": "cf-1009-A", "name": "Codeforces: Game Shopping"}
{"type": "message", "message": {"role": "system", "content": "You are an expert competitive programmer. You solve problems by analyzing requirements, designing algorithms, and writing clean, efficient code."}}
{"type": "message", "message": {"role": "user", "content": "Problem: Game Shopping (Rating: 800)\nTags: implementation\n\nMaxim 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 contains $m$ bills, the $j$-th bill has value $a_j$.\n\nGames in the shop are ordered from left to right, Maxim tries to buy every game in that order.\n\nWhen 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.\n\nMaxim 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.\n\nFor 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$.\n\nYour task is to get the number of games Maxim will buy.\n\nInput: 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.\n\nThe 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.\n\nThe 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.\n\nOutput: Print a single integer — the number of games Maxim will buy.\n\nExamples:\nInput: 5 4\n2 4 5 2 4\n5 3 4 6\nOutput: 3\n\nInput: 5 2\n20 40 50 20 40\n19 20\nOutput: 0\n\nInput: 6 4\n4 8 15 16 23 42\n1000 1000 1000 1000\nOutput: 4\n"}}
{"type": "message", "message": {"role": "assistant", "content": "Let's keep the variable $$$pos$$$ which will represent the number of games Maxim buy. Initially $$$pos = 0$$$. Assume that arrays $$$a$$$ and $$$c$$$ are 0-indexed. Then let's iterate over all $$$i = 0 \\dots n - 1$$$ and if $$$pos < m$$$ and $$$a[pos] \\ge c[i]$$$ make $$$pos := pos + 1$$$. So $$$pos$$$ will be the answer after this cycle."}}