zoukankan      html  css  js  c++  java
  • CF 1009A 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<string>
    #include<cstdlib>
    #include<cmath>
    #include<iostream>
    #include<cstring>
    #include<set>
    #include<queue>
    #include<algorithm>
    #include<vector>
    #include<map>
    #include<cctype>
    #include<stack>
    #include<sstream>
    #include<list>
    #include<assert.h>
    #include<bitset>
    #include<numeric>
    #define debug() puts("++++")
    #define gcd(a,b) __gcd(a,b)
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    #define fi first
    #define se second
    #define pb push_back
    #define sqr(x) ((x)*(x))
    #define ms(a,b) memset(a,b,sizeof(a))
    #define sz size()
    #define be begin()
    #define pu push_up
    #define pd push_down
    #define cl clear()
    #define lowbit(x) -x&x
    #define all 1,n,1
    #define rep(i,x,n) for(int i=(x); i<(n); i++)
    #define in freopen("in.in","r",stdin)
    #define out freopen("out.out","w",stdout)
    using namespace std;
    typedef long long LL;
    typedef unsigned long long ULL;
    typedef pair<int,int> P;
    const int INF = 0x3f3f3f3f;
    const LL LNF = 1e18;
    const int N = 1e5 + 20;
    const int maxm = 1e6 + 10;
    const double PI = acos(-1.0);
    const double eps = 1e-8;
    const int dx[] = {-1,1,0,0,1,1,-1,-1};
    const int dy[] = {0,0,1,-1,1,-1,1,-1};
    int dir[4][2] = {{0,1},{0,-1},{-1,0},{1,0}};
    const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    
    int n,m,x,cnt=0;
    int a[1005],b[1005];
    int h1,h2;
    int main()
    {
        scanf("%d%d",&n,&m);
        for(int i=1; i<=n; i++){
           cin>>a[i];
        }
        for(int i=1; i<=m; i++){
            cin>>b[i];
        }
        int h1=1,h2=1;
        cnt=0;
        while(1)
        {
    
            if(b[h2] >= a[h1])
            {
                cnt++;
                h1++;
                h2++;
            }
    
            if(b[h2] < a[h1])
            {
                h1++;
            }
    
            if(h1>n || h2>m) break; //!注意是>而非=
        }
        cout<<cnt<<endl;
    }
    /*
    2 4 5 2 4
    5 3 4 6
    
    */
    
    
  • 相关阅读:
    apache solr简单搭建
    Flash学习初总结
    UWP多设备加载不同xaml布局文件
    鼠标右键多余选项删除
    用命令查看win10/win8.1等详细激活信息方法:
    win10 登陆选项 无法打开
    UWP应用开发:添加复制按钮,添加引用
    notepad++详细介绍!
    Python安装出现2503 2502 问题解决!
    Genymotion插件安装教程
  • 原文地址:https://www.cnblogs.com/Roni-i/p/9347285.html
Copyright © 2011-2022 走看看