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:

  1. Give m candies to the first child of the line.
  2. If this child still haven‘t got enough candies, then the child goes to the end of the line, else the child go home.
  3. 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

Input

5 2
1 3 1 4 2

Output

4

Input

6 4
1 1 2 2 3 3

Output

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);
        }

    }
}
时间: 2024-08-15 11:18:18

CodeForces 450A 队列的相关文章

codeforces 704A (队列模拟) Thor

题目:这里 题意:n个app,q个操作,当操作数type为1的时候表示y这个app推送了你一条消息,当操作数type为2的时候表示将y这个app已推送的所有消息都读完,当操作数为3的时候 表示将已经推送的前(按推送的时间顺序)y条消息再读一遍(不管这前y条消息中有没有读过的,都再读一遍),问每次操作的时候的未读的消息数是多少? 用队列来模拟就好,记得每次要输出的是所有app的没有读过的消息的总数就行,不要想的太细,把读过的标记一下就行,总是觉得这个会超时,但是一想这才是2的第三题,1 的第一题,

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/problem/319/B Description There are n psychos standing in a line. Each psycho is assigned a unique integer from 1 to n. At each step every psycho who h

CodeForces - 91B单调队列

有一个数列,对于每一个数,求比它小的在他右边距离他最远的那个数和他的距离 用单调队列做,维护单调队列时可采用如下方法,对于每一个数,如果队列中没有数,则加入队列,如果队列头的数比当前数大,则舍弃该数 对于此题,可用两个队列来维护,一个保存值,一个保持位置,每次查询对值进行二分,然后到对应保持位置的队列中求出结果 #include<map> #include<set> #include<cmath> #include<queue> #include<st

CodeForces 602D 【单调队列】【简单数学】

题意: 给你n个数,m次询问,每次询问给l和r代表l和r中间所有子区间中特征值的和. 特征值的定义是在这个区间中找i和j使得|tmp[i]-tmp[j]|/|j-i|最大. 思路: 首先是特征值的定义,这个东西其实是斜率~不知道从哪里看到的证明,这个只有相邻的点才可能最大.所以给定区间找到最大值其实是在相邻的中找.这是这题第一个关键点. 如果一个一个加寻找每一个区间那么复杂度应该是n^2,这里的n大小是1e5,还是不行.然后这个时候思路就是通过单调队列来解决啦~[个人认为更像是个DP]找到某个点

Codeforces Round #149 (Div. 2) Dispute (队列+容器模拟)

[题目链接]click here~~ [题目大意]: 给定一个无向图,图中的每个结点最初的权值都为0.但每个结点有一次机会,可以增加它本身的权值,但是有个副作用即:在增加自己权值的情况下,也会增加与自己直接相连的结点的权值. Ignat和Valera打赌说:valera不可能找到一种方法,使得对于每个结点i,它的权值不等于lgnat给定的ai.然后,题目要求写个程序,让valera可以胜,并输出哪个结点要用到,它给定的那次增加的机会. 输入:第1行n,m(1<=n,m<=10^5)分别表示有n

Codeforces 704A Thor 队列模拟

题目大意:托尔有一部手机可执行三种操作 1.x APP产生一个新消息 2.读取x App已产生的所有消息 3.读取前t个产生的消息 问每次操作后未读取的消息的数量 题目思路: 队列模拟,坑点在于竟然卡内存……详细看代码. #include<iostream> #include<algorithm> #include<cstring> #include<vector> #include<stdio.h> #include<stdlib.h&g

CodeForces 546C(队列)

Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Description Two bored soldiers are playing card war. Their card deck consists of exactly n cards, numbered from 1 to n, all values are different. They divide cards between

codeforces 251A Points on Line(二分or单调队列)

Description Little Petya likes points a lot. Recently his mom has presented him n points lying on the line OX. Now Petya is wondering in how many ways he can choose three distinct points so that the distance between the two farthest of them doesn't e

CodeForces 15D Map 单调队列优化

两次单调队列求出每个子矩阵的最小值,区间减法求出每个子矩阵的和,然后丢到优先队列里跑出来就好了. 写锉了,加了读入挂才过. #include <algorithm> #include <iostream> #include <cstring> #include <cstdlib> #include <cstdio> #include <queue> #include <cmath> #include <stack&g