首页 > 代码库 > Soldier and Badges (set的检索简单运用)

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

4

1 3 1 4

Output

1

思路:让这些数全都不一样就可以了!!!建立数组循环,从第二个开始,每次检索并增加,知道set堆里面没有相同的位置为止!!!

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<string>
 5 #include<set>
 6 #include<vector>
 7 #include<stack>
 8 #include<queue>
 9 #include<algorithm>
10 #include<iostream>
11 #include<cstdio>
12 #include<algorithm>
13 using namespace std;
14 int main()
15 {
16 
17     int n;
18     cin>>n;
19     set <int> s;
20     int a[3005];
21     int flag=0;
22     for(int i=0;i<n;i++)
23         cin>>a[i];
24         s.insert(a[0]);
25         set<int>::iterator it;
26     for(int i=1;i<n;i++)
27     {
28         it=s.find(a[i]);
29        while(it!=s.end())
30         {
31             a[i]++;
32             flag++;
33             it=s.find(a[i]);
34 
35         }
36         s.insert(a[i]);
37 }
38 cout<<flag<<endl;
39 
40 
41     return 0;
42 }

 

Soldier and Badges (set的检索简单运用)