CF Soldier and Badges (贪心)
3 seconds
256 megabytes
standard input
standard output
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.
4
1 3 1 4
1
5
1 2 3 2 5
2 可以得出一个简单的结论,如果有相同的几个出现,那么重复这几个一定要改变,而且只能增大,所以把每一个增大到距离它最近的没有用过那个位置。至于为什么这样的贪心策略是对的。。。。我也不知道,感觉罢了。
#include <iostream>
#include <fstream>
#include <cstdio>
#include <string>
#include <queue>
#include <vector>
#include <map>
#include <algorithm>
#include <cstring>
#include <cctype>
#include <cstdlib>
#include <cmath>
#include <ctime>
using namespace std; const int SIZE = ;
bool HAVE[SIZE + SIZE];
int N,D_P;
int S[SIZE],DEAL[SIZE];
vector<int> CHOOSE[SIZE]; int main(void)
{
scanf("%d",&N);
for(int i = ;i < N;i ++)
{
scanf("%d",&S[i]);
if(HAVE[S[i]])
DEAL[D_P ++] = S[i];
else
HAVE[S[i]] = true;
} int ans = ;
for(int i = ;i < D_P;i ++)
for(int j = DEAL[i] + ;;j ++)
if(!HAVE[j])
{
ans += j - DEAL[i];
HAVE[j] = true;
break;
}
printf("%d\n",ans); return ;
}
CF Soldier and Badges (贪心)的更多相关文章
- 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,有可能a[i-1] = a[i] + 1,所以要 >= */ #include <cstdi ...
- 题解 CF546B Soldier and Badges
CF546B Soldier and Badges 简单的贪心qwq 排个序,如果当前数与之前的数相重,已经用过,则加到一个之前没有用过的数 #include<cstdio> #inclu ...
- 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,问最少经过多少次操作,才能使所有元素互不相同】
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\) 感谢@凉 ...
- 【codeforces 546B】Soldier and Badges
time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 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 ...
- B - Soldier and Badges
Time Limit:3000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Description Colone ...
随机推荐
- 获取和设置localStorage
东钿金融服务平台 用户第一次访问页面出现,引导步骤,起初一直使用cookie,但是cookie一直不稳定 今天老大说改用localStorage 于是乎百度,查了一篇博客 http://www.cnb ...
- [Sparrow OS 设计文档连载(一)] Introduction
- Unity3d:如何让程序在失去焦点时,继续运行,而不是暂停呢?
问题描述如题.解决方案: <ignore_js_op> <ignore_js_op>
- 学习web前端前感
我与IT 不知不觉二十个春夏秋冬过去了,我也从一个小孩变成了大人......然而并没什么卵用!这二十年来一直贴着“菜鸟”这样的标签,自小爱朝抵抗力小的方向走,这陋习至今还蔓延着,让人看来像是怒其不争的 ...
- PPAS上运行pg_dump经过
目前我有两台机器, 分别已经安装了PPAS9.1,安装后建立了OS系统用户enterprisedb和数据库用户enterprisedb. 机器1:master 192.168.10.88 机器2: ...
- ASP.net中GridView中增加一行记录并默认显示为编辑状态
//添加 protected void Button1_Click(object sender, EventArgs e) { DataSet ds = (DataSet)pa.GetDataSet( ...
- iOS 设计模式之抽象工厂
设计模式是程序提升的必备知识,这里说下iOS怎样实现抽象工厂设计模式.本文是看过oc编程之道这本的抽象工厂这章后写出的,假设不明确原理能够看看那本书. TestView.h首先创建一个视图 // // ...
- 浏览器禁用Cookie,基于Cookie的会话跟踪机制失效的解决的方法
当浏览器禁用Cookies时.基于Cookie的会话跟踪机制就会失效.解决的方法是利用URL重写机制跟踪用户会话. 在使用URL重写机制的时候须要注意.为了保证会话跟踪的正确性,全部的链接和重定向语句 ...
- IIS6_IIS7日志文件位置
准备统计下页面访问量 查找IIS日志,发现在以前IIS6日志的位置,竟然木有找到日志... 查看下IIS设置,发现IIS7和6的默认日志位置不一样额... IIS 6 Log files locati ...
- PHP|开发必知的良好实践
过滤.验证.转义 所有这些外部资源都不能完全相信 $_GET $_POST $_REQUEST $_COOKIE $argv php://stdin php://input file_get_cont ...