CodeForces - 546B Soldier and Badges

Colonel has n badges. He wants to give one badge to every of his n soldiers. Each badge has a coolness factor, which shows how much it’s owner reached. Coolness factor can be increased by one for the cost of one coin.

For every pair of soldiers one of them should get a badge with strictly higher factor than the second one. Exact values of their factors aren’t important, they just need to have distinct factors.

Colonel knows, which soldier is supposed to get which badge initially, but there is a problem. Some of badges may have the same factor of coolness. Help him and calculate how much money has to be paid for making all badges have different factors of coolness.

Input

First line of input consists of one integer n (1 ≤ n ≤ 3000).

Next line consists of n integers ai (1 ≤ ai ≤ n), which stand for coolness factor of each badge.

Output

Output single integer — minimum amount of coins the colonel has to pay.

Examples

Input
4
1 3 1 4
Output
1
Input
5
1 2 3 2 5
Output
2

Note

In first sample test we can increase factor of first badge by 1.

In second sample test we can increase factors of the second and the third badge by 1.

题意分析

给你一串长度为n的序列(序列中每一个数都小于等于n),每个数可以加上某个数,使得整个序列中没有相同的数字,求加的最小的和是多少。

刚开始把题意理解错了,误认为加完以后整个序列就是1到n的排列,但不一定是这样,最小值不一定是1.
做题的思路就是首先把序列中每个数字出现的个数统计一下,记录在rec数组里边,然后遍历rec数组,如果某个数他的个数超过了1,就往后便利,找一个个数为零的数,ans+=找到的数减去这个数。
遍历完,就求得了答案

AC代码

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

const int maxn=6e3+10;

int rec[maxn];

int main(){
	int n;
	cin>>n;
	int a[maxn];
	for(int i=0;i<n;i++){
		cin>>a[i];
	} 
	
	for(int i=0;i<n;i++){
		rec[a[i]]++;
	}
	

//	for(int i=1;i<=2*n;i++)
//		cout<<rec[i]<<" "; 
//	cout<<endl;

	int ans=0;
	for(int i=1;i<=n;i++){ 
		while(rec[i]>1){
//			cout<<"Rec[i]=="<<rec[i]<<endl; 
			for(int j=i;j<=2*n;j++){
				if(!rec[j]){
					rec[j]++;
					rec[i]--;
					ans+=j-i;
//					cout<<"ans=="<<ans<<endl;
//					for(int i=1;i<=2*n;i++)
//						cout<<rec[i]<<" "; 
//					cout<<endl;
					break; 
				}
			}
		}
	}
//	cout<<"sum0=="<<sum0<<"sum=="<<sum<<endl;
	
	cout<<ans;
	return 0;
}

心得体会

当出现wa的时候,先检查一下自己哪里错了,然后再重新读题,看看是不是自己把题目理解错了

第一个bug:求这个rec数组的时候,应该是:

	for(int i=0;i<n;i++){
		rec[a[i]]++;
	}

但是自己疏忽,把rec[a[i]]写成了rec[i];这样便利一遍就是求1到n每个数字出现多少次,自然是每个出现一次。

第二个bug:
当出现个数大于1的数字时,要往后遍历出现次数为0的数,找到了以后,应该是

if(!rec[j]){
	rec[j]++;
	rec[i]--;
	ans+=j-i;
	break; 
}

千万千万不能忘了break.你执行完操作以后要break跳出循环,不然他就自己继续往后走找出现次数为零的数了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值