MEX and Increments
题面翻译
给你一个数列,每次可以花费 1 1 1 代价将其中一个数加一。从 0 0 0 到 n n n 分别输出至少要花费多少才能使这个数是数列中没有出现的最小的非负整数。
题目描述
Dmitry has an array of n n n non-negative integers a 1 , a 2 , … , a n , n ≤ 2 ∗ 1 0 5 a_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 j j j ( 1 ≤ j ≤ n 1 \le j \le n 1≤j≤n ) and increase the value of the element a j a_j aj by 1 1 1 . He can choose the same index j j j multiple times.
For each i i i from 0 0 0 to n n n , determine whether Dmitry can make the M E X \mathrm{MEX} MEX of the array equal to exactly i i i . If it is possible, then determine the minimum number of operations to do it.
The M E X \mathrm{MEX} MEX of the array is equal to the minimum non-negative integer that is not in the array. For example, the M E X \mathrm{MEX} MEX of the array [ 3 , 1 , 0 ] [3, 1, 0] [3,1,0] is equal to 2 2 2 , and the array [ 3 , 3 , 1 , 4 ] [3, 3, 1, 4] [3,3,1,4] is equal to 0 0 0 .
样例 #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 = 3 n=3 n=3 :
- to get M E X = 0 \mathrm{MEX}=0 MEX=0 , it is enough to perform one increment: a 1 a_1 a1 ++;
- to get M E X = 1 \mathrm{MEX}=1 MEX=1 , it is enough to perform one increment: a 2 a_2 a2 ++;
- M E X = 2 \mathrm{MEX}=2 MEX=2 for a given array, so there is no need to perform increments;
- it is impossible to get M E X = 3 \mathrm{MEX}=3 MEX=3 by performing increments.
自己解决了80%吧,有一段代码不知道怎么维护。
分析发现一些很明显的规律:
- 出现 -1 之后全是 -1;
- 对于 i i i 需要满足 0 ∼ i − 1 0 \sim i-1 0∼i−1 都出现过, i i i 不出现。
所以当从 i − 1 i-1 i−1 转移到 i i i 时,需要让一个不超过 i − 1 i-1 i−1 的数变成 i − 1 i-1 i−1 ,并且这个数没有使用过。
明显这个数是递增的。
大佬题解使用了栈维护这样一个数。
d t dt dt 不知道咋改的代码
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]<<" ";
}
else
cout<<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;
}