首页 > 其他 > 详细

so easy

时间:2019-09-08 14:25:02      阅读:80      评论:0      收藏:0      [点我收藏+]

题目地址

so easy(The Preliminary Contest for ICPC Asia Xuzhou 2019)

题干

技术分享图片
技术分享图片

代码和解释

本题需要逆向思维。如果是存储所有元素(1e9),然后删除not available的元素或设置标签,那么会超时。所以我们只存not available的元素(1e6),然后判断元素是否被存在 set 里已确定其是否available。
c++代码如下:

#include<bits/stdc++.h>
using namespace std;
int main()
{
    long long n,q;
    int z,x;
    set<int> a;//从小到大,不可用的元素集合 
    set<int>::iterator it;
    int tmp;
    scanf("%lld%lld",&n,&q);
    while(q--){
        scanf("%d%d",&z,&x);
        if(z==1){
            a.insert(x);
        }
        else if(z==2){
            tmp=x;
            while(1){
                if(!a.count(tmp)){//tmp可用 
                    printf("%d\n",tmp);
                    break;
                }
                tmp++;
                if(tmp>n){
                    printf("%d\n",-1);
                    break;
                }
            }
        }
    }
    return 0; 
}

so easy

原文:https://www.cnblogs.com/hardcoreYutian/p/11485897.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!