E. MEX and Increments

E. MEX and Increments

链接

题意:数组每一个数都可以加任意次数的1 求当前数组中缺失为i(0<=i<=n)的操作个数

思路:
每一个i 都是构成前面的操作次数加上将当前值有的个数
如果当前值的个数为0 操作次数为 构成前面序列的操作个数 操作个数要加上前面最大可变的差值去构成当前的值的序列
如果当前值的个数不为0 操作个数为构成前面的操作个数+当前值的个数

代码:

#include <bits/stdc++.h>
using namespace std;

const int N = 2e5 + 10 ;
int all[N] ;  
typedef pair<int ,int> PII ;

int main(){
	int t;
	cin >>t ;
	while(t -- ) {
		int  n ;
		cin>>n;
		map<long long, long long> p ;
		for(int i = 0 ; i < n; i ++ ) cin>>all[i] , p[all[i]] ++ ; 
		long long  sum = 0 , ch = 0 ; 
		if(!p[0]) {
			cout<<"0 " ; 
			for(int i = 1 ; i <= n ; i ++ ) {
				cout<<-1 <<" " ;
			} 
			cout<<endl;
			continue ; 
		}
		int ok = 1 ; 
		cout<<p[0] <<" " ;
		p[0] -- ; 
		priority_queue<PII> q;
		if(p[0] != 0) q.push({0 , p[0]}) ; 
	
		for(int i = 1 ; i <= n;i ++ ) {
			if(ok == 0) cout<<"-1 " ; 
			else if(p[i] == 0 ) {
				cout<< ch <<" " ; 
				if(q.size() != 0 ){
					auto t = q.top() ;
					q.pop();
					long long x = t.first , y =t.second; 
					ch += i - x ; 
					y -- ; 
					if(y != 0 ) {
						q.push({x , y }) ; 
					}
				}
				else {
					ok = 0 ;
					
				}
			}
			else {
				cout<<ch + p[i] <<" " ;
				p[i] -- ;
				if(p[i]){
					q.push({i , p[i]}) ; 
				} 
			}
			
		} 
		cout <<endl;
	} 
	return 0 ; 
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值