E - Table Tennis
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, …, an (1 ≤ ai ≤ n) — powers of the player. It’s guaranteed that this line contains a valid permutation, i.e. all ai are distinct.
Output
Output a single integer — power of the winner.
Examples
Input
2 2
1 2
Output
2
Input
4 2
3 1 2 4
Output
3
Input
6 2
6 5 3 1 2 4
Output
6
Input
2 10000000000
2 1
Output
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.
[题目链接]https://vjudge.net/contest/291644#problem/E
思路:
这道题我刚开始真的是一直理解错题意,以为如果比赛到最后,还没有达到k场的话,就罗列全部,看哪个赢的最多,就是赢家。可是我没有理解到,即使后面的人表面看起来可能没有前面的人赢的多,可是他本身的理解就是比那些淘汰的人强,即使没有比赛那么多场。
也就是分为两种情况:
- 一种是达到k场的时候,后面还有人没有比赛,那么就输出赢了k场的那个人的power,然后呢,继续输入剩余的人的power
- 一种是比赛到最后,最后那个人没有达到赢了k场,那么直接输出最后那个的power就可以,因为他能走到最后,说明他是最强者。
AC代码:
#include<stdio.h>
int main(){
int n;
long long k;
while(~scanf("%d%lld",&n,&k)){
int sum=0;
int max1=0;
int tag=0;
for(int i = 0 ; i < n ; i++){
int a;
scanf("%d",&a);
if(a<max1){
sum++;
if(sum==k&&i!=n){
for(i=i+1;i<n;i++){
scanf("%d",&a);
}
tag=1;
printf("%d\n",max1);
}
}
else if(a>max1){
max1=a;
sum=(i==0)?0:1;
}
}
if(tag==0)
printf("%d\n",max1);
}
}