CF 546 B Soldier and Badges(贪心)
原题链接:http://codeforces.com/problemset/problem/546/B
原题描述:
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.
Example
4
1 3 1 4
1
5
1 2 3 2 5
2
题目感觉很水啊,数量最多才3000个,就算3000个每个都是3000增加后的最大值也不会超过6000,所以我开了个10005的数组;先将每个读入,然后统计1-10000之间每个数字的个数,比如说3000这个数字有2个那么b[3000]值为3,然后让b[3001]增加2,b[3000]变为1,同时需要增加的总量增加2以此类推统计增加的总量即可,也不知道别人看不看得懂。。。。看看代码应该能理解
# include <stdio.h>
# include <string.h>
# include <stdlib.h>
# include <iostream>
# include <fstream>
# include <vector>
# include <queue>
# include <stack>
# include <map>
# include <math.h>
# include <algorithm>
using namespace std;
# define pi acos(-1.0)
# define mem(a,b) memset(a,b,sizeof(a))
# define FOR(i,a,n) for(int i=a; i<=n; ++i)
# define For(i,n,a) for(int i=n; i>=a; --i)
# define FO(i,a,n) for(int i=a; i<n; ++i)
# define Fo(i,n,a) for(int i=n; i>a ;--i)
typedef long long LL;
typedef unsigned long long ULL; int a[],b[]; int main()
{
int n,sum=;
cin>>n;
for(int i=;i<=n;i++)
{
cin>>a[i];
b[a[i]]++;
}
for(int i=;i<=;i++)
{
if(b[i]>)b[i+]+=b[i]-,sum+=b[i]-,b[i]=;
}
cout<<sum<<endl;
return ;
}
最新文章
- html5的web存储
- 怎样使java程序减少内存占用(转载)
- MFC-01-Chapter01:Hello,MFC---1.3 第一个MFC程序(01)
- ios 在storyboard 和 xib中,显示自定义view的预览效果
- [leetcode] Contains Duplicate II
- mysql三
- PHP文件下载原理
- react 全局面包屑
- Oracle之Merge用法
- linux系统下svn服务器操作命令
- jfinal获取服务器的IP和端口
- PHP中file_exists与is_file、is_dir的区别,以及执行效率的比较
- http://codeforces.com/contest/610/problem/D
- 【推荐】Data Structure Visualizations
- Elasticsearch-6.7.0系列(二)ES集群安装与验证
- P4782 【模板】2-SAT 问题
- C#多线程写日志
- http-server 基于nodejs的http服务器
- 理解JavaScript继承(三)
- 复习前面一个月的学习C#感觉道路好艰难啊