Codeforces1009A——Game Shopping

Maxim wants to buy some games at the local game shop. There are n games in the shop, the i-th game costs ci.
Maxim has a wallet which can be represented as an array of integers. His wallet contains m bills, the j-th bill has value aj.
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 c2>a2, the same with the third game, then he buys the fourth game using the bill of value a2 (the third bill becomes the first one in Maxim’s wallet) and buys the fifth game using the bill of value a3.
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≤n,m≤1000) — the number of games and the number of bills in Maxim’s wallet.
The second line of the input contains n integers c1,c2,…,cn (1≤ci≤1000), where ci is the cost of the i-th game.
The third line of the input contains m integers a1,a2,…,am (1≤aj≤1000), where aj 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.
Examples
Input
5 4
2 4 5 2 4
5 3 4 6
Output
3
Input
5 2
20 40 50 20 40
19 20
Output
0
Input
6 4
4 8 15 16 23 42
1000 1000 1000 1000
Output
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.

给一些商品的价格和钞票的面额 钞票要一张一张拿 问能买多少件商品,而是直接模拟就好了

代码:

#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int N=1005;
int c[N];
int a[N];
int cidx;
int aidx;
int n,m;
int main(void){
    scanf("%d%d",&n,&m);
    for(int i=0;i<n;i++){
        scanf("%d",&c[i]);
    }
    for(int i=0;i<m;i++){
        scanf("%d",&a[i]);
    }
    int cnt=0;
    while(cidx<n && aidx<m){
        if(a[aidx]>=c[cidx]){
            aidx++;
            cidx++;
            cnt++;
        }
        else{
            cidx++;
        }
    }
    printf("%d\n",cnt);
    return 0;
}
全部评论

相关推荐

后来123321:别着急,我学院本大二,投了1100份,两个面试,其中一个还是我去线下招聘会投的简历,有时候这东西也得看运气
点赞 评论 收藏
分享
03-31 17:40
已编辑
门头沟学院 算法工程师
程序员牛肉:小牛肉来也! 也不要焦虑啦,你第一志愿还没有结束,只是回到人才库(泡大池子等待各个部门挑选)而已。仅仅代表你不符合这个组的用人标准,并不能够说明你在本次暑期实习中没机会加入美团了。 还是平复好心态,不断的复盘,等待下一次面试就好了。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务