可以证明,当k>=n的时候,必定是队伍里面实力最强的人胜利,因此对于这种情况,我们直接取队伍的最大能力值输出即可。
对于k<n的情况,我们模拟就可以了。
模拟用STL里面的双端队列(deque)是可以的,并且很方便,下面的代码就是用的deque。
当然也可以只用一个queue单向队列,每次贪心地保留最大值,将保留的与新队首进行比较,然后pop掉其中一个也可以。
其他细节可以看代码了,我打的时候一堆堆人被hack,心惊胆颤的,最后总测还是过了,excited。
附上AC代码
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #include<cmath> 5 #include<queue> 6 #include<iostream> 7 using namespace std; 8 template<class T> inline void read(T &_a){ 9 bool f=0;int _ch=getchar();_a=0; 10 while(_ch<'0' || _ch>'9'){if(_ch=='-')f=1;_ch=getchar();} 11 while(_ch>='0' && _ch<='9'){_a=(_a<<1)+(_a<<3)+_ch-'0';_ch=getchar();} 12 if(f)_a=-_a; 13 } 14 15 const int maxn=501; 16 deque<int>a; 17 long long n,k,cnt; 18 19 int main() 20 { 21 read(n); read(k); 22 for (register int i=1,v;i<=n;++i) read(v),a.push_back(v); 23 if(k<n) 24 { 25 int a1,a2; 26 while(cnt<k) 27 { 28 a1=a.front(); a.pop_front(); 29 a2=a.front(); a.pop_front(); 30 if(a1>a2) 31 { 32 ++cnt; 33 a.push_front(a1); 34 a.push_back(a2); 35 } else { 36 cnt=1; 37 a.push_front(a2); 38 a.push_back(a1); 39 } 40 } 41 printf("%d",a.front()); 42 } else { 43 int maxx=0; 44 while(!a.empty()) 45 { 46 maxx=max(maxx,a.front()); 47 a.pop_front(); 48 } 49 printf("%d",maxx); 50 } 51 return 0; 52 }
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.
InputThe first line contains two integers: n and k (2 ≤ n ≤ 500, 2 ≤ k ≤ 1012) — the number of people and the number of wins after which a player leaves, respectively.
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.
OutputOutput a single integer — power of the winner.
Examplesinput2 2
1 2output2input4 2
3 1 2 4output3input6 2
6 5 3 1 2 4output6input2 10000000000
2 1output2NoteGames 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.