360 School Recruitment-Last Winner (C ++)

⭐ Article link: www.mengyingjie.com/archives/39/ ⭐

Winner

Time limit: 1000MS in C / C ++ language; 3000MS in other languages
Memory limit: 65536KB in C / C ++ language; 589824KB in other languages

Title description:

The strongest is not necessarily the final winner.

There are n players participating in a certain event, but unlike other games, this game takes the form of a ring game. N players line up in a row, each time the first and second players of the team play, lose One party will be at the end of the line.

When a player wins m straight, he will become the final winner and the game is over. How many games will be played as of the end of the game.

The result of the two players' competition is determined by their fighting power. The fighting power of the n players is a 1 ~ n arrangement, which means that their fighting powers are different from each other, and there will be no draw.

Input

The first line of input contains two positive integers n, m, representing the number of contestants and the requirements for winning streak. (1 <= n <= 100000, 1 <= m <= 10 ^ 9)

The second line of the input contains n positive integers, separated by spaces. The i-th number represents the fighting strength of the i-th player of the team. The whole is a 1 ~ n arrangement.

Output

The output contains only a positive integer, indicating how many games were played as of the end of the game.

Sample input

4 2
1 3 2 4

Sample output

2

prompt

Sample explanation
Obviously, the player with a combat strength of 3 should win in the first round, and the player with a combat strength of 3 should also win in the second round. The game is ended with 2 straight wins, so the answer is 2. If m is changed to 3 at this time, the result is 5.

Code

The problem-solving ideas are written in the comments of the code

#include <iostream>
#include <queue>

using namespace std;

int main(){
    //n,m,分别代表参赛选手数量和取得连胜的要求
    //cnt记录作为基准选手的胜场数
    int n, m, h, y, cnt;
    //cnt2表示共进行了多少场比赛
    int cnt2 = 0;
    //定义队列来存储每位选手的战斗力
    queue <int> q;
    cin >> n >> m;
    for(int i = 0; i < n; i++){
        //将战斗力存储在队列中
        cin >> h;
        q.push(h);
    }
    //取出第一个队列当作基准
    h = q.front();
    q.pop();
    //默认胜场为零
    cnt = 0;
    while(cnt < m){
        //一次循环代表进行一场比赛
        cnt2 ++;
        y = q.front();
        //将基准h与现在队首的y相比较
        if(h > y){
            //如果h胜了,他继续当基准,然后他的胜场+1
            cnt++;
            //将队首的y取出,放到队列
            q.pop();
            q.push(y);
            cout << h << " " << y << " " << cnt  << endl;
        }
        else{
            //如果y胜了,把原来的h放到队尾,然后将y作为基准,他的胜场置为1,
            cout << h << " " << y << " " << cnt  << endl;
            q.pop();
            q.push(h);
            h = y;
            cnt = 1;
        }
    }
    cout << cnt2 << endl;
    return 0;
}

Encountered such problems, but still unresolved after reading the article,
comment or add QQ: 781378815

Guess you like

Origin www.cnblogs.com/mengyingjie/p/12723747.html