codeforces1294D(思维+暴力)
题意描述
Recall that MEX of an array is a minimum non-negative integer that does not belong to the array. Examples:
for the array [0,0,1,0,2] MEX equals to 3 because numbers 0,1 and 2 are presented in the array and 3 is the minimum non-negative integer not presented in the array; for the array [1,2,3,4] MEX equals to 0 because 0 is the minimum non-negative integer not presented in the array; for the array [0,1,4,3] MEX equals to 2 because 2 is the minimum non-negative integer not presented in the array. You are given an empty array a=[] (in other words, a zero-length array). You are also given a positive integer x.
You are also given q queries. The j-th query consists of one integer yj and means that you have to append one element yj to the array. The array length increases by 1 after a query.
In one move, you can choose any index i and set ai:=ai+x or ai:=ai−x (i.e. increase or decrease any element of the array by x). The only restriction is that ai cannot become negative. Since initially the array is empty, you can perform moves only after the first query.
You have to maximize the MEX (minimum excluded) of the array if you can perform any number of such operations (you can even perform the operation multiple times with one element).
You have to find the answer after each of q queries (i.e. the j-th answer corresponds to the array of length j).
Operations are discarded before each query. I.e. the array a after the j-th query equals to [y1,y2,…,yj].
给定Q个询问和一个值x,每次询问向空数组中插入一个数,对于一个数可以进行无限次的加x和减x的操作,对于每次询问,求最大的mex值
思路
首先是我的暴力做法:
由于每次都可以进行加减 x x x的操作,所以可以保存插入的数对 x x x求余的结果,如果一个数字对 x x x求余后的余数已经存在,则该数字为a[i]=(value%x)+(cnt[value%x]-1)*x;
。当处理完插入的q个数字以后,就从头开始处理,如果ans没有出现过,则输出ans,否则就找到最小的没有出现过的ans。第一次暴力时,对于没有出现过的ans,我选择从头开始找,结果在第三个样例TLE。后来考虑维护一个最大值,因为答案保证一定在 [ a n s , m a x n + 1 ] [ans,maxn+1] [ans,maxn+1]这个区间内,所以每次就在这个区间范围内找答案即可。
然后是题解做法:
由于每次可以任意次数的加减 x x x,所以我们可以使用一个数组来记录余数的个数,最后用ans去递增查询是否满足要求,如果num[ans%x]的值不为0,则说明之前有一个没有发挥作用的数可以顶替ans,则让ans+1去查询下一个。
(和我的思路差不多,只是优化的更多)
AC代码
1.暴力做法
#include<bits/stdc++.h>
#define x first
#define y second
#define PB push_back
#define mst(x,a) memset(x,a,sizeof(x))
#define all(a) begin(a),end(a)
#define rep(x,l,u) for(ll x=l;x<u;x++)
#define rrep(x,l,u) for(ll x=l;x>=u;x--)
#define sz(x) x.size()
#define ins(x) inserter(x,x.begin())
#define IOS ios::sync_with_stdio(false);cin.tie(0);
using namespace std;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef pair<long,long> PLL;
typedef pair<char,char> PCC;
typedef long long ll;
const int N=4*1e5+10;
const int M=1e6+10;
const int INF=0x3f3f3f3f;
const int MOD=1e9+7;
ll a[N];
void solve(){
int q,value;cin>>q>>value;
map<int,int> cnt;
map<ll,int> used;
rep(i,0,q){
int x;cin>>x;
x%=value;
cnt[x]++;
a[i]=x+(cnt[x]-1)*value;
}
ll ans=0,maxn=0;
rep(i,0,q){
used[a[i]]=1;
maxn=max(maxn,a[i]);
if(!used[ans]) cout<<ans<<endl;
else{
//优化
rep(j,ans,maxn+2){
if(!used[j]){
ans=j;
break;
}
}
cout<<ans<<endl;
}
}
}
int main(){
IOS;
//int t;cin>>t;
//while(t--){
solve();
//}
return 0;
}
2.题解做法
#include<bits/stdc++.h>
#define x first
#define y second
#define PB push_back
#define mst(x,a) memset(x,a,sizeof(x))
#define all(a) begin(a),end(a)
#define rep(x,l,u) for(ll x=l;x<u;x++)
#define rrep(x,l,u) for(ll x=l;x>=u;x--)
#define sz(x) x.size()
#define ins(x) inserter(x,x.begin())
#define IOS ios::sync_with_stdio(false);cin.tie(0);
using namespace std;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef pair<long,long> PLL;
typedef pair<char,char> PCC;
typedef long long ll;
const int N=4*1e5+10;
const int M=1e6+10;
const int INF=0x3f3f3f3f;
const int MOD=1e9+7;
int cnt[N];
void solve(){
int q,x;cin>>q>>x;
int ans=0;
while(q--){
int y;cin>>y;
cnt[y%x]++;
while(cnt[ans%x]){
cnt[ans%x]--;
ans++;
}
cout<<ans<<endl;
}
}
int main(){
IOS;
//int t;cin>>t;
//while(t--){
solve();
//}
return 0;
}
- 利用placeholder属性来添加输入框默认文字提示,提高用户体验
- Linux系统监控、诊断工具之top命令详解
- 【Dev Club分享】iOS黑客技术大揭秘
- Linux终端:用cat命令查看不可见字符
- golang 函数定义及其接口实例
- 分享两种圣诞节雪花特效JS代码(网站下雪效果)
- React 移动 web 极致优化
- golang 高效字符串拼接
- Linux+Nginx/Apache/Tomcat新增SSL证书,开启https访问教程
- golang 使用时间通过md5生成token
- golang中对map操作类
- Nginx在线服务状态下平滑升级或新增模块的详细操作记录
- 【Dev Club分享】微信读书iOS性能优化
- [svn: E155004]svn update报database is locked错误的解决办法
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- nginx优化之keepalive
- 血泪教训,线程池引发的内存泄露
- RestTemplate设置转换类型
- 详解PROTOCOL BUFFERS
- Spring boot常用注解收集
- Go依赖模块版本之Module避坑使用详解
- 【tcl学习】vivado write_edif
- K8s中优雅停机和零宕机部署
- Python读取.edf格式脑电数据文件
- Kotlin修炼指南(三)——奇技淫巧
- 前端|CSS盒阴影和文字阴影
- 一文读懂Spring Boot各模块组件依赖关系
- 谈一谈|MkDocs介绍及应用
- 讲得最明白的Elasticsearch源码调试环境搭建教程
- 史上最全ThreadPoolExecutor梳理(下篇)