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 = ;
int a[N],b[N];
int main(){
int n,k;
while(scanf("%d%d",&n,&k)!=EOF){
int sum = ;
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
sum+=a[i];
}
int cnt = ;
sort(a+,a++n);
while(sum<n*k){
cnt++;
for(int i=;i<=n;i++) b[i] = a[i];
if(a[]<k){
b[]++;
sum++;
}
for(int i=;i<=n;i++){
if(a[i]==a[i-]||a[i]==k) continue;
b[i]++;
sum++;
}
for(int i=;i<=n;i++){
a[i] = b[i];
}
sort(a+,a++n);
}
printf("%d\n",cnt);
}
}

csu 1749: Soldiers ' Training(贪心)的更多相关文章

  1. CSU 1859 Gone Fishing(贪心)

    Gone Fishing [题目链接]Gone Fishing [题目类型]贪心 &题解: 这题要先想到枚举走过的湖,之后才可以贪心,我就没想到这,就不知道怎么贪心 = = 之后在枚举每个湖的 ...

  2. 【贪心】CSU 1809 Parenthesis (2016湖南省第十二届大学生计算机程序设计竞赛)

    题目链接: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1809 题目大意: 给一个长度为N(N<=105)的合法括号序列.Q(Q<= ...

  3. 2017 Multi-University Training Contest - Team 1 1002&&HDU 6034 Balala Power!【字符串,贪心+排序】

    Balala Power! Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)T ...

  4. 2018 Multi-University Training Contest 1 Distinct Values 【贪心 + set】

    任意门:http://acm.hdu.edu.cn/showproblem.php?pid=6301 Distinct Values Time Limit: 4000/2000 MS (Java/Ot ...

  5. ACM学习历程—CSU 1216 异或最大值(xor && 贪心 && 字典树)

    题目链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1216 题目大意是给了n个数,然后取出两个数,使得xor值最大. 首先暴力枚举是C(n,  ...

  6. csu - 1538: Shopping (贪心)

    http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1538 很奇妙的一个题,开始没有思路.问了别人才知道. 题目的意思可以理解成上图中,从0点开始向右走 ...

  7. csu 1757(贪心或者树状数组)

    1757: 火车入站 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 209  Solved: 51[Submit][Status][Web Board] ...

  8. 2014 Multi-University Training Contest 1/HDU4864_Task(贪心)

    解题报告 题意,有n个机器.m个任务. 每一个机器至多能完毕一个任务.对于每一个机器,有一个最大执行时间Ti和等级Li,对于每一个任务,也有一个执行时间Tj和等级Lj.仅仅有当Ti>=Tj且Li ...

  9. 2018 Multi-University Training Contest 1-1002 -Balanced Sequence(括号匹配+贪心)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6299 题目: 题意:t组数据,每组数据给你一个n表示给你n个括号串,这n个括号串之间进行组合,求能够匹 ...

随机推荐

  1. 【ZJOI2005】沼泽鳄鱼 题解报告

    题目描述 潘塔纳尔沼泽地号称世界上最大的一块湿地,它地位于巴西中部马托格罗索州的南部地区.每当雨季来临,这里碧波荡漾.生机盎然,引来不少游客. 为了让游玩更有情趣,人们在池塘的中央建设了几座石墩和石桥 ...

  2. 【原创】【目录】实现rich editor(富文本编辑器)教程,深入理解selectiona/range操作与浏览器差异

    日常工作中,接触富文本编辑的次数还是很多,特发此教程,希望可以改变富文本编辑的技术门槛较高的现状,解决这部分的坑. 前提准备,兼容获取range,统一回车行为,前期准备工作 了解document.ex ...

  3. 解题:ZJOI 2006 书架

    题面 学习了如何在维护序列的平衡树上查找某个数:按初始的顺序定个权值,然后每次找那个权值的DFS序即可.具体实现就是不停往上跳,然后是父亲的右儿子就加上父亲的左儿子,剩下的就是继续熟悉无旋树堆 #in ...

  4. 【bzoj3230】相似子串

    Portal -->bzoj3230 Description 给你一个长度为\(n\)的字符串,把它的所有本质不同的子串按字典序大小排序,有\(m\)个询问,对于每一个询问\(x,y\)你需要回 ...

  5. Web前端之HTML详解20180327

    一.html概述 html就是超文本标记语言的简写,是最基础的网页语言.html通过标签来定义语言,代码都是由标签所组成. 1.html代码从<html>开始</html>结束 ...

  6. 孙鑫视频VC++深入详解学习笔记

    孙鑫视频VC++深入详解学习笔记 VC++深入详解学习笔记 Lesson1: Windows程序运行原理及程序编写流程 Lesson2: 掌握C++基本语法 Lesson3: MFC框架程序剖析 Le ...

  7. move_base的 局部路径规划代码研究

    base_local_planner teb_local_planner parameter code g2o base_local_planner ROS wiki Given a plan to ...

  8. POJ 3260 完全背包+多重背包+思维

    传送门:https://vjudge.net/problem/20465/origin 题意:你有n种钞票,面值为c[i],数量为v[i],便利店老板有无数张面值为c[i]的钞票,问你买一个价值为T的 ...

  9. codevs 1491 取物品

    1491 取物品 http://codevs.cn/problem/1491/  时间限制: 1 s  空间限制: 128000 KB     题目描述 Description 现在有n个物品(有可能 ...

  10. 「LibreOJ β Round #4」框架

    https://loj.ac/problem/527 题目描述 有一个n×m的矩形框架,但其中有些边被删除了.qmqmqm想知道剩余部分中还有多少完整的正方形.只有当一个正方形的每一条边均被保留下来, ...