B - Soldier and Badges
Time Limit:3000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u
Description
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.
Sample Input
Input4
1 3 1 4Output1Input5
1 2 3 2 5Output2Hint
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。
首先我们设一个数组a来储存所有n个数字,按升序排序。对a数组扫一遍,如果a[i]==a[i+1],则需要改变数的大小。那么我们怎么知道要加多少个一呢?在本题中我设了一个ans标记用来表示比a[i+1]大且不存在a中的最小整数,ans-a[i+1]即为我们要加1的个数。
注意:每次加1之后整个a数组就会发生改变,为此我定义了一个数组b来表示变化后的a数组,以便于求ans。
附AC代码:
#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std; int a[],b[]; int main(){
int n,t;
cin>>n;
for(int i=;i<n;i++){
cin>>a[i];
b[i]=a[i];//b数组来表示变化后的a数组
}
sort(a,a+n);//排序
sort(b,b+n);
int ans=a[];
int sum=;
for(int i=;i<n;i++){
if(a[i]==ans){
ans++;
}
}
for(int j=;j<n;j++){
if(a[j]==a[j+]){
sum+=(ans-a[j+]);
b[j+]=ans;//变化
ans++;
}
else if(a[j+]>ans){//保证ans不小于a[j+1]
ans=a[j+];
}
for(int i=;i<n;i++){
if(b[i]==ans){
ans++;
}
}
}
cout<<sum; return ;
}
B - Soldier and Badges的更多相关文章
- CF Soldier and Badges (贪心)
Soldier and Badges time limit per test 3 seconds memory limit per test 256 megabytes input standard ...
- Codeforces Round #304 (Div. 2) B. Soldier and Badges 水题
B. Soldier and Badges Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/54 ...
- 贪心 Codeforces Round #304 (Div. 2) B. Soldier and Badges
题目传送门 /* 题意:问最少增加多少值使变成递增序列 贪心:排序后,每一个值改为前一个值+1,有可能a[i-1] = a[i] + 1,所以要 >= */ #include <cstdi ...
- CF 546 B Soldier and Badges(贪心)
原题链接:http://codeforces.com/problemset/problem/546/B 原题描述: Soldier and Badges Colonel has n badges. H ...
- Codeforces Round #304 (Div. 2) B. Soldier and Badges【思维/给你一个序列,每次操作你可以对一个元素加1,问最少经过多少次操作,才能使所有元素互不相同】
B. Soldier and Badges time limit per test 3 seconds memory limit per test 256 megabytes input standa ...
- 「CodeForces 546B」Soldier and Badges 解题报告
CF546B Soldier and Badges 题意翻译 给 n 个数,每次操作可以将一个数 +1,要使这 n 个数都不相同, 求最少要加多少? \(1 \le n \le 3000\) 感谢@凉 ...
- 题解 CF546B Soldier and Badges
CF546B Soldier and Badges 简单的贪心qwq 排个序,如果当前数与之前的数相重,已经用过,则加到一个之前没有用过的数 #include<cstdio> #inclu ...
- Soldier and Badges (set的检索简单运用)
Colonel has n badges. He wants to give one badge to every of his n soldiers. Each badge has a coolne ...
- 【codeforces 546B】Soldier and Badges
time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
随机推荐
- PYTHON 源码
http://www.wklken.me/index2.html http://blog.csdn.net/dbzhang800/article/details/6683440
- UICollectionView 讲解
什么是UICollectionView UICollectionView是一种新的数据展示方式,简单来说可以把他理解成多列的UITableView(请一定注意这是 UICollectionView的最 ...
- HDU2897( 巴什博奕变形)
邂逅明下 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submissi ...
- angular 的 GET 请求 和 POST 请求的 区别 及 实现
1.GET 请求 .factory('AlarmService', ['$rootScope','ENV','$resource','$http','ionicToast',function($roo ...
- linked-list-cycle-ii——链表,找出开始循环节点
Given a linked list, return the node where the cycle begins. If there is no cycle, returnnull. Follo ...
- to_char(sysdate,'D')的用法
select to_char(sysdate,'D') from dual; 这周的第几天:Day of week (1–7) 1,2,3,4,5,6,7--注意:每星期的第1天是”星期日“ se ...
- 文件宝局域网传输/播放功能使用帮助(Windows电脑用户)
使用局域网账户密码登录,可以访问电脑上所有文件 使用游客无账户密码登录,只能访问电脑上指定共享文件夹的文件. 1.怎么设置共享文件夹请参考: 方法1 1.在文件资源管理器中选择自己一个想共享的文件夹, ...
- POJ 2482 Stars in Your Window(线段树+扫描线)
题目链接 非常不容易的一道题,把每个点向右上构造一个矩形,将问题转化为重合矩形那个亮度最大,注意LL,注意排序. #include <cstdio> #include <cstrin ...
- 登录日志的访问日志的 统计 MapReduce
登录日志的访问日志的 统计 MapReduce <!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-commo ...
- Spark理论学习笔记(一)
1.调度 分为FIFO和FAIR两种模式 创建调度池:sc.setLocalProperty("spark.scheduler.pool", "pool6") ...