CF879B

CF879B

B. Table Tennis

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner.

For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner.

Input

The first line contains two integers: n and k (2 ≤ n ≤ 500, 2 ≤ k ≤ 1012) — the number of people and the number of wins.

The second line contains n integers a1, a2, …, a**n (1 ≤ a**i ≤ n) — powers of the player. It’s guaranteed that this line contains a valid permutation, i.e. all a**i are distinct.

Output

Output a single integer — power of the winner.

Examples

input

Copy

2 2
1 2

output

Copy

2 

input

Copy

4 2
3 1 2 4

output

Copy

3 

input

Copy

6 2
6 5 3 1 2 4

output

Copy

6 

input

Copy

2 10000000000
2 1

output

Copy

2

Note

Games in the second sample:

3 plays with 1. 3 wins. 1 goes to the end of the line.

3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.


唉,本来一道简单的队列模拟,都死在了获胜数的重新归位上,嘤嘤嘤

思路很简单,纯模拟,过程写在注释上了(让我伤心一会,第一次ratting本来成绩能不错的嘤嘤嘤)

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <queue>
#define ll long long

using namespace std;

ll n, k;
ll a[500 + 10];
queue<ll> q;

// struct queue{ //我甚至找错误找到了是不是stl队列出问题了,甚至自己手写了一个
//     ll q[1000 + 100];
//     int top = 1;
//     int end = 1;

//     void pop(){
//         top++;
//     }

//     void push(int n){
//         q[++end] = n;
//     }

//     int front(){
//         return q[top];
//     }
// }q;


//模拟
//还有好办法吗?好像桶来看看能行吗
//模拟队列,

int main(){
    cin >> n >> k;
    ll maxx = -1;
    for (ll i = 1; i <= n; i++){
        cin >> a[i];
        q.push(a[i]);
        maxx = max(maxx, a[i]); //找最大的
    }

    ll ans = 0;
    ll now_power = q.front(); //当前第一个来打球
    q.pop();//弹出第一个
    while(ans != k){
        ll next_power = q.front(); //与当前要打的这个
        q.pop(); // 把对手弹出队列
        if(now_power == maxx){ //最强的这个肯定最后能获得k场胜利,那么我直接跳出来就行了
            break;
        }
        if(now_power >= next_power){ // 如果当前没有被淘汰这个能力比对手强
            ans++; //说明他获胜了一场
            q.push(next_power); //把这个输的放回队列里
        }
        else{ //如果不是,说明刚出来这个比当前这个要强
            q.push(now_power); //就把当前这个放回队列里
            now_power = next_power; //更新为当前获胜这个
            ans = 1; //获胜数要更为1!!!!!吃了好几次亏,提交次数原因都在这了
        }
    }
    cout << now_power;
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值