MEX and Increments
MEX and Increments
题面翻译
给你一个数列,每次可以花费 111 代价将其中一个数加一。从 000 到 nnn 分别输出至少要花费多少才能使这个数是数列中没有出现的最小的非负整数。
题目描述
Dmitry has an array of nnn non-negative integers a1,a2,…,an,n≤2∗105a_1, a_2, \dots, a_n,n\leq 2*10^{5}a1,a2,…,an,n≤2∗105 .
In one operation, Dmitry can choose any index jjj ( 1≤j≤n1 \le j \le n1≤j≤n ) and increase the value of the element aja_jaj by 111 . He can choose the same index jjj multiple times.
For each iii from 000 to nnn , determine whether Dmitry can make the MEX\mathrm{MEX}MEX of the array equal to exactly iii . If it is possible, then determine the minimum number of operations to do it.
The MEX\mathrm{MEX}MEX of the array is equal to the minimum non-negative integer that is not in the array. For example, the MEX\mathrm{MEX}MEX of the array [3,1,0][3, 1, 0][3,1,0] is equal to 222 , and the array [3,3,1,4][3, 3, 1, 4][3,3,1,4] is equal to 000 .
样例 #1
样例输入 #1
5
3
0 1 3
7
0 1 2 3 4 3 2
4
3 0 0 0
7
4 6 2 3 5 0 5
5
4 0 1 0 4
样例输出 #1
1 1 0 -1
1 1 2 2 1 0 2 6
3 0 1 4 3
1 0 -1 -1 -1 -1 -1 -1
2 1 0 2 -1 -1
提示
In the first set of example inputs, n=3n=3n=3 :
- to get MEX=0\mathrm{MEX}=0MEX=0 , it is enough to perform one increment: a1a_1a1 ++;
- to get MEX=1\mathrm{MEX}=1MEX=1 , it is enough to perform one increment: a2a_2a2 ++;
- MEX=2\mathrm{MEX}=2MEX=2 for a given array, so there is no need to perform increments;
- it is impossible to get MEX=3\mathrm{MEX}=3MEX=3 by performing increments.
自己解决了80%吧,有一段代码不知道怎么维护。
分析发现一些很明显的规律:
- 出现 -1 之后全是 -1;
- 对于 iii 需要满足 0∼i−10 \sim i-10∼i−1 都出现过,iii 不出现。
所以当从 i−1i-1i−1 转移到 iii 时,需要让一个不超过 i−1i-1i−1 的数变成 i−1i-1i−1 ,并且这个数没有使用过。
明显这个数是递增的。
大佬题解使用了栈维护这样一个数。
dtdtdt 不知道咋改的代码
ll n,m,_;
int a[N],sum[N];
void solve(){cin>>n;map<int,int>mp;fo(i,1,n)sum[i] = 0,a[i] = 0;fo(i,1,n){cin>>a[i];mp[a[i]]++;sum[a[i]]++;}fo(i,1,n){sum[i] += sum[i-1];}fo(i,0,n){cout<<i<<" "<<mp[i]<<" "<<sum[i]<<endl;;}cout<<endl;bool flag = 1 ,F = 1;ll dt = 0;fo(i,0,n){if(mp[i]<1){F = 0;}if(!flag){cout<<"-1 ";continue;}if(sum[i-1]<i){flag = 0;cout<<"-1 ";}else{// dt 怎么调和?if(F){cout<<sum[i-1] - i + mp[i]<<" ";}elsecout<<sum[i-1] - i + mp[i] + dt<<" ";}}cout<<endl;
}int main(){cin>>_;while(_--)solve();return 0;
}
非常妙的代码
/*
A: 10min
B: 20min
C: 30min
D: 40min
*/
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <queue>
#include <set>
#include <map>
#include <vector>
#include <sstream>
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define mem(f, x) memset(f,x,sizeof(f))
#define fo(i,a,n) for(int i=(a);i<=(n);++i)
#define fo_(i,a,n) for(int i=(a);i<(n);++i)
#define debug(x) cout<<#x<<":"<<x<<endl;
#define endl '\n'
using namespace std;
//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")template<typename T>
ostream& operator<<(ostream& os,const vector<T>&v){for(int i=0,j=0;i<v.size();i++,j++)if(j>=5){j=0;puts("");}else os<<v[i]<<" ";return os;}
template<typename T>
ostream& operator<<(ostream& os,const set<T>&v){for(auto c:v)os<<c<<" ";return os;}
template<typename T1,typename T2>
ostream& operator<<(ostream& os,const map<T1,T2>&v){for(auto c:v)os<<c.first<<" "<<c.second<<endl;return os;}
template<typename T>inline void rd(T &a) {char c = getchar(); T x = 0, f = 1; while (!isdigit(c)) {if (c == '-')f = -1; c = getchar();}while (isdigit(c)) {x = (x << 1) + (x << 3) + c - '0'; c = getchar();} a = f * x;
}typedef pair<int,int>PII;
typedef pair<long,long>PLL;typedef long long ll;
typedef unsigned long long ull;
const int N=2e5+10,M=1e9+7;
ll n,m,_;int a[N],stk[N],t;//t 栈顶指针
void solve(){cin>>n;map<int,int>mp;fo(i,1,n){cin>>a[i];mp[a[i]]++;}cout<<mp[0]<<" ";ll sum = 0;fo(i,1,n){for(int j=1;j<=mp[i-1];j++){stk[++t] = i-1;//从i-1转到i,如果有i-1的话,就存下来}if(!t){for(int j=1;j<=n-i+1;j++){cout<<"-1 ";}break;}else{sum += (i-1-stk[t--]);cout<<mp[i] + sum<<" ";}}cout<<endl;
}int main(){cin>>_;while(_--)solve();return 0;
}
MEX and Increments相关推荐
- E. MEX and Increments
E. MEX and Increments 链接 题意:数组每一个数都可以加任意次数的1 求当前数组中缺失为i(0<=i<=n)的操作个数 思路: 每一个i 都是构成前面的操作次数加上将当 ...
- #762 (Div. 3) E. MEX and Increments(贪心构造
E. MEX and Increments 题意: 思路: 贪心加模拟 code: #include<bits/stdc++.h> #define endl '\n' #define ll ...
- codeforces:E. MEX and Increments【贪心 + 略加优化】
分析 mex(a) 表示不在a中的最小非负整数 ans[i] 表示最少的操作数使得mex(a) = i 我们用个counter记录一下原始每个位置出现的次数 我们的思路很简单:找出缺口,然后把多余的平 ...
- Codeforces Round #762 (Div. 3)
E. MEX and Increments F. Let's Play the Hat? G. Unusual Minesweeper H. Permutation and Queries 用个优先 ...
- outlook安全模式修复_Outlook Com安全漏洞Microsoft无法修复
outlook安全模式修复 Microsoft is one of the biggest tech companies in the world, and yet their online emai ...
- BZOJ 3585: mex( 离线 + 线段树 )
离线, 询问排序. 先处理出1~i的答案, 这样可以回答左端点为1的询问.完成后就用seq(1)将1到它下一次出现的位置前更新. 不断这样转移就OK了 ------------------------ ...
- 构造 ---- 最小没出现过的数(逆向构造) D. Replace by MEX
题目链接 题目大意: 给你一个aaa序列长度为nnn,每次挑选一个位置pos∈[1,n]pos\in[1,n]pos∈[1,n],把apos=MEX(a)a_{pos}=MEX(a)apos=MEX ...
- [模板] 区间mex 区间元素种数
区间mex 问题 给定序列\({a_i}\), 每次询问给出\(l\), \(r\), 询问 \(\text{mex} \{a_i\}, i \in \{l, l+1, \cdots r\}\) 解法 ...
- 使用matlab进行mex编译时的路径问题mexopts
matlab和vs 进行混合编程时总须要使用matlab编译mexFunction.cpp文件. 这些文件免不了使用include下的*.h和lib下的*.lib文件.举例说明.这次我的cpp中用到了 ...
最新文章
- 【直播预告】云栖直播:阿里热修复产品HotFix2.0升级详解
- Swift - 移除页面视图上的所有元素
- 【PAT】A1074 Reversing Linked List ***
- 多线程的创建方式---继承Thread和实现Runnable
- android ListView常用知识总结
- Hive大数据-Hive的架构---大数据之Hive工作笔记0003
- hudi延迟日志命名
- 升级指南:将 SharePoint Portal Server 2003 升级到 Office SharePoint Server 2007
- UE4之整合第三方库
- spring cloud(一) 副 consul
- Dex2Oat执行参数总结
- Spark独到见解--Action算子总结
- lammps教程:不同原子组势能计算方法
- jshop测试分析总览
- vs c语言打印心形情书,最美C语言情书(输出心形图案).doc
- Dubbo笔记 ㉗ : 服务自省-提供者
- ccna 服务器输入域名显示不出,思科为服务器设置域名
- Vue2.0 饿了么报错: Unexpected side effect in lis tShow computed property
- ITOP资产信息登记指导书
- 男人的魅力不在于财富,而在于精神深度