博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
A-Mex Query-2018年第二届河北省大学生程序设计竞赛
阅读量:4537 次
发布时间:2019-06-08

本文共 1742 字,大约阅读时间需要 5 分钟。

Give

nn
n non-negative integers, please find the least non-negative integer that doesn’t occur in the
nn
n numbers.
输入:
The first line is an integer
TT
T, representing the number of test cases.
For each test case:
The first line of each test case is an integer
nn

n.

The second line of each test case are
nn
n non-negative integers
aia_i
a
i

题意:给你n个数,找出缺失的第一个非负整数.

题解:用map标记已经出现过数,然后从 0 到 1<<31枚举,

#include 
using namespace std;typedef long long ll;map
m;int main(){ int t; cin>>t; while(t--){ m.clear(); int n; cin>>n; unsigned tmp; for(int i=1;i<=n;i++){ scanf("%u",&tmp); m[tmp]=1; } unsigned i=0; while(i<=(1<<31)){ if(m[i]==0){ printf("%u\n",i); break; } i++; } } return 0; }

--------------分割线-5/2----------

放上一个不用stl的算法.

题解:将n个数sort后

1) 如果第一个不是 0 那就输出 0,否则跳到 2)

2) 枚举这n个数,如果 a[i]-a[i-1]>1 ,说明 a[i]a[i] 之间有其他数,输出 a[i]+1 ,枚举完所有数后都没有,则跳到 3)
3) 枚举完n个数后都发现都是连续的,那么可以知道 缺少的最小的非负数为 a[n]+1

#include 
using namespace std;const int N=2e5+5;int a[N];int main(){ int t; cin>>t; while(t--){ int n;cin>>n; for(int i=1;i<=n;i++){ scanf("%d",&a[i]); } sort(a+1,a+1+n); if(a[1]!=0){ puts("0"); } else{ int k=0; for(int i=1;i<=n;i++){ if(a[i]-a[i-1]>1){ printf("%d\n",a[i-1]+1); k=1; break; } } if(k==0){ printf("%d\n",a[n]+1); } } } return 0;}

转载于:https://www.cnblogs.com/-yjun/p/10800473.html

你可能感兴趣的文章
BestCoder Round #14 B 称号 Harry And Dig Machine 【TSP】
查看>>
hdu 1114 Piggy-Bank
查看>>
maven集成tomcat插件启动报错
查看>>
Boost库编译安装
查看>>
算法复习——数位dp(不要62HUD2089)
查看>>
Spark2.1.0——运行环境准备
查看>>
Codeforces 543.B Destroying Roads
查看>>
noip模拟赛 寻宝之后
查看>>
ZOJ2833*(并查集)
查看>>
外连接简要总结
查看>>
第一次作业-准备篇
查看>>
【C++】继承时构造函数和析构函数
查看>>
opencv源代码之中的一个:cvboost.cpp
查看>>
swift
查看>>
pycharm 快捷键
查看>>
Linux常用命令
查看>>
.net中的设计模式---单例模式
查看>>
安装程序工具 (Installutil.exe)22
查看>>
如何简单解释 MapReduce算法
查看>>
从 0 到 1 实现 React 系列 —— 1.JSX 和 Virtual DOM
查看>>