• B


    B - Settlers’ Training

    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.

    题目大意:升级士兵,一枚金币可让每个不同等级的士兵全都升级一次,若在一个等级内有(人数>=2)的士兵,则在这个等级中随便挑取一个进行升级,请问一共要多少枚金币(升级多少次)使得全体士兵都达到等级k;

    思路:

    模拟

    记得在模拟的结尾加个边界
    a[n] = k;
    以便于最后一个元素的比较;

    #include<string>
    #include<stdio.h>
    #include<queue>
    #include<algorithm>
    #include<vector>
    #include<iostream>
    using namespace std;
    //reverse
    
    
    int main()
    {
    	int n, k, a[105];
    	cin >> n >> k;
    	for (int i = 0; i < n; i++)
    	{
    		cin >> a[i];
    	}
    	a[n] = k;//关键,给下面的第二个循环进行边界的比较。
    	int i;
    	sort(a, a + n);
    	for (i = 0; a[0]!=k; i++)//模拟,此处i则为需要升级的次数。
    	{
    		for (int j = 0; j < n; j++)
    		{
    			if (a[j] != a[j + 1])
    				a[j]++;
    		}
    	}
    	cout << i << endl;
    	return 0;
    }
    
  • 相关阅读:
    PHP Mysql-插入多条数据
    PHP Mysql-插入数据
    PHP Mysql-创建数据表
    PHP Mysql-创建数据库
    PHP Mysql-连接
    PHP Mysql-简介
    PHP-7
    postgresql 创建函数
    在psql客户端中修改函数
    修改PostgreSQL数据库的默认用户postgres的密码
  • 原文地址:https://www.cnblogs.com/gidear/p/11773636.html
Copyright © 2020-2023  润新知