1009A - Game Shopping

time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Maxim wants to buy some games at the local game shop. There are nn games in the shop, the ii-th game costs cici.

Maxim has a wallet which can be represented as an array of integers. His wallet contains mmbills, the jj-th bill has value ajaj.

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 ii 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 ii-th game using this bill. After Maxim tried to buy the nn-th game, he leaves the shop.

Maxim buys the ii-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 ii-th game. If he successfully buys the ii-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]c=[2,4,5,2,4] and array a=[5,3,4,6]a=[5,3,4,6] the following process takes place: Maxim buys the first game using the first bill (its value is 55), the bill disappears, after that the second bill (with value 33) becomes the first one in Maxim's wallet, then Maxim doesn't buy the second game because c2>a2c2>a2, the same with the third game, then he buys the fourth game using the bill of value a2a2 (the third bill becomes the first one in Maxim's wallet) and buys the fifth game using the bill of value a3a3.

Your task is to get the number of games Maxim will buy.

Input

The first line of the input contains two integers nn and mm (1n,m10001≤n,m≤1000) — the number of games and the number of bills in Maxim's wallet.

The second line of the input contains nn integers c1,c2,,cnc1,c2,…,cn (1ci10001≤ci≤1000), where cici is the cost of the ii-th game.

The third line of the input contains mm integers a1,a2,,ama1,a2,…,am (1aj10001≤aj≤1000), where ajaj is the value of the jj-th bill from the Maxim's wallet.

Output

Print a single integer — the number of games Maxim will buy.

Examples
input
Copy
5 4
2 4 5 2 4
5 3 4 6
output
Copy
3
input
Copy
5 2
20 40 50 20 40
19 20
output
Copy
0
input
Copy
6 4
4 8 15 16 23 42
1000 1000 1000 1000
output
Copy
4
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.


题意:一个人买游戏,游戏购买顺序是从左到右的,给n个游戏,他的钱包里有m张钞票,每张都有 一定的面值,,如果当前面值能够买游戏,就会买游戏,然后钞票用掉。不能购买就跳下张钞票,直到能够买为止。输出最多能购买多少游戏。

题解:模拟

#include<bits/stdc++.h>
using namespace std;
int a[1010],c[1010],n,m,j;
int main()
{
    cin>>n>>m;
    for(int i=0; i<n; i++)
        cin>>c[i];
    for(int i=0; i<m; i++)
        cin>>a[i];
    for(int i=0; i<n; i++)
        if(c[i]<=a[j])
            j++;
    cout<<j<<endl;
    return 0;
}

python:

n,m=map(int,input().split())
c=list(map(int,input().split()))
a=list(map(int,input().split()))
j=0
for i in c:
    if j<m and i<=a[j]:
        j+=1 
print(j)

猜你喜欢

转载自blog.csdn.net/memory_qianxiao/article/details/81056043
今日推荐