收集卡牌

题目 收集卡牌

image-1836dda1

思路分析

种类与个数的问题 双指针和队列里见过很多次了

维护每种的个数 再维护种类的数量

可以用unordered_map也可以直接用值做下标计数

代码实现

#include<bits/stdc++.h>

using namespace std;

const int N=100010;

int cnt[N];

int n,m;

int main()

{

    cin>>n>>m;

    int total=0;

    while(m--){

        int x;

        cin>>x;

        if(!cnt[x])

            total++;

        cnt[x]++;

        if(total==n){

            cout<<1;

            for(int i=1;i<=n;i++){

                if(--cnt[i]==0)

                    total--;

            }

        }

        else

            cout<<0;

    }

    return 0;

}
#include<bits/stdc++.h>

using namespace std;

unordered_map<int,int> h;

int n,m;

int main()

{

    cin>>n>>m;

    int total=0;

    while(m--){

        int x;

        cin>>x;

        if(!h[x])

            total++;

        h[x]++;

        if(total==n){

            cout<<1;

            for(int i=1;i<=n;i++){

                if(--h[i]==0)

                    total--;

            }

        }

        else

            cout<<0;

    }

    return 0;

}

同类题型

视频讲解


⬅️ 商品总类 🏠 00-刷题理模型 ➡️ 最大数量