1. 程式人生 > >Soldier and Badges (set的檢索簡單運用)

Soldier and Badges (set的檢索簡單運用)

space 建立 blog tput pan con ios output esp

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的檢索簡單運用)