D1. Balance (Easy version) Codeforces Round #830 (Div. 2)

传送门

 

 

 

 题目意思;给你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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值