Soldier and Badges

题目链接:http://acm.hust.edu.cn/vjudge/problem/visitOriginUrl.action?id=173144

题意:

输入n个数,要使这n个数都不相同,且只能加,输出最少要加的多少。

案例:

1)input

4

1 3 1 4

output

1

2)input

5

1 2 3 2 5

output

2

思路分析:

利用插空法。尽量减少循环。

先对数组进行排序,再找出有x个相同的数,且把相同的数存在另一个数组c中。因为这个数组c也是排序的,所以只要找出x个比c大的缺少的数。

最后,只要加上所有数组b中的数减去数组c中的数,就是所要输出的数。

源代码如下:

 1 #include<iostream>
 2 #include<algorithm>
 3 #define max 3000
 4 using namespace std;
 5 int main()
 6 {
 7     int n,a[max],i,count=0,j=0,k,b[max],c[max],x=0;
 8     cin>>n;
 9     for(i=0;i<n;i++)
10         cin>>a[i];
11     sort(a,a+n);
12     k=a[0];
13     for(i=0;i<n;i++)
14         if(a[i]==a[i+1])
15         {
16             c[x]=a[i];
17             x++;
18         }
19     while(1)
20     {
21         for(i=j;i<n;i++)
22             if(k==a[i])
23                break;
24         if(i>=n)
25             if(k>c[j])
26             {
27                 b[j]=k;
28                 j++;
29             }
30         if(j>=x)
31             break;
32         k++;
33     }
34     for(i=0;i<x;i++)
35         count+=b[i]-c[i];
36     cout<<count<<endl;
37     return 0;
38 }
时间: 2024-08-01 17:43:36

Soldier and Badges的相关文章

Week 1 # O Soldier and Badges

原题描述: O - 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

贪心 Codeforces Round #304 (Div. 2) B. Soldier and Badges

题目传送门 1 /* 2 题意:问最少增加多少值使变成递增序列 3 贪心:排序后,每一个值改为前一个值+1,有可能a[i-1] = a[i] + 1,所以要 >= 4 */ 5 #include <cstdio> 6 #include <cstring> 7 #include <algorithm> 8 using namespace std; 9 10 typedef long long ll; 11 12 const int MAXN = 3e3 + 10;

Codeforces Round #304 (Div. 2)——1002—— 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 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 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 o

O - 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 o

CF 546B Soldier and Badges

Describe 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 soldi

546B. Soldier and Badges

题目链接 题意: n个数,要保证这n个数完全不相同,求需要把原来的数增加多少,求这个值得最小值   Java 程序 import java.io.PrintStream; import java.util.Arrays; import java.util.Scanner; import java.util.Set; import java.util.TreeSet; public class B546 { static void run(){ Scanner in = new Scanner(S

codeforce 304 B. Soldier and Badges

<span style="font-family: 'Courier New'; background-color: rgb(255, 255, 255);"> </span><span style="font-family: 'Courier New'; background-color: rgb(255, 255, 255);">第一次打cf,感觉挺爽的,页面看着很爽,大概是因为这次的比较简单吧,这道题有点像FZU的最小花费,

Codeforces546B:Soldier and Badges

题意: 给出一些数字,要求每个数字都不一样需要增加几 思路: 先排序,然后一个个增加,最后求总和差 #include <iostream> #include <stdio.h> #include <string.h> #include <string> #include <stack> #include <queue> #include <map> #include <set> #include <vec