本文主要是介绍A - Settlers' Training CodeForces - 63B(模拟),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
A - Settlers’ Training CodeForces - 63B
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
4 4
1 2 2 3
Output
4
Input
4 3
1 1 1 1
Output
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.
题意: 很有意思的模拟题,相同数字为一组,每次操作每一组一个数字+1,问多少次操作使得所有数字都变成k。
思路: 基本思路是模拟过程。我的模拟过程是for的过程中 只加 一个数字一次。无限循环直到达到终状态。这样的复杂度还是很高的。或许换种思路,考虑一下需要维护什么,题目要求的是不同数字的组合。我们或许可以对于每一个不同数字的组合额外开一个空间分别维护(比如队列,或者数组映射),然后转移来,转移去~~
#include<iostream>
#include<stdio.h>
#include<map>
#include<cstring>
#include<algorithm>
#include <vector>
#include <queue>
#include <stack>
using namespace std;int a[107];
stack<int>Q[100];
int cmp(int a,int b)
{return a > b;
}
int main()
{int n,k;scanf("%d%d",&n,&k);for(int i = 1;i <= n;i++){scanf("%d",&a[i]);}sort(a + 1,a + 1 + n,cmp);int ans = 0;int tmp = -1;int flag = 1;for(int i = 1;i <= n;i++){if(a[i] != k){flag = 0;break;}}if(flag){printf("0\n");return 0;}while(1){tmp = -1;ans++;for(int i = 1;i <= n;i++){if(a[i] == k)continue;else if(tmp == a[i])continue;else{tmp = a[i];a[i]++;}}int flag = 1;for(int i = 1;i <= n;i++){if(a[i] != k){flag = 0;break;}}if(flag)break;}printf("%d",ans);return 0;
}
这篇关于A - Settlers' Training CodeForces - 63B(模拟)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!