7.18
来源:互联网 发布:windows 平板电脑推荐 编辑:程序博客网 时间:2024/05/29 02:45
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.
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 single integer — minimum amount of coins the colonel has to pay.
41 3 1 4
1
51 2 3 2 5
2贪心算法:先排一次序,然后找相同的,是相同的++,在排序在返复这样的操作,但是这样肯定会超时,反复排序! (注意有几个>2个数相同的情况)所以 直接拍一次序,要是后面的比前面的的小或等于,就执行:a[i]=a[i-1]+1;还看到过别人的另外一种做法,直接用该数组的元素和对应的下标比较源代码链接:#include <iostream>#include <cstdio>#include <algorithm>using namespace std;int main(){ int a[3000]; int i,j,k; int sum1,sum2; sum1=sum2=0; int n; cin>>n; for (i=0;i<n;i++) { scanf("%d",&a[i]); sum1+=a[i]; } sort(a,a+n); k=0; for (i=0;i<n;i++) { if (a[i+1]==a[i] && i<n) { a[i+1]++; } if (a[i+1]<a[i] && i<n)//用来判断这一数组元素有两个以上相等的情况 { a[i+1]+=a[i]-a[i+1]+1; } sum2+=a[i]; } printf("%d\n",sum2-sum1); return 0;}
- 7.18
- 7.18
- 7.18
- 7.18
- 7.18
- 7.18
- 7.18
- 7.18
- 7.18
- 7.18
- 7.18
- 7.18
- 7.18小雨
- 7.18计划
- 建议7.18
- 7.18计划
- MZOI-7.18
- C++7.18
- iOS自动布局框架-Masonry详解
- Spring中ApplicationContext加载机制和配置初始化
- POJ 3044 City Skyline 笔记
- checkException和RuntimeException
- linux文件管理
- 7.18
- 类初始化的时机
- 为什么有些人充满活力,有人却十分憔悴?
- Python+Selenium利用link text定位元素
- solr对nested json(多层复杂嵌套json)的文档式处理方式。childDoc&parentDoc _childDocuments_
- 调用线程无法访问此对象,因为另一个线程拥有该对象
- 一些函数、指针、const、结构体、extern零散整理
- Android自定义view-高仿小米视频加载动画效果
- Glide .centerCrop() 和transform圆角图片不能同时存在解决方法