CodeForces 450A 队列
Description
There are n children in Jzzhu's school. Jzzhu is going to give some candies to them. Let's number all the children from
1 to n. The
i-th child wants to get at least ai candies.
Jzzhu asks children to line up. Initially, the i-th child stands at the
i-th place of the line. Then Jzzhu start distribution of the candies. He follows the algorithm:
- Give m candies to the first child of the line.
- If this child still haven't got enough candies, then the child goes to the end of the line, else the child go home.
- Repeat the first two steps while the line is not empty.
Consider all the children in the order they go home. Jzzhu wants to know, which child will be the last in this order?
Input
The first line contains two integers n, m(1 ≤ n ≤ 100; 1 ≤ m ≤ 100). The second line contains
n integers a1, a2, ..., an(1 ≤ ai ≤ 100).
Output
Output a single integer, representing the number of the last child.
Sample Input
5 2
1 3 1 4 2
4
6 4
1 1 2 2 3 3
6
Hint
Let's consider the first sample.
Firstly child 1 gets 2 candies and go home. Then child 2 gets 2 candies and go to the end of the line. Currently the line looks like [3, 4, 5, 2] (indices of the children in order of the line). Then child 3 gets 2 candies and go home, and then child 4 gets
2 candies and goes to the end of the line. Currently the line looks like [5, 2, 4]. Then child 5 gets 2 candies and goes home. Then child 2 gets two candies and goes home, and finally child 4 gets 2 candies and goes home.
Child 4 is the last one who goes home.
//队列
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<queue> using namespace std; struct node
{
int a,b;
} p,t;
int main()
{
queue<node> q;
int n,m;
while(~scanf("%d%d",&n,&m))
{
int top=-1;
for(int i=0; i<n; i++)
{
scanf("%d",&p.a);
p.b=i+1;
if(p.a-m>0)
{
p.a-=m;
q.push(p);
top=p.b;
}
}
// t=q.top();
// printf("%d\n",t.a);
if(top==-1)
printf("%d\n",n);
else
{
while(!q.empty())
{
t=q.front();
q.pop();
if(t.a-m>0)
{
t.a-=m;
q.push(t);
top=t.b;
}
}
printf("%d\n",top);
} }
}
CodeForces 450A 队列的更多相关文章
- codeforces 704A (队列模拟) Thor
题目:这里 题意:n个app,q个操作,当操作数type为1的时候表示y这个app推送了你一条消息,当操作数type为2的时候表示将y这个app已推送的所有消息都读完,当操作数为3的时候 表示将已经推 ...
- CodeForces 164 B. Ancient Berland Hieroglyphs 单调队列
B. Ancient Berland Hieroglyphs 题目连接: http://codeforces.com/problemset/problem/164/B Descriptionww.co ...
- Codeforces Round #189 (Div. 1) B. Psychos in a Line 单调队列
B. Psychos in a Line Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/p ...
- CodeForces 546C(队列)
CodeForces 546C Soldier and Cards Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I ...
- Codeforces Beta Round #6 (Div. 2 Only) 单调队列
题目链接: http://codeforces.com/contest/6/problem/E E. Exposition time limit per test 1.5 secondsmemory ...
- Codeforces 445A Boredom(DP+单调队列优化)
题目链接:http://codeforces.com/problemset/problem/455/A 题目大意:有n个数,每次可以选择删除一个值为x的数,然后值为x-1,x+1的数也都会被删除,你可 ...
- Codeforces Round #304 (Div. 2) C. Soldier and Cards —— 模拟题,队列
题目链接:http://codeforces.com/problemset/problem/546/C 题解: 用两个队列模拟过程就可以了. 特殊的地方是:1.如果等大,那么两张牌都丢弃 : 2.如果 ...
- Codeforces 1029B. Creating the Contest 动态规划O(nlogn)解法 及 单调队列O(n)解法
题目链接:http://codeforces.com/problemset/problem/1029/B 题目大意:从数组a中选出一些数组成数组b,要求 b[i+1]<=b[i]*2 . 一开始 ...
- Codeforces 940 E.Cashback (单调队列,dp)
Codeforces 940 E.Cashback 题意:一组数,要分为若干个区间,每个区间长度为ki(1<=ki<=n),并且对于每个区间删去前ki/c(向下取整)个小的数(即对区间升序 ...
随机推荐
- ubuntu14.04 server 安装docker
安装docker服务 $ curl -sSL https://get.docker.com/ | sh $ ocker run hello-world 测试docker是否安装成功 ubuntu ...
- es6 中的 模板字符串
Template literals are string literals allowing embedded expressions. You can use multi-line strings ...
- HTML 中图片的隐藏与显示
<html xmlns="http://www.w3.org/1999/xhtml"><head> <meta http-equiv="Co ...
- ubuntu各版本代号(更新至15.04)及各版本下载地址等
版本号 代号 发布时间 15.04 Vivid Vervet 2015/04/22 14.10 Utopic Unicorn 2014/10/23 14.04 LTS Trusty Tahr 2014 ...
- redux-actions
其作用都是用来简化action.reducer. 1.安装 npm install --save redux-actions // 或 yarn add redux-actions 2.使用 crea ...
- odoo8.0条形码改为js方式处理
群里网友@上海-gavin 提供的odoo条形码处理,将原来的图片生成方式改为js处理方式. <div class="row text-center"> <div ...
- SVN学习(三)——在Eclipse 中安装和使用SVN客户端插件
0 基本概念了解 0.1 SVN的工作原理:采取客户端/服务器模式——在服务器的版本库中保存项目文件的各个版本,所有参与协同开发的程序员在自己本地电脑上保存一个工作副本.SVN支持程序员将本地副本更新 ...
- Android平台Native开发与JNI机制详解
源文链接: http://mysuperbaby.iteye.com/blog/915425 一个Native Method就是一个Java调用非Java代码的接口.一个Native Method是这 ...
- 初次玩耍lucene.net,一个小小的记录
lucene.net虽说是强大,但是我还是一年前第一次玩耍,然后就没有然后了,最近准备养成记录博客的习惯了,所以又玩了玩,回来记录一下 首先新建一个类,便于调用 public class Lucene ...
- W25Q128页数和扇区数
int8_t STORAGE_GetCapacity (uint8_t lun, uint32_t *block_num, uint32_t *block_size){ *block_size = 4 ...