zoukankan      html  css  js  c++  java
  • csu 1749: Soldiers ' Training(贪心)

    1749: Soldiers ' Training

    Time Limit: 1 Sec  Memory Limit: 512 MB
    Submit: 37  Solved: 18
    [Submit][Status][Web Board]

    Description

    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

    Each case contains two lines.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

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

    Sample Input

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

    Sample Output

    4
    5

    HINT


    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.

    Source

    题意:每次可以提升在相同等级中的人中任意一个,完成这一步需要一个金币,问将所有人升到满级所需时间?

    题解:贪心模拟即可。

    #include <stdio.h>
    #include <iostream>
    #include <algorithm>
    #include <math.h>
    #include <string.h>
    using namespace std;
    const int N = 105;
    int a[N],b[N];
    int main(){
        int n,k;
        while(scanf("%d%d",&n,&k)!=EOF){
            int sum = 0;
            for(int i=1;i<=n;i++){
                scanf("%d",&a[i]);
                sum+=a[i];
            }
            int cnt = 0;
            sort(a+1,a+1+n);
            while(sum<n*k){
                cnt++;
                for(int i=1;i<=n;i++) b[i] = a[i];
                if(a[1]<k){
                    b[1]++;
                    sum++;
                }
                for(int i=2;i<=n;i++){
                    if(a[i]==a[i-1]||a[i]==k) continue;
                    b[i]++;
                    sum++;
                }
                for(int i=1;i<=n;i++){
                    a[i] = b[i];
                }
                sort(a+1,a+1+n);
            }
            printf("%d
    ",cnt);
        }
    }
  • 相关阅读:
    磁盘分区,fdisk,gdisk,开机自动挂载,swap分区,修复文件系统,备份文件
    进程脱离窗口运行,僵尸、孤儿进程
    top命令、kill命令
    进程状态
    rpm包、挂载、yum命令
    DRF源码分析
    forms组件源码
    Django CBV源码分析
    魔法方法
    鸭子类型
  • 原文地址:https://www.cnblogs.com/liyinggang/p/5806152.html
Copyright © 2011-2022 走看看