(模拟)B. Settlers' Training

B. Settlers' Training

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

In a strategic computer game "Settlers II" one has to build defense structures to expand and protect the territory. Let's take one of these buildings. At the moment the defense structure accommodates exactly n soldiers. Within this task we can assume that the number of soldiers in the defense structure won't either increase or decrease.

Every soldier has a rank — some natural number from 1 to k. 1 stands for a private and k stands for a general. The higher the rank of the soldier is, the better he fights. Therefore, the player profits from having the soldiers of the highest possible rank.

To increase the ranks of soldiers they need to train. But the soldiers won't train for free, and each training session requires one golden coin. On each training session all the n soldiers are present.

At the end of each training session the soldiers' ranks increase as follows. First all the soldiers are divided into groups with the same rank, so that the least possible number of groups is formed. Then, within each of the groups where the soldiers below the rank k are present, exactly one soldier increases his rank by one.

You know the ranks of all n soldiers at the moment. Determine the number of golden coins that are needed to increase the ranks of all the soldiers to the rank k.

Input

The first line contains two integers n and k (1 ≤ n, k ≤ 100). They represent the number of soldiers and the number of different ranks correspondingly. The second line contains n numbers in the non-decreasing order. The i-th of them, ai, represents the rank of the i-th soldier in the defense building (1 ≤ i ≤ n, 1 ≤ ai ≤ k).

Output

Print a single integer — the number of golden coins needed to raise all the soldiers to the maximal rank.

Examples

input

Copy

4 4
1 2 2 3

output

Copy

4

input

Copy

4 3
1 1 1 1

output

Copy

5

Note

In the first example the ranks will be raised in the following manner:

1 2 2 3  →  2 2 3 4  →  2 3 4 4  →  3 4 4 4  →  4 4 4 4

Thus totals to 4 training sessions that require 4 golden coins.

题意:给你一串数字,相同的数字为一组,每次可以给一组中的一个数字加一,问这一串数字全变成K需要多少步?

题解:模拟

#include<stdio.h>
#include<queue>
#include<string.h>
#include<algorithm>
#include<vector>
#include<iostream>
using namespace std;
int a[105],b[105];
int main(){
    int n,k;
    scanf("%d %d",&n,&k);
    int sum=0;
    for(int i=0;i<n;i++){
        scanf("%d",&a[i]);
        sum+=a[i];
    }
    sort(a,a+n);
    int ans=0;
    while(sum<n*k){
        ans++;
        for(int i=0;i<n;i++)
            b[i]=a[i];
        if(b[0]<k){
            sum++;
            b[0]++;
        }
        for(int i=1;i<n;i++){
            int j=i-1;
            if(a[i]==a[j]||a[i]==k)
                continue;
            b[i]++;
            sum++;
        }
        sort(b,b+n);
        for(int i=0;i<n;i++)
            a[i]=b[i];
    }
    printf("%d\n",ans);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/black_horse2018/article/details/81624838