题目意思;给你n次操作,每一次操作给你'+'或者'?'分别代表添加,和查询,之后跟一个数字。
'+',将这个数字放到集合中
'?',输出可被这个数字整除且不包含在集合中的最小的非负整数x。
思路;乍一眼看打算直接一个map存,然后每次都遍历操作的,但是因为时间复杂度太大肯定会超时,那么我们可以再深究一下题目:
因为题目中找的是可以被这个数整数而且不包含在集合中的最小的非负整数x,那么我们可以用一个额外的map去存当前x的最大的不包含在集合的一个数,然后每次可以根据这个最大的值去遍历一下我们的map,这样的时间复杂度会少很多。
代码:
/**
* ┏┓ ┏┓+ +
* ┏┛┻━━━┛┻┓ + +
* ┃ ┃
* ┃ ━ ┃ ++ + + +
* ████━████+
* ◥██◤ ◥██◤ +
* ┃ ┻ ┃
* ┃ ┃ + +
* ┗━┓ ┏━┛
* ┃ ┃ + + + +Code is far away from
* ┃ ┃ + bug with the animal protecting
* ┃ ┗━━━┓ 神兽保佑,代码无bug
* ┃ ┣┓
* ┃ ┏┛
* ┗┓┓┏━┳┓┏┛ + + + +
* ┃┫┫ ┃┫┫
* ┗┻┛ ┗┻┛+ + + +
*/
#include<cstdio>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <string>
#include <math.h>
#include<vector>
#include<queue>
#include<map>
#define sc_int(x) scanf("%d", &x)
#define sc_ll(x) scanf("%lld", &x)
#define pr_ll(x) printf("%lld", x)
#define pr_ll_n(x) printf("%lld\n", x)
#define pr_int_n(x) printf("%d\n", x)
#define ll long long
using namespace std;
const int N=1000000+100;
int n ,m,h;
ll s[N];
map<ll,ll>q,last;
int main()
{
int t;
sc_int(t);
q[0]=1;
while(t--)
{
char x;ll y;
cin>>x>>y;
if(x=='+')q[y]=1;
else {
ll k=last[y];
while(q[k])k+=y;
last[y]=k;
cout<<k<<endl;
}
}
return 0;
}
如果是和直接t了的代码的一个最主要的区别就是,我们在每次查询之后都保留了被数字整除且不包含在集合中的最小的非负整数x,而如果产生了多次的查询那么以前的代码一定会t