Search is not available for this dataset
name
stringlengths 2
112
| description
stringlengths 29
13k
| source
int64 1
7
| difficulty
int64 0
25
| solution
stringlengths 7
983k
| language
stringclasses 4
values |
---|---|---|---|---|---|
879_B. 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. | 2 | 8 | import sys
n, k = map(int, input().split())
a = [int(i) for i in input().split()]
if (k >= n - 1):
print(max(a));
else:
pref_max = a[0];
for i in range(n):
s = a[i];
to = i + k + 1;
if (s < pref_max):
continue;
else:
pref_max = s;
if (i):
to -= 1
for j in range(i + 1, to):
if (a[j % n] > s):
break;
else:
print(s)
sys.exit(0) | PYTHON3 |
879_B. 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. | 2 | 8 | def tabletennis(n, cP):
st = 0
for i in range(1, p):
if st == nOW:
return cP
elif cP > n[i]:
st += 1
else:
cP = n[i]
st = 1
return cP
p, nOW = map(int, input().split())
n = input().split()
n = list(map(int, n))
cP = n[0]
print(tabletennis(n, cP))
| PYTHON3 |
879_B. 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. | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int main() {
long long n, k, arr[500], ck = 0;
cin >> n >> k;
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
long long j = arr[0];
for (int i = 1; i < n; i++) {
if (j < arr[i]) {
ck = 1;
swap(j, arr[i]);
} else {
ck++;
}
if (ck == k) break;
}
cout << j << endl;
}
| CPP |
879_B. 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. | 2 | 8 | import java.util.*;
public class A
{
public static void main(String ar[])
{
Scanner s=new Scanner(System.in);
int n=s.nextInt();
long k=s.nextLong();
int a[]=new int[n];
for(int i=0;i<n;i++)
a[i]=s.nextInt();
int b[]=new int[n]; int z=0; int l=-1;
int r=0;
for(int i=1;i<n;i++)
{
if(a[r]>a[i])
{ b[r]++; z=Math.max(z,b[r]); }
else
{ b[i]++; r=i; z=Math.max(z,b[i]); }
}
for(int i=0;i<n;i++)
if(b[i]>=k)
{ l=i; break; }
if(l!=-1)
{
System.out.println(a[l]);
}
else
System.out.println(n);
}
} | JAVA |
879_B. 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. | 2 | 8 | import java.util.Collections;
import java.util.LinkedList;
import java.util.Scanner;
/**
*
* @author arabtech
*/
public class TableTennis {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
long k=sc.nextLong();
int wins=0;
int temp;
LinkedList<Integer> power =new LinkedList<Integer>();
for(int i=0;i<n;i++){
power.add(sc.nextInt());
}
if(k>=n){
Collections.sort(power);
System.out.println(power.get(n-1));
}
else{
while(wins<k){
if(power.get(0)>power.get(1)){
wins++;
temp=power.get(1);
power.remove(1);
power.add(temp);
}
else{
wins=1;
temp=power.get(0);
power.remove(0);
power.add(temp);
}
}
System.out.println(power.get(0));
}
}
} | JAVA |
879_B. 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. | 2 | 8 | // CodeForces Round #879 B train
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
public class TableTennis {
int n;
long k;
int []a; // player powers
int winPower;
private void readData(BufferedReader bin) throws IOException {
String s = bin.readLine();
String []ss = s.split(" ");
n = Integer.parseInt(ss[0]);
k = Long.parseLong(ss[1]);
a = new int[n];
s = bin.readLine();
ss = s.split(" ");
for (int i=0; i<n; i++) {
a[i] = Integer.parseInt(ss[i]);
}
}
void printRes() {
System.out.println(winPower);
}
int curr;
int cur() {
return curr;
}
int next() {
return (curr+1) %n;
}
int last() {
return (n+curr-1) % n;
}
void swap(int ia, int ib) {
int t=a[ia]; a[ia] = a[ib]; a[ib] = t;
}
private void calculate() {
int winCnt = 0;
for (curr=0; curr<n; curr++) {
if (a[cur()] > a[next()]) {
winCnt++;
if (winCnt>=k) {
winPower = a[cur()];
return;
}
swap(cur(), next());
} else {
winCnt = 1;
}
}
winPower = n;
}
public static void main(String[] args) throws IOException {
// BufferedReader bin = new BufferedReader(new FileReader("cactus.in"));
BufferedReader bin = new BufferedReader(
new InputStreamReader(System.in));
TableTennis l = new TableTennis();
l.readData(bin);
l.calculate();
l.printRes();
}
}
| JAVA |
879_B. 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. | 2 | 8 | n,k=map(int,input().split(' '))
a=list(map(int,input().split(' ')))
s=0
e=0
f=1
if k>=n-1:
e=max(a)
else:
while s<k:
if a[0]>a[1] and f!=1:
s+=1
c=a[1]
del a[1]
a.append(c)
elif f==1:
s+=1
c=max(a[0],a[1])
d=min(a[0],a[1])
del a[0]
del a[0]
a.insert(0,c)
a.append(d)
else:
s=1
c=a[0]
del a[0]
a.append(c)
f-=1
e=a[0]
print(e)
| PYTHON3 |
879_B. 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. | 2 | 8 | n,k=map(int,raw_input().split())
nums=map(int,raw_input().split())
cons=max(nums[0],nums[1])
ct=1
ind=2
while ind<n:
if ct==k:
break
#print "cons,nums[ind],ind",cons,nums[ind],ind
if cons>nums[ind]:
#print "cons>nums[ind]"
ct+=1
#print "new ct",ct
else:
#print "new cons"
ct=1
cons=nums[ind]
ind+=1
print cons
| PYTHON |
879_B. 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. | 2 | 8 | a,b=map(int,input().split())
z=list(map(int,input().split()))
p=z[0];s=0
for i in range(1,a):
if z[i]<p:s+=1
else:s=1;p=z[i]
if s>=b:break
print(p) | PYTHON3 |
879_B. 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. | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
void solve() {
long long n, k;
cin >> n >> k;
vector<int> a(n);
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
vector<int> cnt(n + 1);
deque<int> dq(a.begin(), a.end());
for (int i = 0; i < n - 1; ++i) {
if (dq[0] > dq[1]) {
swap(dq[0], dq[1]);
}
cnt[dq[1]]++;
if (cnt[dq[1]] >= k) {
cout << dq[1] << "\n";
return;
}
dq.push_back(dq[0]);
dq.pop_front();
}
cout << *max_element(a.begin(), a.end()) << "\n";
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
solve();
return 0;
}
| CPP |
879_B. 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. | 2 | 8 |
if __name__ == '__main__':
nk = input().split()
n = int(nk[0])
k = int(nk[1])
a = list(map(int, input().rstrip().split()))
winner = 0
if (k > n):
winner = max(a)
else:
q = []
for i in a:
q.append(i)
wins = 0
winner = q[0]
q.pop(0)
#print (q)
while (wins != k):
front = q[0]
if (winner > front):
wins = wins + 1
q.append(front)
q.pop(0)
#print(q)
else:
wins = 1
q.append(winner)
winner = q[0]
q.pop(0)
#print (q)
print (winner)
| PYTHON3 |
879_B. 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. | 2 | 8 | n, k = map(int, input().split())
a = [*map(int, input().split())]
current = 0
ans = a[0]
i = 1
while i < n :
if ans > a[i]:
current += 1
else:
current = 1
ans = a[i]
if current == k:
break
i += 1
print(ans)
| PYTHON3 |
879_B. 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. | 2 | 8 | n,m=map(int,input().split())
l=list(map(int,input().split()))
ka=max(l[0],l[1])
c=1
for i in range(2,n):
if(c==m):
break
if(l[i]>ka):
ka=l[i]
c=1
else:
c=c+1
print(ka)
| PYTHON3 |
879_B. 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. | 2 | 8 | n,k=[int(i) for i in raw_input().split()]
l=[int(i) for i in raw_input().split()]
m=max(l)
if l[0]==m or l[1]==m:
print max(l[0],l[1])
else:
if l[0]>l[1]:
wt=0
else:
wt=1
cw=1
for i in range(2,n):
if l[i]==m:
print m
break
elif l[i]>l[wt]:
cw=1
wt=i
else:
cw+=1
if cw==k:
print l[wt]
break | PYTHON |
879_B. 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. | 2 | 8 | import java.util.Scanner;
import java.util.StringTokenizer;
public class B {
public static Scanner sc = new Scanner(System.in);
public static StringTokenizer st;
public static void main(String[] args) {
loadLine();
int N = getInt();
long K = getLong();
int[] a = new int[N];
int toload = 0; // properly load everything.
int wins = 0;
int startidx = 0;
int cidx = 1;
loadLine();
a[0] = getInt();
while ((cidx != startidx)&&(wins) < K){
//System.out.println(startidx +" "+cidx+" "+wins+" "+K);
if(toload < N-1){
toload += 1;
a[toload] = getInt();
}
if (a[cidx] > a[startidx]){
wins = 1;
startidx = cidx;
}
else{
wins += 1;
}
cidx = (cidx + 1) % N;
}
System.out.println(a[startidx]);
}
public static void loadLine(){
st = new StringTokenizer(sc.nextLine());
}
public static int getInt(){
return Integer.parseInt(st.nextToken());
}
public static double getDouble(){
return Double.parseDouble(st.nextToken());
}
public static long getLong(){
return Long.parseLong(st.nextToken());
}
}
| JAVA |
879_B. 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. | 2 | 8 | n, k = map(int, input().rstrip().split(' '))
players = list(map(int, input().rstrip().split(' ')))
if n <= k:
k = n-1
winner = 0
winCount = 0
for i in range(1, n):
tempComp = max(players[i], players[winner])
if players[winner] == tempComp:
winCount += 1
else:
winCount = 1
winner = i
if winCount == k:
break
print(players[winner])
| PYTHON3 |
879_B. 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. | 2 | 8 | n,k = map(int, raw_input().split())
l = map(int, raw_input().split())
m = max(l)
ans = -1
curr = l[0]
run = 0
if curr == m:
ans = m
else:
for j in l[1:]:
if j == m:
ans = m
break
else:
if j < curr:
run += 1
if run == k:
ans = curr
break
else:
curr = j
run = 1
if run == k:
ans = curr
break
print ans | PYTHON |
879_B. 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. | 2 | 8 | firstInput = input().split()
n = int(firstInput[0])
k = int(firstInput[1])
players = list(map(int, input().rstrip().split()))
numOfWins = 0
temporaryArray = []
currentPlayerWinner = players[0]
if n == 2:
if players[0] > players[1]:
print(players[0])
else:
print(players[1])
elif k > n**2:
maximum = players[0]
index = 1
while index <= len(players) - 1:
if players[index] > maximum:
maximum = players[index]
index += 1
print(maximum)
else:
while numOfWins < k:
if players[0] > players[1]:
currentPlayerWinner = players[0]
numOfWins += 1
index = 2
temporaryArray.append(players[0])
while index <= (len(players) - 1):
temporaryArray.append(players[index])
index += 1
temporaryArray.append(players[1])
else:
numOfWins = 1
currentPlayerWinner = players[1]
temporaryArray.append(players[1])
index = 2
while index <= (len(players) - 1):
temporaryArray.append(players[index])
index += 1
temporaryArray.append(players[0])
players = temporaryArray
temporaryArray = []
print(players[0]) | PYTHON3 |
879_B. 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. | 2 | 8 | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Collection;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.util.Queue;
import java.io.BufferedReader;
import java.util.LinkedList;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author zcw
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskB solver = new TaskB();
solver.solve(1, in, out);
out.close();
}
static class TaskB {
public void solve(int testNumber, InputReader in, PrintWriter out) {
int n = in.nextInt();
long k = in.nextLong();
int p = 0;
Queue<Integer> queue = new LinkedList<>();
for (int i = 0; i < n; i++) {
int x = in.nextInt();
if (p < x) p = x;
queue.add(x);
}
if (k >= n) {
out.println(p);
} else {
int win = 0;
int cur = queue.poll();
while (!queue.isEmpty() && win < k) {
int nxt = queue.poll();
if (cur > nxt) {
win++;
queue.add(nxt);
} else {
win = 1;
queue.add(cur);
cur = nxt;
}
}
out.println(cur);
}
}
}
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream));
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
}
}
| JAVA |
879_B. 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. | 2 | 8 | R=lambda:list(map(int,input().split()))
n,k=R()
a=R()
c=[0 for i in range(n+1)]
while 1>0:
if a[0]>a[1]: a[0],a[1]=a[1],a[0]
a=a[1:]+a[:1]
c[a[0]]+=1
if a[0]==n or c[a[0]]>=k:
print(a[0])
break | PYTHON3 |
879_B. 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. | 2 | 8 | n,k = map(int,input().split())
a= list(map(int,input().split()))
if k>=n:
print(max(a))
else:
win_player = a[0]
win_streak = 0
for power in a[1:] :
if win_player > power:
win_streak +=1
else:
win_player = power
win_streak = 1
if win_streak ==k:
break
print(win_player)
| PYTHON3 |
879_B. 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. | 2 | 8 | import java.util.Scanner;
public class TableTennis {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner s = new Scanner(System.in);
int n = s.nextInt();
long win = s.nextLong();
int f = 0;
int x = 0;
int[] a = new int[n];
for (int i = 0;i < n;i++) {
int l = s.nextInt();
a[i] = l;
}
int i = a[x];
while(i < n && f < win && x+1<n) {
if(i > a[x+1]) {
x++;
f++;
} else {
i = a[x+1];
x++;
f = 1;
}
}
System.out.println(i);
}
}
| JAVA |
879_B. 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. | 2 | 8 | # -*- coding: utf-8 -*-
import math
import collections
import bisect
import heapq
import time
import random
import itertools
import sys
"""
created by shhuan at 2017/10/28 15:51
"""
N, K = map(int, input().split())
P = [int(x) for x in input().split()]
if K >= N-1:
print(max(P))
else:
if P[0] > max(P[1: K+1]):
print(P[0])
exit(0)
for i in range(1, N):
if P[i] > max(P[:i] or [float('-inf')]):
tail = []
p = P[0]
for j in range(1, i):
tail.append(min(p, P[j]))
p = max(p, P[j])
tail.append(p)
tail = P[i+1: ] + tail
if P[i] > max(tail[:K-1]):
print(P[i])
exit(0)
| PYTHON3 |
879_B. 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. | 2 | 8 | '''
Codeforces: B. Table Tennis
'''
def queue(list_, k):
wins = 0
while wins < k:
if int(list_[0]) > int(list_[1]):
index = 1
wins+=1
else:
index = 0
wins = 1
list_.append(list_[index])
list_.pop(index)
return list_[0]
first = input().split()
k = int(first[1])
list_ = input().split()
if k > len(list_)-1:
k = len(list_)-1
print(queue(list_, k)) | PYTHON3 |
879_B. 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. | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int main() {
int length, backToBack;
scanf("%d%d", &length, &backToBack);
int count = 0;
long long winnerForce;
scanf("%lld", &winnerForce);
for (int i = 1; i < length; i++) {
long long force;
scanf("%lld", &force);
if (count < backToBack) {
if (force > winnerForce) {
winnerForce = force;
count = 1;
} else
count++;
}
}
printf("%d", winnerForce);
return 0;
}
| CPP |
879_B. 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. | 2 | 8 | nk = input().split()
n = int(nk[0])
k = int(nk[1])
players = [int(i) for i in input().split()]
if k>n:
print(max(players))
else:
playing = players[0:2]
queue = players[2:]
win = False
wins = 0
while wins<k:
if playing[0]>playing[1]:
wins+=1
queue.append(playing[1])
playing[1] = queue.pop(0)
else:
wins=1
playing[0], playing[1] = playing[1], playing[0]
queue.append(playing[1])
playing[1] = queue.pop(0)
print(playing[0]) | PYTHON3 |
879_B. 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. | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int n;
long long k;
int power[555];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> k;
for (int i = 0; i < n; i++) cin >> power[i];
queue<int> line;
for (int i = 2; i <= n - 1; i++) line.push(i);
int p1 = 0;
int p2 = 1;
int consecutive = 0;
while (true) {
int win, lost;
if (power[p1] < power[p2]) {
win = p2;
lost = p1;
consecutive = 1;
} else {
win = p1;
lost = p2;
consecutive++;
}
if (consecutive >= k) {
cout << power[win] << endl;
return 0;
}
if (consecutive > n) {
cout << power[win] << endl;
return 0;
}
line.push(lost);
int nex = line.front();
line.pop();
p1 = win;
p2 = nex;
}
return 0;
}
| CPP |
879_B. 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. | 2 | 8 | from collections import deque
n, k = map(int, input().split())
arr = deque(list(map(int, input().split())))
major = arr.popleft()
cnt = 0
while cnt < min(k, n):
item = arr.popleft()
if item < major:
arr.append(item)
cnt += 1
else:
cnt = 1
arr.append(major)
major = item
print(major)
| PYTHON3 |
879_B. 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. | 2 | 8 | # your code goes here
n,k = input().split()
n=int(n)
k=int(k)
a = list(map(int,input().split()))
count=0
i=0
j=1
while True:
if(k>n):
print(max(a))
break
if(a[i]>a[j]):
count+=1
if(count==k):
print(a[i])
break
a.append(a[j])
a.remove(a[j])
else:
a.append(a[i])
a.remove(a[i])
count=1
| PYTHON3 |
879_B. 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. | 2 | 8 | n,k = map(int, input().split())
m=list(map(int,input().split()))
a=max(m)
d=max(m[0],m[1])
s=1
for i in m[2:]:
if i==a:
d=a
break
if i>d:
d=i
s=1
else:
s+=1
if s==k:
break
print(d) | PYTHON3 |
879_B. 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. | 2 | 8 | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.StringTokenizer;
import javax.print.attribute.standard.MediaSize.ISO;
public class B {
static int n ;
static long K ;
static int[] num = new int[500];
static ArrayList<coord> p = new ArrayList<>();
static Comparator< coord > comp = new Comparator<coord>() {
@Override
public int compare(coord o1, coord o2) {
return Integer.compare( o1.power , o2.power );
}
};
static int ok( int i , int k , int d ){
int count = d ;
int j = i+1 ;
while( j != n && count != k && num[i] > num[j] ){
j++;
count++;
}
if( count == k ) return -1 ;
return j ;
}
static int solve( int k ){
int s = p.size() ;
int d = 0 ;
for (int i = 0 ; i < s-1 ;) {
int lasti = i ;
i = ok( i , k , d );
if( i == -1 ) return num[lasti];
d = 1 ;
}
return p.get( s-1 ).power ;
}
public static void main(String[] args) throws IOException {
BufferedReader bf = new BufferedReader( new InputStreamReader( System.in ) );
StringTokenizer token = new StringTokenizer( bf.readLine() );
n = Integer.valueOf( token.nextToken() );
K = Long.valueOf( token.nextToken() );
token = new StringTokenizer( bf.readLine() );
for (int i = 0; i < n; i++){
int a = Integer.valueOf( token.nextToken() );
num[i] = a ;
p.add( new coord( i , a ) );
}
Collections.sort( p , comp );
if( K >= n ){
System.out.println( p.get( p.size()-1 ).power);
}else
System.out.println( solve( (int )K ) );
}
}
class coord{
int idx ;
int power;
public coord(int idx, int power) {
super();
this.idx = idx;
this.power = power;
}
} | JAVA |
879_B. 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. | 2 | 8 | firstInput = input().split(" ")
numberPlayers = int(firstInput[0])
numberWinsNecessary = int(firstInput[1])
powerPlayers = []
secondInput = input().split(" ")
for i in range(numberPlayers):
powerPlayers.append(int(secondInput[i]))
lastWinner = powerPlayers[0]
wins = 0
while wins < numberWinsNecessary and len(powerPlayers) > 1:
if lastWinner > powerPlayers[1]:
powerPlayers.pop(1)
wins += 1
else:
powerPlayers[0] = powerPlayers[1]
powerPlayers.pop(1)
lastWinner = powerPlayers[0]
wins = 1
print(lastWinner)
| PYTHON3 |
879_B. 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. | 2 | 8 | #http://codeforces.com/problemset/problem/879/B
from collections import deque
inp = lambda: map(int, input().split())
n, k = inp()
player = deque(list(inp()))
win = 0
while win < k:
if player[0] > player[1]:
win += 1
winner = player.popleft()
loser = player.popleft()
player.appendleft(winner)
player.append(loser)
else:
win = 1
loser = player.popleft()
player.append(loser)
if win >= n:
break
print(player[0])
| PYTHON3 |
879_B. 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. | 2 | 8 | n, k = map(int, input().split())
a = list(map(int, input().split()))
get = 1
ans = max(a[0], a[1])
for i in range(2, n):
if (ans > a[i]): get +=1
else: ans = a[i]; get =1
if (get == k): break
print(ans) | PYTHON3 |
879_B. 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. | 2 | 8 | s1 = input()
n = int(s1.split()[0])
k = int(s1.split()[1])
s2 = input()
pl = []
for i in s2.split():
pl.append(int(i))
rang = True
c = 0
p1 = pl[0]
j=1
while(c!=k):
if j==n:
break
if p1>pl[j]:
j+=1
c+=1
else:
p1 = pl[j]
j+=1
c=1
print(p1) | PYTHON3 |
879_B. 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. | 2 | 8 | import java.util.*;
import java.io.*;
public class _879B {
static void empty(int[] a, int i){
for (int j = 0; j < a.length; j++) {
if (j == i) continue;
a[j] = 0;
}
}
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int n = in.nextInt();
long k = in.nextLong();
int[] cnt = new int[n+1];
Deque<Integer> Q = new ArrayDeque<>();
for (int i = 0; i < n; i++) {
Q.addLast(in.nextInt());
}
if (k>= n){
System.out.println(n);
} else {
while (true){
int a = Q.removeFirst();
int b = Q.removeFirst();
if (a > b){
Q.addFirst(a);
Q.addLast(b);
cnt[a]++;
empty(cnt, a);
if (cnt[a] == k){
System.out.println(a);
return;
}
} else {
Q.addFirst(b);
Q.addLast(a);
cnt[b]++;
empty(cnt, b);
if (cnt[b] == k){
System.out.println(b);
return;
}
}
//System.out.println(Q);
}
}
}
}
| JAVA |
879_B. 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. | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int main() {
int n, a;
long long int k;
int wins = 0;
int cur;
cin >> n >> k;
cin >> cur;
for (int i = 1; i < n; ++i) {
cin >> a;
if (a > cur) {
cur = a;
wins = 1;
} else {
++wins;
}
if (wins == k) {
cout << cur;
return 0;
}
}
cout << cur;
return 0;
}
| CPP |
879_B. 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. | 2 | 8 | n,k = map(int,input().split())
l = list(map(int,input().split()))
if k>=n-1:
print(max(l))
else:
count = 0
max = l[0]
for i in range(1,n+k+1):
if max>l[i]:
l.append(l[i])
count += 1
else:
l.append(max)
max = l[i]
count = 1
if count==k:
break
print(max) | PYTHON3 |
879_B. 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. | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
;
int n;
long long int k;
cin >> n >> k;
int a[n], maxi = 0, p;
for (int i = 0; i < n; i++) {
cin >> a[i];
if (a[i] > maxi) {
maxi = a[i];
p = i;
}
}
if (p == 0 || k >= p) {
cout << maxi << endl;
} else {
int ans = -1, c = 0;
for (int i = 0; i < n - 1; i++) {
if (a[i] > a[i + 1]) {
c++;
a[i + 1] = a[i];
if (c == k) {
ans = a[i + 1];
break;
}
} else
c = 1;
}
if (ans == -1)
cout << maxi << endl;
else
cout << ans << endl;
}
return 0;
}
| CPP |
879_B. 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. | 2 | 8 | from collections import deque
p, k = list(map(int, input().split()))
players = deque(list(map(int, input().split())))
wins = {}
for i in players:
wins[i] = 0
max_p = max(players)
index_max = players.index(max_p)
if index_max <= k:
print(max_p)
else:
while True:
if players[0] > players[1]:
aux = players[0]
players[0] = players[1]
players[1] = aux
first = players.popleft()
players.append(first)
wins[players[0]] += 1
wins[players[1]] = 0
else:
wins[players[0]] = 0
wins[players[1]] += 1
aux = players.popleft()
players.append(aux)
if wins[players[0]] == k:
print(players[0])
break
| PYTHON3 |
879_B. 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. | 2 | 8 | def tableTennis(wins, players):
p = players
idx = 0
while idx < len(p):
win_count = 0
has_fought = []
fight_count = 0
while True:
fight_count += 1
if(p[0] > p[1]):
if fight_count == 1 and p[0] > p[-1]:
win_count += 2
else:
win_count += 1
has_fought.append(p[1])
if win_count == wins or (len(has_fought) + 1 == len(p) and max(p) == p[0]):
print(p[0])
return
else:
temp = p[1]
p.remove(temp)
p.append(temp)
else:
val = p.pop(0)
p.append(val)
break
idx += 1
def main():
first_line = [int(x) for x in input().split(' ')]
second_line = [int(x) for x in input().split(' ')]
tableTennis(first_line[1], second_line)
main()
| PYTHON3 |
879_B. 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. | 2 | 8 | def main():
n, k = map(int, raw_input().split())
arr = list(map(int, raw_input().split()))
if k >= n:
print max(arr)
return
last_winner = 0
curr_streak = 0
for i in range(1, n):
if arr[i] > arr[last_winner]:
winner = i
else:
winner = last_winner
if winner != last_winner:
curr_streak = 1
last_winner = winner
else:
curr_streak += 1
if curr_streak == k:
print arr[last_winner]
return
print arr[last_winner]
if __name__ == '__main__':
main()
| PYTHON |
879_B. 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. | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
int a[600], st;
long long k;
cin >> n >> k;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
int ans = a[0];
st = 0;
for (int i = 1; i < n; i++) {
if (st >= k) {
cout << ans << endl;
return 0;
}
if (ans > a[i])
st++;
else {
st = 1;
ans = a[i];
}
}
cout << ans << endl;
return 0;
}
| CPP |
879_B. 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. | 2 | 8 |
import java.io.BufferedReader;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import static java.lang.Math.*;
public class TableTennis implements Closeable {
private InputReader in = new InputReader(System.in);
private PrintWriter out = new PrintWriter(System.out);
public void solve() {
int n = in.ni();
long k = in.nl();
int[] power = new int[n];
int idx = -1;
for (int i = 0; i < n; i++) {
power[i] = in.ni();
if (power[i] == n) idx = i;
}
int[] cnt = new int[n];
boolean[] tail = new boolean[n];
for (int i = 0; i < idx; i++) {
if (!tail[i]) {
for (int j = i + 1; j < idx; j++) {
if (power[i] > power[j]) {
cnt[i]++;
tail[j] = true;
} else {
tail[i] = true;
cnt[j]++;
break;
}
if (cnt[i] == k) {
out.println(power[i]);
return;
}
}
}
}
out.println(power[idx]);
}
@Override
public void close() throws IOException {
in.close();
out.close();
}
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int ni() {
return Integer.parseInt(next());
}
public long nl() {
return Long.parseLong(next());
}
public void close() throws IOException {
reader.close();
}
}
public static void main(String[] args) throws IOException {
try (TableTennis instance = new TableTennis()) {
instance.solve();
}
}
}
| JAVA |
879_B. 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. | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int main() {
long long n, k;
cin >> n >> k;
long long pla, num, a, b, cnt = 0;
cin >> a;
for (long long i = 1; i < n; i++) {
cin >> b;
if (a > b)
cnt++;
else
cnt = 1, a = b;
if (a == n or cnt == k) {
cout << a << endl;
break;
}
}
}
| CPP |
879_B. 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. | 2 | 8 | userInput = input()
inputL1P1 = int(userInput.split()[0])
inputL1P2 = int(userInput.split()[1])
userInput2 = input()
inputArray = userInput2.split()
inputArray = [ int(x) for x in inputArray ]
winCounter = 0
for i in range (inputL1P1):
if i == inputL1P1:
break
if winCounter == inputL1P2:
break
if inputArray[0] > inputArray[i]:
winCounter = winCounter + 1
elif inputArray[0] < inputArray[i]:
winCounter = 1
inputArray[0] = inputArray[i]
print(str(inputArray[0])) | PYTHON3 |
879_B. 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. | 2 | 8 | import java.util.*;
import java.io.*;
public class Main {
static InputReader in;
static PrintWriter out;
public static void main(String[] args) {
in = new InputReader();
out = new PrintWriter(System.out);
solve();
out.close();
}
static void solve() {
int n = in.nextInt();
long k = in.nextLong();
int[] a = new int[n];
int max = -1;
for (int i = 0; i < n; i++) {
a[i] = in.nextInt();
max = Math.max(max, a[i]);
}
int[] cnt = new int[n + 1];
if (k > n - 1) {
out.println(max);
} else {
Deque<Integer> q = new LinkedList<>();
for (int i = 0; i < n; i++) {
q.add(a[i]);
}
while (true) {
int f = q.remove();
int s = q.remove();
if (f > s) {
q.addFirst(f);
q.addLast(s);
if (++cnt[f] >= k) {
break;
}
} else {
q.addFirst(s);
q.addLast(f);
if (++cnt[s] >= k) {
break;
}
}
}
out.println(q.peekFirst());
}
}
static class InputReader {
BufferedReader br;
StringTokenizer st;
public InputReader() {
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}
| JAVA |
879_B. 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. | 2 | 8 | import java.util.*;
import java.io.*;
public class codeforces
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int n = in.nextInt();
long k = in.nextLong();
int[] a = new int[n];
for(int i=0;i<n;i++)
a[i] = in.nextInt();
int max = Math.max(a[0], a[1]),ans = max;
long count = 1;
for(int i=2;i<n;i++)
{
if(max>a[i])
count++;
else
{
count = 1;
max = a[i];
}
if(count==k)
{
System.out.println(max);
return;
}
ans = Math.max(a[i], ans);
}
System.out.println(ans);
}
}
| JAVA |
879_B. 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. | 2 | 8 | import queue
N, K = map(int, input().split())
A = list(map(int, input().split()))
Q = queue.Queue()
if(K > N):
print(max(A))
else:
B, W = A[0], 0
for i in range(1, N):
Q.put(A[i])
while True:
top = Q.get()
if B > top:
Q.put(top)
W += 1
else:
Q.put(B)
B, W = top, 1
if W >= K:
print(B)
break
| PYTHON3 |
879_B. 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. | 2 | 8 | n, k = [int(x) for x in input().split()]
power = [int(x) for x in input().split()]
achou = False
winner = 0
if k >= len(power):
winner = max(power)
achou = True
vitorias = 0
while not achou:
if power[0] > power[1]:
temp = power[0]
power.append(power[1])
power.pop(1)
else:
temp = power[1]
power.append(power[0])
power.pop(0)
if winner == temp:
vitorias += 1
else:
winner = temp
vitorias = 1
if vitorias == k:
achou = True
print(winner)
| PYTHON3 |
879_B. 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. | 2 | 8 | import math
n , k = map (int , input().split())
l = list( map(int , input().split()))
ll= list(l+l)
cnt = int (0)
if(k>= n-1 ):print (n)
else:
for i in ll:
if(cnt==0):
if(i==max (ll[cnt:cnt+k+1])):
print(i)
break
elif(i==max (ll[cnt-1:cnt+k])):
print(i)
break
cnt+=1
| PYTHON3 |
879_B. 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. | 2 | 8 | import java.util.Scanner;
public class tableTennis{
public static void main(String[] args){
Scanner kbd = new Scanner(System.in);
int n = kbd.nextInt();
long k = kbd.nextLong();
int answer = kbd.nextInt();
int b = 0;
for(int i = 1; i < n; i++){
int a = kbd.nextInt();
if(b >= k){
break;
}else if(a > answer){
b = 1;
answer = a;
}else{
b++;
}
}
System.out.println(answer);
}
} | JAVA |
879_B. 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. | 2 | 8 | from collections import deque
nums = input().split(' ')
powers = input().split(' ')
n = int(nums[0])
k = int(nums[1])
queue = []
#result = -1
for i in range(n):
queue.append(int(powers[i]))
#result = max(result, num)
ans = queue[0]
s = 0
for j in range(1,n):
if s >= k:
#print(ans)
break
elif ans > queue[j]:
s = s + 1
else:
s = 1
ans = queue[j]
print(ans) | PYTHON3 |
879_B. 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. | 2 | 8 | #include <bits/stdc++.h>
int main() {
int qu[100009], head = 1, tail = 0;
long long n, k;
int x;
scanf("%lld%lld", &n, &k);
int mmax = 0;
for (int i = 1; i <= n; i++) {
scanf("%d", &x);
if (x > mmax) mmax = x;
qu[++tail] = x;
}
if (k >= n - 1) {
printf("%d\n", mmax);
} else {
long long sum = 0;
while (1) {
if (qu[head] > qu[head + 1]) {
sum++;
qu[++tail] = qu[head + 1];
qu[head + 1] = qu[head];
head++;
} else {
sum = 1;
qu[++tail] = qu[head];
head++;
}
if (k == sum) {
printf("%d\n", qu[head]);
break;
}
}
}
return 0;
}
| CPP |
879_B. 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. | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
const int N = 2005;
void solve() {
long long int n, k;
cin >> n >> k;
long long int a[n];
for (long long int i = 0; i < n; i++) cin >> a[i];
if (k >= n) {
cout << *max_element(a, a + n);
} else {
deque<long long int> q;
for (int i = 2; i < n; i++) q.push_back(a[i]);
long long int cnt = 1, ele = max(a[0], a[1]);
q.push_back(min(a[0], a[1]));
while (cnt < k) {
long long int top = q.front();
q.pop_front();
if (top > ele) {
q.push_back(ele);
cnt = 1;
ele = top;
} else {
cnt++;
q.push_back(top);
}
}
cout << ele;
}
}
int main() {
ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
srand(time(NULL));
;
long long int t = 1;
for (long long int i = 1; i <= t; i++) {
solve();
}
}
| CPP |
879_B. 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. | 2 | 8 | import java.util.Scanner;
import java.util.Queue;
import java.util.LinkedList;
import java.lang.Math;
public class Main{
public static void main(String args[]){
Scanner input = new Scanner(System.in);
String[] nk = (input.nextLine()).split(" ");
long n = Long.parseLong(nk[0]);
long k = Long.parseLong(nk[1]);
Queue<Long> queue = new LinkedList<Long>();
String[] powers = (input.nextLine()).split(" ");
for(int i=0; i < n;i++){
queue.add(Long.parseLong(powers[i]));
}
System.out.println(andTheWinnerIs(queue, k, n));
}
private static long andTheWinnerIs(Queue<Long> queue, long k, long n){
long loser = -1;
long winCount = 0;
long winner = queue.remove();
while(winCount<k){
long player2 = queue.remove();
long mostPowerful = Math.max(winner, player2);
loser = Math.min(winner, player2);
if(mostPowerful==winner){
winCount++;
}else{
winner = mostPowerful;
winCount = 1;
}
queue.add(loser);
if(winCount>n && k > n){
break;
}
}
return winner;
}
} | JAVA |
879_B. 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. | 2 | 8 | inp=lambda:map(int,input().split())
n,k=inp()
a=list(inp())
if(k>n):
print(max(a))
quit()
line=[0]*501
win=[0]*501
for i in range(n):
line[i]=i
max_win=0;
for i in range(n+k+2):
if(a[line[0]]>a[line[1]]):
win[line[0]]+=1
if win[line[0]]>max_win:
max_win=win[line[0]]
if(max_win>=k):
print(a[line[0]])
quit()
x=line[1]
for j in range(1,n-1):
line[j]=line[j+1]
line[n-1]=x
else:
win[line[1]]+=1
if win[line[0]]>max_win:
max_win=win[line[1]]
if(max_win>=k):
print(a[line[1]])
quit()
x=line[0]
for j in range(0,n-1):
line[j]=line[j+1]
line[n-1]=x
| PYTHON3 |
879_B. 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. | 2 | 8 | from collections import deque
nk = input().split()
n = int(nk[0])
k = int(nk[1])
if n<=k:
k = n-1
power = deque(map(int, input().rstrip().split()))
winner = power.popleft()
winCount = 0
while winCount != k:
enemy = power[0]
if winner > enemy:
power.append(enemy)
power.popleft()
winCount += 1
else:
power.append(winner)
winner = power.popleft()
winCount = 1
print (winner)
| PYTHON3 |
879_B. 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. | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
int n;
cin >> n;
long long k;
cin >> k;
vector<int> a(n);
int i;
int mx_pos = -1;
for (i = 0; i < n; i++) {
cin >> a[i];
if (a[i] == n) {
mx_pos = i;
}
}
int p1 = -1, p2 = -1;
long long w = 0;
for (i = 0; w < k && i < mx_pos; i++) {
if (p1 == -1) {
p1 = a[i];
} else if (p2 == -1) {
p2 = a[i];
}
if (p2 != -1) {
if (p1 < p2) {
p1 = p2;
p2 = -1;
w = 1;
} else {
p2 = -1;
w++;
}
}
}
if (w == k) {
cout << p1;
} else {
cout << n;
}
return 0;
}
| CPP |
879_B. 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. | 2 | 8 | from collections import deque
n, k = [int(p) for p in input().split()]
q = deque([int(p) for p in input().split()])
curr = q[0]
count = 0
while k > count:
p1 = q.popleft()
p2 = q.popleft()
win = max(p1, p2)
los = min(p1, p2)
if win == curr:
count += 1
else:
curr = win
count = 1
q.append(los)
q.appendleft(win)
if win == n:
break
print(win)
| PYTHON3 |
879_B. 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. | 2 | 8 | from collections import deque
def funcao(queue, k):
if k >= len(queue):
return max(queue)
win = queue.popleft()
cont = 0
while cont < k:
prox = queue.popleft()
if win >= prox:
queue.append(prox)
cont += 1
else:
queue.append(win)
win = prox
cont = 1
return win
k = int(input().split()[1])
lista = map(int, input().split())
queue = deque(lista)
print(funcao(queue, k)) | PYTHON3 |
879_B. 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. | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int n;
long long k;
int main() {
cin >> n >> k;
long long b;
long long c = 0, a;
cin >> b;
for (int i = 2; i <= n; i++) {
cin >> a;
if (b > a)
c++;
else {
b = a, c = 1;
}
if (c == k) {
cout << b;
return 0;
}
}
cout << b;
return 0;
}
| CPP |
879_B. 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. | 2 | 8 | import java.util.*;
import java.io.*;
public class Codeforces {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
long k = Long.parseLong(st.nextToken());
Queue<Integer> queue = new LinkedList<>();
st = new StringTokenizer(br.readLine());
int max = 0;
for(int i = 0; i < n; i++){
int tmp = Integer.parseInt(st.nextToken());
queue.add(tmp);
max = Math.max(max, tmp);
}
int counter = 0;
boolean f = false;
while(counter < n){
int cur = queue.poll();
int c;
if(!f){
c = 0;
}else {
c = 1;
}
while(!queue.isEmpty() && queue.peek() < cur && c < k && c < n){
queue.add(queue.poll());
c++;
}
if(c == k){
System.out.println(cur);
return;
}
if(!queue.isEmpty() && queue.peek() > cur){
queue.add(cur);
f = true;
}
counter++;
}
System.out.println(max);
}
} | JAVA |
879_B. 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. | 2 | 8 | n,k = map(int,raw_input().split())
a = map(int,raw_input().split())
if k>=n-1:
print n
else:
nbw = 0
toplay = 0
chall = 1
while nbw<=k:
if a[toplay]==n or nbw==k:
print a[toplay]
break
if a[toplay]>a[chall]:
nbw+=1
else:
toplay = chall
nbw = 1
chall +=1 | PYTHON |
879_B. 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. | 2 | 8 | import java.util.Scanner;
import java.util.Deque;
import java.util.LinkedList;
public class Table_Tennis {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scan = new Scanner(System.in);
int count = scan.nextInt();
long loop = scan.nextLong();
if (loop > count) {
loop = count;
}
int first = scan.nextInt();
Deque deque = new LinkedList<>();
for (int i = 0; i < count - 1; i++) {
deque.add(scan.nextInt());
}
for (int i = 0; i < loop; i++) {
if (first > (int) deque.getFirst()) {
deque.add(deque.pop());
} else {
deque.add(first);
first = (int) deque.pop();
i = 0;
}
}
System.out.print(first);
}
}
| JAVA |
879_B. 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. | 2 | 8 | while True:
try:
n,k=map(int,input().split())
a=input().split()
ans=int(a[0])
st=0
for i in range(1,n):
if st>=k:
break
if ans>int(a[i]):
st+=1
else:
st=1
ans=int(a[i])
print(ans)
except EOFError:
break | PYTHON3 |
879_B. 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. | 2 | 8 |
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
public class duh {
public static void main(String[] args) {
Scanner ob = new Scanner(System.in);
int n=ob.nextInt();
long k=ob.nextLong();
int[] ar=new int[n];
for(int i=0;i<n;i++)
{
ar[i]=ob.nextInt();
}
int max=Integer.MIN_VALUE;
for(int j=0;j<n;j++)
{
max=Math.max(ar[j],max);
}
int s=0,index=0;
for(int j=index+1;j<n;j++)
{
int x=ar[index];
if(x==max)
{break;}
if(x>ar[j])
s++;
else {
index=j;
s=1;
}
if(s==k)
{break;}
}
if(index>=n)
System.out.println(max);
else
System.out.println(ar[index]);
}}
| JAVA |
879_B. 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. | 2 | 8 | n, k = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]
wins = []
for i in range(n):
wins.append(0)
while True:
if a[0] > a[1]:
a.append(a[1])
del a[1]
wins.append(wins[1])
del wins[1]
wins[0] += 1
else:
a.append(a[0])
del a[0]
wins.append(wins[0])
del wins[0]
wins[0] += 1
if wins[0] == k:
print(a[0])
break
else:
if a[0] == max(a):
print(a[0])
break
| PYTHON3 |
879_B. 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. | 2 | 8 | from fractions import gcd
n,k = map(int,raw_input().split())
s = map(int,raw_input().split())
i = [0]*n
a = 0
b = 1
if k<n:
while max(i)<k:
if s[a]>s[b]:
b = (b+1)%n
i[a]+=1
else:
a=b
i[a]+=1
b = (b+1)%n
#print a,b
print s[a]
else:
print max(s) | PYTHON |
879_B. 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. | 2 | 8 | R=lambda:map(int,raw_input().split())
n,k=R()
a=R()
w,i,c,m=0,1,0,max(a)
while w<k and a[0]!=m:
if a[0]<a[i]:
w=0
a[0],a[i]=a[i],a[0]
w+=1
i+=1
if i>=n:
i=1
print a[0] | PYTHON |
879_B. 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. | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int main() {
int n, powers[510];
long long k;
vector<int> vec1;
cin >> n >> k;
for (int i = 0; i < n; i++) {
scanf("%d", &powers[i]);
vec1.push_back(powers[i]);
}
if (k >= n - 1)
printf("%d\n", *max_element(powers, powers + n));
else {
int num = 0;
int flag = 0;
int count = 0;
while (!flag) {
for (vector<int>::iterator v = vec1.begin() + 1; v != vec1.end(); v++) {
if (*vec1.begin() > *v) {
num++;
if (num == k) {
printf("%d\n", *vec1.begin());
flag = 1;
break;
}
} else {
count = 0;
for (vector<int>::iterator v1 = vec1.begin() + 1; v1 != v; v1++) {
vec1.push_back(*v1);
count++;
}
vec1.push_back(*vec1.begin());
num = 1;
vec1.erase(vec1.begin(), vec1.begin() + count + 1);
break;
}
}
}
}
return 0;
}
| CPP |
879_B. 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. | 2 | 8 |
n,k = map(int,input().split())
t =list(map(int,input().split()))
a=t[0]
p=0
h=-1
for j in range(n):
if a>t[j]:
p+=1
elif a<t[j]:
a=t[j]
p=1
if p==k:
h=a
break
if h!=-1:
print(h)
else:
print(max(t))
| PYTHON3 |
879_B. 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. | 2 | 8 | # @author Nayara Souza
# UFCG - Universidade Federal de Campina Grande
# AA - Basico
n, k = list(map(int, input().split()))
x = list(map(int, input().split()))
count = 0
j = x[0]
for i in range(1,n):
if count >= k:
break
if j > x[i]:
count += 1
else:
j = x[i]
count = 1
print(j)
| PYTHON3 |
879_B. 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. | 2 | 8 | n, k = map(int, input().split())
a = list(map(int, input().split()))
maxa = max(a)
if len(a) <= k:
print(maxa)
else:
curr = a[0]
j = 0
for i in range(1, len(a)):
if j == k:
print(curr)
break
if a[i] == maxa:
print(maxa)
break
if a[i] > curr:
curr = a[i]
j = 1
else:
j += 1
| PYTHON3 |
879_B. 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. | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
template <typename T>
void takearray(vector<T> &a, long long int n) {
T y;
for (long long int i = 0; i < n; i++) {
cin >> y;
a.push_back(y);
}
}
template <typename T>
void print1d(vector<T> &a) {
for (long long int i = 0; i < a.size(); i++) {
cout << a[i] << " ";
}
cout << endl;
}
void sieveoferatosthenes(long long int n, vector<long long int> &a) {
vector<bool> prime(n + 1, true);
for (long long int p = 2; p * p <= n; p++) {
if (prime[p] == true) {
for (long long int i = p * p; i <= n; i += p) prime[i] = false;
}
}
for (long long int p = 2; p <= n; p++)
if (prime[p]) a.push_back(p);
}
long long int first_BS(vector<long long int> a, long long int low,
long long int high, long long int x) {
while (low <= high) {
long long int mid = low + (high - low) / 2;
if ((mid == 0 || x > a[mid - 1]) && a[mid] == x)
return mid;
else if (a[mid] < x)
low = mid + 1;
else
high = mid - 1;
}
return -1;
}
long long int last_BS(vector<long long int> a, long long int low,
long long int high, long long int x) {
while (low <= high) {
long long int mid = low + (high - low) / 2;
if ((mid == a.size() - 1 || x < a[mid + 1]) && a[mid] == x)
return mid;
else if (x < a[mid])
high = mid - 1;
else
low = mid + 1;
}
return -1;
}
bool sortbool(pair<long long int, long long int> &a,
pair<long long int, long long int> &b) {
return a.first < b.first;
}
vector<long long int> numberDivisior(long long int n) {
vector<long long int> b;
for (long long int i = 1; i <= sqrt(n); i++) {
if (n % i == 0) {
if (n / i == i)
b.push_back(i);
else {
b.push_back(n / i);
b.push_back(i);
}
}
}
return b;
}
long long int modiBinarySearch(long long int l, long long int r,
long long int k) {
long long int q, y, mid;
while (l <= r) {
mid = l + (r - l) / 2;
q = (mid * (mid - 1)) / 2;
y = (mid * (mid + 1)) / 2;
if (q == k)
return mid;
else if (q < k && y > k)
return mid;
else if (q > k)
r = mid - 1;
else
l = mid + 1;
}
return -1;
}
int32_t main() {
ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
;
long long int n, k;
cin >> n >> k;
vector<long long int> a(n, 0);
for (long long int i = 0; i < n; i++) {
cin >> a[i];
}
long long int cnt = 0, q = a[0];
for (long long int i = 1; i < n; i++) {
if (q > a[i])
cnt++;
else {
q = a[i];
cnt = 1;
}
if (k == cnt) {
cout << q << endl;
return 0;
}
}
cout << q << endl;
return 0;
}
| CPP |
879_B. 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. | 2 | 8 | from collections import Counter
n, k = map(int, input().split())
a = map(int, input().split())
d = Counter()
p1 = next(a)
for p2 in a:
if p1 > p2:
d[p1] += 1
else:
d[p2] += 1
p1 = p2
if d[p1] >= k:
print(p1)
exit(0)
print(n) | PYTHON3 |
879_B. 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. | 2 | 8 | import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException{
Reader.init(System.in);
int n = Reader.nextInt();
long k = Reader.nextLong();
int[] players = new int[n];
for (int i = 0; i < players.length; i++) {
players[i] = Reader.nextInt();
}
int player1 = 0;
int player2 = 1;
int[] wins = new int[players.length];
for (int i = 2; i < players.length; i++) {
if (players[player1] > players[player2]){
player2 = i;
wins[player1]++;
if (wins[player1] == k) {
System.out.println(players[player1]);
return;
}
}
else {
player1 = i;
wins[player2]++;
if (wins[player2] == k) {
System.out.println(players[player2]);
return;
}
}
}
System.out.println(Arrays.stream(players).max().getAsInt());
}
}
/**
* Reader class based on the article at "https://www.cpe.ku.ac.th/~jim/java-io.html"
* */
class Reader{
private static BufferedReader reader;
private static StringTokenizer tokenizer;
static void init(InputStream inputStream){
reader = new BufferedReader(new InputStreamReader(inputStream));
tokenizer = new StringTokenizer("");
}
private static String next() throws IOException {
String read;
while (!tokenizer.hasMoreTokens()){
read = reader.readLine();
if (read == null || read.equals(""))
return "-1";
tokenizer = new StringTokenizer(read);
}
return tokenizer.nextToken();
}
static int nextInt() throws IOException{
return Integer.parseInt(next());
}
static long nextLong() throws IOException{
return Long.parseLong(next());
}
//Get a whole line.
// static String line() throws IOException{
// return reader.readLine();
// }
//
// static double nextDouble() throws IOException{return Double.parseDouble(next());}
}
| JAVA |
879_B. 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. | 2 | 8 | import java.util.Scanner;
public class TableTennis {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
long k = sc.nextLong();
int p = sc.nextInt();
long c = 0;
for(int i = 1; i < n; i++) {
int r = sc.nextInt();
if(r > p) {
c = 1;
p = r;
} else {
c++;
}
if(c >= k) {
break;
}
}
System.out.println(p);
}
} | JAVA |
879_B. 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. | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int main() {
long long n, k;
cin >> n >> k;
int winner = 0;
long long fk = k;
for (int i = 0; i < n; i++) {
int temp;
cin >> temp;
if (winner > temp) {
fk--;
if (fk == 0) break;
} else {
winner = temp;
fk = k;
if (i != 0) --fk;
}
}
cout << winner << endl;
}
| CPP |
879_B. 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. | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
const long long mod2 = 998244353;
const int inf = 0x3f3f3f3f;
const double tiaohe = 0.57721566490153286060651209;
long long oula(long long x) {
long long res = x;
for (int i = 2; i <= x / i; ++i) {
if (x % i == 0) {
res = res / i * (i - 1);
while (x % i == 0) x /= i;
}
}
if (x > 1) res = res / x * (x - 1);
return res;
}
long long quickmod(long long a, long long n, long long m) {
long long s = 1;
while (n) {
if (n & 1) {
s = s * a % m;
}
a = (a * a) % m;
n = n / 2;
}
return s;
}
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
void ex_gcd(long long a, long long b, long long &x, long long &y,
long long &d) {
if (!b) {
d = a, x = 1, y = 0;
} else {
ex_gcd(b, a % b, y, x, d);
y -= x * (a / b);
}
}
long long inv(long long t, long long p) {
long long d, x, y;
ex_gcd(t, p, x, y, d);
return d == 1 ? (x % p + p) % p : -1;
}
bool isPrime(long long x) {
if (x == 2) return true;
if (x % 2 == 0) return false;
for (long long i = 2; i * i <= x; i++)
if (x % i == 0) return false;
return true;
}
inline int in() {
char ch = getchar();
int x = 0, f = 1;
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
double eqa = (1 + sqrt(5.0)) / 2.0;
const double eps = 1e-6;
const int N = 6e5 + 5;
;
int a[N];
int main() {
int n = in();
long long k;
cin >> k;
int mx = 0, mx_pos = 2e9;
for (int i = 1; i <= n; ++i) {
a[i] = in();
mx = max(a[i], mx);
}
for (int i = 1; i <= n; ++i) {
int num = 0;
for (int j = i + 1; j <= n; ++j) {
if (a[i] > a[j])
num++;
else
break;
}
if (i >= 2 && a[i] > a[i - 1]) num++;
if (num >= k) {
mx_pos = a[i];
break;
}
}
cout << min(mx_pos, mx) << endl;
return 0;
}
| CPP |
879_B. 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. | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
long long int gcd(long long int a, long long int b) {
if (b == 0) return a;
return gcd(b, a % b);
}
long long int lcm(long long int a, long long int b) {
return ((a * b) / gcd(a, b));
}
void scanint(int &x) {
register int c = getchar();
x = 0;
int neg = 0;
for (; ((c < 48 || c > 57) && c != '-'); c = getchar())
;
if (c == '-') {
neg = 1;
c = getchar();
}
for (; c > 47 && c < 58; c = getchar()) {
x = (x << 1) + (x << 3) + c - 48;
}
if (neg) x = -x;
}
int main() {
ios_base::sync_with_stdio(false);
long long int n, k, i, j, a, b;
cin >> n >> k;
int ar[n + 505];
int mi = 0, mv = -1;
for (i = 0; i < n; i++) {
cin >> ar[i];
if (ar[i] > ar[mi]) {
mi = i;
}
}
if (k >= mi) {
cout << ar[mi] << endl;
} else {
int vic[n + 1], f = 0;
memset(vic, 0, sizeof(vic));
for (i = 0; i < n - 1; i++) {
mv = max(ar[i], ar[i + 1]);
ar[i + 1] = mv;
vic[mv]++;
if (vic[mv] == k) {
f = 1;
cout << mv << endl;
break;
}
}
if (f == 0) cout << ar[mi] << endl;
}
return 0;
}
| CPP |
879_B. 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. | 2 | 8 | # n=int(input())
# n,k=map(int,input().split())
# arr=list(map(int,input().split()))
#ls=list(map(int,input().split()))
#for i in range(m):
# for _ in range(int(input())):
#from collections import Counter
#from fractions import Fraction
#s=iter(input())
from collections import deque
n,k=map(int,input().split())
arr=list(map(int,input().split()))
if n<=2:
print(max(arr))
exit() ###########if i==0 then player had not played previous match
####else he had win previous match
for i in range(n):
var=i+k
if i==0:
if var>=n:
if arr[i]>=max(arr[i:n]) and arr[i]>=max(arr[:(k-(n-i-1))+1]):
print(arr[i])
break
else:
if arr[i] >= max(arr[i:i+k+1]):
print(arr[i])
break
else:
#print(i)
if var>=n:
if arr[i]>=max(arr[i-1:n]) and arr[i]>=max(arr[:(k-(n-i-1))]):
print(arr[i])
break
else:
if arr[i]>=max(arr[i-1:i+k]):
print(arr[i])
break | PYTHON3 |
879_B. 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. | 2 | 8 | n,k=map(int,input().split(' '))
massiv = list(map(int, input().split(' ') ))
def sila_v_igroka(mas,k):
x=0
sila=0
if len(mas)<=k:
return(max(mas))
while x!=k :
if mas[0]>mas[1]:
sila=mas[0]
mas=[mas[0]]+mas[2:]+[mas[1]]
x+=1
else:
mas =[mas[1]] + mas[2:] + [mas[0]]
sila = mas[0]
x=1
if x==k:
break
return(sila)
print(sila_v_igroka(massiv,k)) | PYTHON3 |
879_B. 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. | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int main() {
unsigned long long int i, n, k;
cin >> n >> k;
deque<unsigned long long int> P;
unsigned long long int temp;
for (i = 0; i < n; i++) {
cin >> temp;
P.push_back(temp);
}
if (n == 2) {
cout << max(P[0], P[1]);
return 0;
}
unsigned long long int count = 0;
unsigned long long int one, two, win;
one = P.front();
P.pop_front();
two = P.front();
P.pop_front();
if (one > two) {
win = one;
P.push_back(two);
} else {
win = two;
P.push_back(one);
}
count++;
while (1) {
if (k - 1 > n - 1) {
P.push_front(win);
cout << *(max_element(P.begin(), P.end()));
return 0;
} else {
two = P.front();
P.pop_front();
if (win > two) {
count++;
P.push_back(two);
if (count == k) {
cout << win;
return 0;
}
} else {
P.push_back(win);
win = two;
count = 1;
}
}
}
return 0;
}
| CPP |
879_B. 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. | 2 | 8 | n,k = map(int,input().split())
a = [int(i) for i in input().split()]
d = a[0]
cnt = 0;
mx = d
for i in range(1,n) :
mx = max(mx,a[i])
if k >= n :
print (mx)
exit()
for i in range(1,n) :
if a[i] < d :
cnt += 1
else :
cnt = 1
d = a[i]
if cnt == k :
break
print (d)
| PYTHON3 |
879_B. 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. | 2 | 8 | n,k=map(int,raw_input().split())
a=map(int,raw_input().split())
if k>=n-1:
print n
else:
x=0
y=0
z=1
while x<=k:
if a[y]==n or x==k:
print a[y]
break
if a[y]>a[z] :
x+=1
else :
y=z
x=1
z+=1 | PYTHON |
879_B. 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. | 2 | 8 | from collections import deque
n, k = map(int, input().split())
a = deque(map(int, input().split()))
f = a.popleft()
s = a.popleft()
cnt = 0
while cnt < k:
if f > s:
cnt += 1
a.append(s)
s = a.popleft()
else:
cnt = 1
a.append(f)
f, s = s, a.popleft()
if f == n:
cnt = k
print(f)
| PYTHON3 |
879_B. 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. | 2 | 8 | inp=input(); inp=inp.split(); n=int(inp[0]); k=int(inp[1])
d=input(); d=d.split(); d=[ int(d[x]) for x in range(n) ]
tot=[ 0 for _ in range(505) ]
i=0
ans=d[0]
for i in range(1,n):
if ans>d[i]:
tot[ans]+=1
else:
ans,d[i]=d[i],ans
tot[ans]+=1
if tot[ans]==k:
print(ans)
exit(0)
print(ans)
| PYTHON3 |
879_B. 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. | 2 | 8 | from collections import *
Q = deque()
n, k = [int(x) for x in input().split()]
Q.extend([int(x) for x in input().split()])
cnt = 0
maximo = max(list(Q))
last_winner = -1
while True:
u = Q.popleft()
v = Q.popleft()
loser = min(u, v)
winner = max(u, v)
if winner == last_winner:
cnt += 1
else:
cnt = 1
last_winner = winner
if cnt == k: break
if winner == maximo: break
Q.append(loser)
Q.appendleft(winner)
print(last_winner)
| PYTHON3 |
879_B. 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. | 2 | 8 | import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Deque;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int[] arr = new int[n];
Deque<Integer> deque = new ArrayDeque<>();
long k = scanner.nextLong();
for (int i = 0; i < n; i++) {
arr[i] = scanner.nextInt();
deque.addLast(arr[i]);
}
Arrays.parallelSort(arr);
int max = arr[n - 1];
boolean check = true;
if (k >= n) {
System.out.println(max);
} else {
int buff = deque.poll();
long count = 0;
while (check || buff == max) {
if (buff > deque.peek()) {
count++;
deque.addLast(deque.poll());
} else {
deque.addLast(buff);
buff = deque.poll();
count = 1;
}
if (count >= k) {
System.out.println(buff);
return;
}
}
System.out.println(max);
}
}
} | JAVA |
879_B. 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. | 2 | 8 |
n, w = map(int, input().split())
arr = list(map(int,input().split()))
winner = arr[0]
consecutivesWins = 0
for i in range(1, n):
if(consecutivesWins == w):
break
elif (winner < arr[i] ):
winner = arr[i]
consecutivesWins = 1
else:
consecutivesWins += 1
print (winner)
| PYTHON3 |
879_B. 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. | 2 | 8 | n ,k = map(int , input().split())
arr = list(map(int,input().split()))
if k >= n:
print(max(arr))
else:
len = len(arr)
cnt = 0
prev_ans = -1
ans = 0
for idx in range(2000):
idx = idx + 1
mini = min(arr[idx],arr[idx - 1])
maxi = max(arr[idx],arr[idx - 1])
if arr[idx - 1] > arr[idx]:
arr[idx], arr[idx - 1] = arr[idx - 1], arr[idx]
ans = maxi
if prev_ans != ans:
cnt = 0
prev_ans = ans
arr.append(mini)
cnt += 1
if cnt >= k:
print(ans)
break | PYTHON3 |
879_B. 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. | 2 | 8 | entrada = list(map(int,input().split()))
jogo = list(map(int,input().split()))
i = jogo[0]
j = 1
temp = 0
vitorias = 0
cond = True
if(entrada[1] >= max(jogo)):
temp = max(jogo)
cond = False
jogo.append('X')
while(cond):
if(vitorias >= entrada[1] or jogo[j] == 'X'):
break
elif (i > jogo[j]):
vitorias += 1
j += 1
else:
i = jogo[j]
j += 1
vitorias = 1
temp = i
print(temp)
| PYTHON3 |
879_B. 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. | 2 | 8 | import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Scanner;
public class B879 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
long k = scan.nextLong();
Deque<Integer> dq = new ArrayDeque<Integer>();
int[] winCount = new int[n+1];
for (int i=0; i<n; i++) {
dq.addLast(scan.nextInt());
}
int ans = 0;
for (int i=0; ; i++) {
int first = dq.pollFirst();
int second = dq.pollFirst();
int max = Math.max(first, second);
int min = Math.min(first, second);
winCount[max] ++;
dq.addFirst(max);
dq.addLast(min);
if (k == (long)winCount[max] || n == max) {
ans = max;
break;
}
}
System.out.println(ans);
}
}
| JAVA |
879_B. 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. | 2 | 8 | import java.util.*;
import java.io.*;
import java.math.*;
public final class Solution
{
public static void main(String[] args)
{
Reader input = new Reader();
PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
int n = input.nextInt();
long k = input.nextLong();
ArrayList<Integer> arr = new ArrayList<>();
int max = Integer.MIN_VALUE;
for(int i = 0 ; i < n ; i++)
{
arr.add(input.nextInt());
if(arr.get(i) > max)
max = arr.get(i);
}
if(k >= n - 1)
out.println(max);
else
{
int current = arr.get(0);
int count = 0;
if(current == max)
{
out.println(current);
out.close();
return;
}
while(true)
{
while(true)
{
if(arr.get(1) > current)
{
if(count >= k)
{
out.println(current);
out.close();
return;
}
arr.add(arr.get(0));
arr.remove(0);
current = arr.get(0);
if(current == max)
{
out.println(current);
out.close();
return;
}
count = 1;
}
else
{
arr.add(arr.get(1));
arr.remove(1);
count++;
}
}
}
}
out.close();
}
public static int z(int n)
{
/*finds the number of trailing zeros in factorial */
int count = 0;
for(int i = 5 ; i <= n ; i *= 5)
{
count += n / i;
}
return count;
}
public static int gcd(int a , int b)
{
if(b == 0)
return a;
return gcd(b , a %b);
}
public static class UnionFind
{
/* this class uses zeroIndexing and gives the result back in zero indexing*/
int[] parent;
int[] size;
int maxSize = 0;
public UnionFind(int n)
{
size = new int[n];
parent = new int[n];
for(int i = 0 ; i < n ; i++)
{
parent[i] = i;
size[i] = 1;
}
maxSize = 1;
}
public int find(int pos)
{
if(parent[pos] == pos)
return pos;
return parent[pos] = find(parent[pos]);
}
public void unify(int x , int y)
{
int xParent = find(x);
int yParent = find(y);
if(xParent == yParent)
return;
else if(size[xParent] > size[yParent])
{
size[xParent] += size[yParent];
if(size[xParent] > maxSize)
maxSize = size[xParent];
parent[yParent] = xParent;
}
else
{
size[yParent] += size[xParent];
if(size[yParent] > maxSize)
maxSize = size[yParent];
parent[xParent] = yParent;
}
}
public int maxSize()
{
return maxSize;
}
}
public static class Pair implements Comparable<Pair>
{
int a , b;
public Pair(int a , int b)
{
this.a = a;
this.b = b;
}
@Override
public int compareTo(Pair t) {
// TODO Auto-generated method stub
if(t.a == this.a)
{
if(this.b < t.b)
return -1;
else if(this.b > t.b)
return 1;
else
return 0;
}
else if(this.a < t.a)
return -1;
else
return 1;
}
}
public static class Reader
{
BufferedReader br;
StringTokenizer st;
public Reader()
{
br = new BufferedReader(new InputStreamReader(System.in));
}
public String next()
{
try
{
if(st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
}
catch(IOException ex)
{
ex.printStackTrace();
System.exit(1);
}
return st.nextToken();
}
public int nextInt()
{
return Integer.parseInt(next());
}
public double nextDouble()
{
return Double.parseDouble(next());
}
public long nextLong()
{
return Long.parseLong(next());
}
public String nextLine()
{
try
{
return br.readLine();
}
catch(IOException ex)
{
ex.printStackTrace();
System.exit(1);
}
return "";
}
}
} | JAVA |
879_B. 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. | 2 | 8 | n, k = map(int, input().split(" "))
numbers = list(map(int, input().split(" ")))
best = max(numbers)
def f():
if k >= n:
print(best)
return
else:
current_best = numbers[0]
if numbers[0] == best:
print(best)
return
winners = 0
for i in numbers[1:]:
if i == best:
print(best)
return
elif i > current_best:
current_best = i
winners = 1
else:
winners += 1
if winners >= k:
print(current_best)
return
f() | PYTHON3 |
879_B. 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. | 2 | 8 | # -*- coding: utf-8 -*-
"""
Created on Mon Sep 16 09:32:34 2019
@author: avina
"""
n,k = map(int, input().split())
l = list(map(int, input().split()))
a = l[0]
win = 0
for i in range(1,n):
if a > l[i]:
win+=1
else:
a = l[i]
win = 1
if win == k:
break
print(a)
| PYTHON3 |
879_B. 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. | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int gcd(int a, int b) {
if (b == 0) return a;
return gcd(b, a % b);
}
bool isPrime(long long n) {
long long c;
if (n <= 1) return false;
c = sqrt(n);
for (int i = 2; i <= c; i++)
if (n % i == 0) return false;
return true;
}
const long long N = 1e6 + 7;
long long l, k, d, tr, n, m, sum, mini = 1e9 + 7, maxi, a, b, c, x, y;
long long t[N];
set<int> vis;
vector<int> v;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> k;
for (long long i = 0; i < n; i++) {
cin >> t[i];
}
if (n - 1 >= k) {
mini = t[0];
for (int i = 1; i < n; i++) {
if (t[i] > mini) {
t[n + a] = mini;
a++;
mini = t[i];
} else {
t[n + a] = t[i];
a++;
}
}
t[a + n] = N;
for (int i = 0; i < n; i++) {
b = 1;
while (t[i] > t[i + b]) {
b++;
if (i) {
if (b == k) {
cout << t[i];
i = n + a;
break;
}
} else {
if (b - 1 == k) {
cout << t[i];
i = n + a;
break;
}
}
}
}
} else {
sort(t, t + n);
mini = t[n - 1];
cout << mini;
}
}
| CPP |
879_B. 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. | 2 | 8 | #include <bits/stdc++.h>
int a[550], book[550];
int main() {
int n;
long long k;
while (~scanf("%d%lld", &n, &k)) {
memset(book, 0, sizeof(book));
int i, j, max = -1;
for (i = 0; i < n; i++) {
scanf("%d", &a[i]);
if (a[i] > max) {
max = a[i];
}
}
int head, end = 0, flag = 0;
for (i = 0; i < n; i++) {
head = end;
for (j = head + 1; j < n; j++) {
if (book[head] == k) {
printf("%d\n", a[head]);
flag = 1;
}
if (a[head] > a[j]) {
book[head]++;
} else if (a[head] < a[j]) {
book[j]++;
end = j;
break;
}
}
if (flag == 1) break;
}
if (!flag) printf("%d\n", max);
}
return 0;
}
| CPP |
879_B. 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. | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
ifstream fin("input.txt");
ofstream fout("output.txt");
inline void boostIO() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
int main() {
boostIO();
long long int n, k;
cin >> n >> k;
vector<long long int> A(n * 2);
for (int i = 0; i < (n); ++i) {
cin >> A[i];
}
long long int Spes = A[0];
long long int Index = n;
for (int i = 1; i < n; ++i) {
if (A[i] < Spes) {
A[Index] = A[i];
++Index;
} else {
A[Index] = Spes;
++Index;
Spes = A[i];
}
}
if (A[n * 2 - 1] == 0) A[n * 2 - 1] = Spes;
int Min = A[0];
for (int i = 0; i < (n); ++i) {
long long int Temp = 0;
if (A[i] > Min) ++Temp;
long long int j = i + 1;
while (A[j] < A[i]) {
++Temp;
++j;
}
if (A[j] == A[i]) {
cout << A[i];
return 0;
}
if (Temp >= k) {
cout << A[i];
return 0;
}
}
return 0;
}
| CPP |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.