B. Table Tennis
Problem Statement
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
Example 1
Input
2 2
1 2
Output
2
Example 2
Input
4 2
3 1 2 4
Output
3
Example 3
Input
6 2
6 5 3 1 2 4
Output
6
Example 4
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.
题意
给一个长度为n的序列,按顺序表示每个人的能力值,一开始是由第一个人和第二个人对打,输了的人走到队列的最后面,之后由获胜的人与剩下的队列的开头的人打,能力值高的赢。问第一个能连续赢k局的人的能力值是多少。
思路
首先我们很容易就可以想到,如果k>=n-1,那输出的值肯定是n个数中最大的那个,就是n,因为k>=n-1就代表如果要获胜,就要与所有人对打,并且全都赢。所以只有能力值为n的人才能做到。而对于k
Code
#pragma GCC optimize(3)
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
bool Finish_read;
template<class T>
inline void read(T &x) {
Finish_read=0;x=0;int f=1;char ch=getchar();
while(!isdigit(ch)){if(ch=='-')f=-1;if(ch==EOF)return;ch=getchar();}
while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
x*=f;Finish_read=1;
}
template<class T>
inline void print(T x) {
if(x/10!=0)
print(x/10);
putchar(x%10+'0');
}
template<class T>
inline void writeln(T x) {
if(x<0)
putchar('-');
x=abs(x);
print(x);
putchar('\n');
}
template<class T>
inline void write(T x) {
if(x<0)
putchar('-');
x=abs(x);
print(x);
}
/*================Header Template==============*/
ll a[1005],n,k;
int main() {
read(n);
read(k);
if(k>=n-1) {
writeln(n);
return 0;
}
for(int i=1;i<=n;i++) {
read(a[i]);
a[i+n]=a[i];
}
int cnt=0,pos=1,cntpos=2;
while(1) {
int x=a[pos];
if(cnt==k) {
writeln(x);
return 0;
}
if(x<a[cntpos]) {
cnt=1;
pos=cntpos;
cntpos++;
}
else {
cnt++;
cntpos++;
}
}
return 0;
}