题解:P9343 一曲新词酒一杯

· · 题解

解题思路

维护一个集合,集合元素代表至少有一张红纸的酒杯编号:

参考代码

#include <bits/stdc++.h>
using namespace std;

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int T;
    cin>>T;
    set<int> s;
    while(T--)
    {
        s.clear();
        int n,m;
        cin>>n>>m;
        int ans=-1,p=0;
        for(int i=1;i<=m;i++)
        {
            int o,x;
            cin>>o>>x;
            if(ans!=-1)continue;
            if(o==1)s.insert(x);
            else if(o==2)
            {
                if(!p)
                {
                    for(int j=1;j<=n;j++)
                    {
                        if(j!=x)s.insert(j);
                    }
                    p=x;
                }
                else if(x!=p)ans=i;
            }
            if(s.size()==n)ans=i;
        }
        cout<<ans<<'\n';
    }
    return 0;
}