A7980 | Settlers' Training
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
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$ .
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$ .
输入格式
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, $a_{i}$ , represents the rank of the $i$ -th soldier in the defense building ( $1<=i<=n$ , $1<=a_{i}<=k$ ).
输出格式
Print a single integer — the number of golden coins needed to raise all the soldiers to the maximal rank.
输入输出样例
输入 #1
4 4 1 2 2 3
输出 #1
4
输入 #2
4 3 1 1 1 1
输出 #2
5
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.
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.
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted