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 ...
随机推荐
- Filter和Interceptor的终归作用还是从入口修改或验证请求进来的数据
Filter是Java EE标准.Inteceptor是Spring 标准. Filter在servlet前面,Interveptor在servlet之后 Filter和Inteceptor都可以改变 ...
- 标准IO函数以及基本知识点总结
什么是标准IO呢?有哪些特点? 标准IO是标准c库提供的对文件操作的函数接口.他的特点是:1 带缓存,2 大部分都调用系统接口函数实现.(c库就是一种实现好的函数接口,作用是屏蔽下层细节.提供上层接口 ...
- Qt5官方demo解析集30——Extending QML - Binding Example
本系列全部文章能够在这里查看http://blog.csdn.net/cloud_castle/article/category/2123873 接上文Qt5官方demo解析集29--Extendin ...
- String、StringBuffer和StringBuilder的差别
String.StringBuffer和StringBuilder的差别 1.String类是不可变类,即一旦一个String对象被创建后.包括这个对象中的字符序列是不可改变的 2.在字符串拼接的过程 ...
- 如何使用RDP跳过网络隔离?
简介 本文我将向大家演示,如何通过RDP跳转盒进入隔离/受保护的网络.下图是我为该场景制作的拓扑图: 简要说明: LAN是一种扁平的工作站和服务器网络. 一些服务器(包括RDP跳转盒)无法与Inter ...
- BUPT复试专题—寻找变化前01序列(2016)
题目描述 给你一个01序列,HDLC协议处理的话,如果出现连续的5个1会补1个0.例如1111110,会变成11111010. 现在给你一个经过HDLC处理后的01序列,你需要找到HDLC处理之前的0 ...
- UVa 10295 - Hay Points
题目:有非常多工人.相应一个能力描写叙述表,每种能力有一个权值,求每一个工人的能力值. 分析:字符串.hash表,字典树.利用散列表或者字典树存储相应的单词和权值.查询就可以. 说明:注意初始化,计算 ...
- 怎样改动X-code中的字体大小、颜色
- 老大写得一个非常高大上的Makefile,包括非常多语法:
一个非常高大上的Makefile,包括非常多语法: TARGET = api-login INSTALL_PATH = /huishoubao/cgi include ../../implements ...
- requirejs中的define
关于requirejs中的define的原理理解 我们已经了解到模块模式是为单例创建私有变量和特权方法的.一个最基本的例子: var foo=(function(){ var something= ...