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 java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n;
double k;
n = sc.nextInt();
k = sc.nextDouble();
sc.nextLine();
int tab[] = new int[n];
for (int i = 0; i < n; i++) {
tab[i] = sc.nextInt();
}
sc.nextLine();
int x = 0;
// row wiekszy niz liczba graczy
if (k >= n) {
for (int i = 0; i < n; i++) {
if (x < tab[i]) {
x = tab[i];
}
}
System.out.println(x);
return;
}
int j = 0;
int w = 0;
for(int i = 0; i<n-1; i++){
if(tab[j] > tab[i+1] ) {
w++;
if(w == k) {
x = tab[j];
break;
}
} else {
w = 1;
x = tab[i+1];
j = i + 1;
}
}
if (x != 0) {
System.out.println(x);
}
//Jesli nic nie znajdzie
if (x == 0) {
for (int i = 0; i < n; i++) {
if (x < tab[i]) {
x = tab[i];
}
}
System.out.println(x);
}
}
}
| 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())
powers = [int(i) for i in input().split()]
wins = {}
for i in powers:
wins[i]=0
i=0
while(True):
if powers[0] == max(powers):
print(powers[0])
break
if powers[0] > powers[1]:
wins[powers[0]]+=1
powers.append(powers[1])
powers.pop(1)
if wins[powers[0]] == k:
print(powers[0])
break
else:
wins[powers[1]]+=1
powers.append(powers[0])
powers.pop(0)
if wins[powers[1]] == k:
print(powers[1])
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 | #include <bits/stdc++.h>
using namespace std;
const int N = 510;
int b[N];
int main() {
int n, k;
cin >> n >> k;
int a[n];
for (int i = 0; i < n; i++) {
cin >> a[i];
}
if (k >= n) {
cout << n;
return 0;
}
int y = a[0];
for (int i = 1; i < n; i++) {
int p = a[i];
if (p > y) {
b[p]++;
y = p;
}
if (p < y) {
b[y]++;
}
for (int i = 0; i < N; i++) {
if (b[i] >= k) {
cout << i;
return 0;
}
}
}
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 | #include <bits/stdc++.h>
using namespace std;
long long k;
int main() {
int i, n, t;
vector<int> vi;
map<int, int> mp;
scanf("%d%lld", &n, &k);
for (i = 0; i < n; i++) {
scanf("%d", &t);
vi.push_back(t);
}
while (1) {
if (vi[0] > vi[1]) {
mp[vi[0]]++;
if (mp[vi[0]] >= n) {
cout << vi[0] << endl;
return 0;
}
if (mp[vi[0]] == k) {
cout << vi[0] << endl;
return 0;
}
t = vi[1];
vi.erase(vi.begin() + 1);
vi.push_back(t);
} else {
mp[vi[1]]++;
if (mp[vi[1]] >= n) {
cout << vi[1] << endl;
return 0;
}
if (mp[vi[0]] == k) {
cout << vi[0] << endl;
return 0;
}
t = vi[0];
vi.erase(vi.begin());
vi.push_back(t);
}
}
}
| 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 | inp = list(map(int, input().split()))
players = list(map(int, input().split()))
n = inp[0]
k = inp[1]
if (n == 2):
print(max(players[0], players[1]))
else:
flag = False
indice_prox = 2
maior = max(players)
num_vitorias = 0
prev_vencedor = max(players[0], players[1])
num_vitorias += 1
if(prev_vencedor == maior):
print(prev_vencedor)
else:
while(num_vitorias < k):
prox = players[indice_prox]
if(prox == maior):
flag = True
print(maior)
break
vencedor = max(prev_vencedor, prox)
if(vencedor == prev_vencedor):
num_vitorias += 1
else:
num_vitorias = 1
prev_vencedor = vencedor
indice_prox += 1
if(flag == False):
print(prev_vencedor) | 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.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n=sc.nextInt(),s=0,a=0,b=0,c=0,d=0,m;
long x=sc.nextLong();
if(x>500)s=500;
else s=(int)x;
while(n-->0){
m=sc.nextInt();
if(m>a){
c=a;
a=m;
if(c==0)b=0;
else b=1;
}else b++;
if(b==x){
c=a;
n=0;
}
}
if(a>c)c=a;
System.out.println(c);
}
} | 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.LinkedList;
import java.util.Scanner;
/**
* @author Jaseem
*/
public class TableTennis {
long k;
LinkedList<Integer> a;
int solve(){
if(a.size()==1) return a.getFirst();
int champion=a.poll();
int winCount=0;
while (winCount<k && winCount<a.size()){
int next=a.poll();
if(next>champion){
a.add(champion);
champion=next;
winCount=1;
} else{
a.add(next);
winCount++;
}
}
return champion;
}
public static void main(String[] args) {
TableTennis tableTennis=new TableTennis();
Scanner reader=new Scanner(System.in);
int n=reader.nextInt();
tableTennis.k=reader.nextLong();
tableTennis.a=new LinkedList<>();
for(int i=0;i<n;i++){
tableTennis.a.add(reader.nextInt());
}
System.out.println(tableTennis.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;
const long N = 10000 + 10;
long long n, k;
int a[N];
int main() {
cin >> n >> k;
int boss = 0;
for (int i = 1; i <= n; i++) {
cin >> a[i];
boss = max(a[i], boss);
}
int cur = a[1], curi = 1, curk = 0;
if (a[1] == boss) {
cout << boss;
return 0;
}
for (int i = 2; i <= n; i++) {
if (a[i] == boss) {
cout << a[i];
return 0;
}
if (cur > a[i]) {
++curk;
if (curk == k) {
cout << cur;
return 0;
}
} else {
cur = a[i];
curk = 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())
arr = list(map(int,input().split()))
count = 0
i = 1
t = arr[0]
while i<n:
if arr[0]>arr[1]:
arr.append(arr[1])
del arr[1]
count += 1
else:
arr.append(arr[0])
del arr[0]
count = 1
if count==k:
print(arr[0])
break
i += 1
if count!=k:
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 | n,k = map(int,input().split())
l = list(map(int,input().split()))
my_max = l[0]
count = 0
if(k>=n-1):
print(max(l))
else:
for i in l[1:]:
if(my_max == max(my_max,i)):
count += 1
else:
count = 1
my_max = max(my_max, i)
if (count == k):
exit(print(my_max))
print(my_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 | I = lambda : map(int, input().split())
n, k = I()
l = list(I())
if(n <= k):
print(max(l[:k+1]))
else:
victories = 1
if(l[0] > l[1]):
v = 0
else:
v = 1
for i in range(2, len(l)):
if(l[i] > l[v]):
v = i
victories = 1
else:
victories += 1
if(victories == k):
break
print(l[v]) | 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
import os
import random
import re
import sys
pw = input().split()
p = int(pw[0])
w = int(pw[1])
powers = list(map(int, input().rstrip().split()[:p]))
x = 0
checker = []
if w > 1000:
w = 1000
#checker.append(powers[0])
while len(checker) < w:
if powers[x] > powers[x+1]:
checker.append(powers[x+1])
powers.append(powers[x+1])
#powers.pop(powers[x+1])
del powers[x+1]
elif powers[x] < powers[x+1]:
powers.append(powers[x])
del powers[x]
checker.clear()
checker.append(powers[x+1])
print(powers[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 | n, k = map(int, input().split())
array = list(map(int, input().split()))
winner = array[0]
count = 0
for i in range(1, len(array)):
if array[i] > winner:
winner = array[i]
count = 1
elif array[i] < winner:
count += 1
if count == k:
break
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() {
long long a, b, now = 0;
long long ma = 0, t, ch;
queue<long long> Q;
scanf("%lld %lld", &a, &b);
for (int i = 0; i < a; i++) {
scanf("%lld", &t);
Q.push(t);
if (ma < t) ma = t;
}
ch = Q.front();
Q.pop();
for (int i = 0; i <= a + 5; i++) {
if (ch > Q.front()) {
now++;
Q.push(Q.front());
Q.pop();
} else {
now = 1;
Q.push(ch);
ch = Q.front();
Q.pop();
}
if (now == b || ch == ma) {
printf("%lld", ch);
return 0;
}
}
return -1;
}
| 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.*;
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.StringTokenizer;
public class Solution {
public static void main(String[] args){
MyScanner sc = new MyScanner();
out = new PrintWriter(new BufferedOutputStream(System.out));
//--------Solution---------------------------------------------//
int n = sc.nextInt();
long k = sc.nextLong();
ArrayDeque<Integer> q = new ArrayDeque<>(502);
for(int i=0; i<n; i++)
q.add(sc.nextInt());
int ans=0;
if(k>=n-1){
for(int p : q) ans = Math.max(p,ans);
}
else{
long c = 0;
while(c<k) {
int f = q.poll();
int s = q.poll();
if (f > s) {
c++;
ans = f;
q.addFirst(f);
q.add(s);
}
else {
c = 1;
q.addFirst(s);
q.add(f);
}
}
}
out.println(ans);
//-------------------------------------------------------------//
out.close();
}
//-----------PrintWriter for faster output-------------------------//
public static PrintWriter out;
//-----------MyScanner class for faster input----------------------//
public static class MyScanner {
BufferedReader br;
StringTokenizer st;
public MyScanner() {
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 | bruhMoment = input().split()
n = int(bruhMoment[0])
k = int(bruhMoment[-1])
bruhMomentX = input().split()
winCounter = 0
for i in range(1,n):
if int(bruhMomentX[0]) > int(bruhMomentX[i]):
winCounter += 1
else:
bruhMomentX[0], bruhMomentX[i] = bruhMomentX[i], bruhMomentX[0]
winCounter = 1
if winCounter == k:
break
print(bruhMomentX[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 | from collections import deque
from collections import defaultdict
n,k=list(map(int,input().split()))
a=list(map(int,input().split()))
maxo=max(a)
if a.index(maxo)==0:
print(maxo)
elif n==2:
print(maxo)
elif k>n:
print(maxo)
elif k<=n:
di=defaultdict(int)
ans=0
que=a
while True:
x=(a[0])
y=(a[1])
que.pop(0)
que.pop(0)
que.insert(0,max(x,y))
que.append(min(x,y))
di[max(x,y)]+=1
if di[max(x,y)]==k:
ans=max(x,y)
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 | n, k = map(int, raw_input().split())
powers = map(int, raw_input().split())
cont = 0
player = powers[0]
for i in range(1,len(powers)):
if cont == k:
break
if player > powers[i]:
cont += 1
else:
player = powers[i]
cont = 1
print player
| 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>
int main(void) {
long long int k;
int n, pow1, cont = 0, pow2;
scanf("%d %lli\n", &n, &k);
scanf("%d", &pow1);
for (int i = 1; i < n; i++) {
scanf("%d", &pow2);
if (pow1 < pow2) {
pow1 = pow2;
cont = 1;
} else
cont++;
if (cont >= k) break;
}
printf("%d", pow1);
}
| 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())
powers = list(map(int,input().split()))
win = 0
champ = powers[0]
i = 1
while(i < len(powers)):
if(win == k): break
prox = powers[i]
if(champ > prox):
win += 1
else:
champ = prox
win = 1
i+=1
print(champ)
| 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()]
a = [int(x) for x in input().split()]
it = iter(a)
curr = -1
cnt = k
x, y = next(it), next(it)
while cnt > 0:
nxt = max(x, y)
if nxt > curr:
cnt = k
curr = nxt
cnt -= 1
try:
x, y = curr, next(it)
except StopIteration:
break
print(curr) | 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 -*-
import sys
def f(n, k, fo):
c = 0
p = 0
w = 0
if k >= n-1:
p = max(fo)
else:
while c < k:
if fo[1] > fo[0]:
c = 1
p = fo[1]
w = 0
else:
c += 1
p = fo[0]
w = 1
fo.append(fo.pop(w))
print(p)
if __name__ == '__main__':
n, k = list(map(int, sys.stdin.readline().split()))
fo = list(map(int, sys.stdin.readline().split()))
f(n, k, fo)
# f(4, 2, [3, 1, 2 ,4])
# f(6, 2, [6, 5, 3, 1, 2, 4])
# f(2, 10000000000, [2, 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=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()
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 | import java.io.*;
import java.util.*;
import java.util.TreeSet;
import java.lang.*;
public class abc{
public static void main(String args[]){
Scanner in=new Scanner(System.in);
int i,n=in.nextInt();
String z=in.next();
int l=z.length();
int k;
if(l>3)
k=1000;
else
k=Integer.parseInt(z);
int maxw,max;
{
int v=in.nextInt();
maxw=v;
max=v;
}
int xl=k;
for(i=1;i<n;i++){
int v=in.nextInt();
if(k>0){
k--;
if(v>max){
max=v;
if(i==1);
else
k=xl-1;
}
}
if(v>maxw)
maxw=v;
}
if(n<=xl)
System.out.println(maxw);
else{
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 | #include <bits/stdc++.h>
using namespace std;
int main() {
long long n, k, maxx = -1, a, b, prev, curr, counter, i, x;
cin >> n >> k;
deque<int> dq;
for (i = 0; i < n; i++) {
cin >> x;
maxx = max(x, maxx);
dq.push_back(x);
}
if (k > n)
cout << maxx << endl;
else {
counter = 1;
a = dq.front();
dq.pop_front();
b = dq.front();
dq.pop_front();
prev = max(a, b);
dq.push_back(min(a, b));
dq.push_front(max(a, b));
while (counter != k) {
a = dq.front();
dq.pop_front();
b = dq.front();
dq.pop_front();
curr = max(a, b);
if (curr == prev)
counter++;
else
counter = 1;
dq.push_back(min(a, b));
dq.push_front(max(a, b));
prev = curr;
}
cout << curr << 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;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n;
long long k;
cin >> n >> k;
deque<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
if (k <= n) {
long long w = 0;
int c = a[0];
a.pop_front();
while (w != k) {
int d = a.front();
a.pop_front();
if (c > d) {
w++;
a.push_back(d);
} else {
swap(c, d);
w = 1;
a.push_back(d);
}
}
cout << c;
} else {
cout << *max_element(a.begin(), a.end());
}
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=list(map(int,input().split()))
per = A[0]
cons =0
ans =0
for j in range(1,n):
if (per > A[j]):
cons+=1
if cons == k:
print(per)
ans = 1
break
else:
cons = 1
per = A[j]
if(ans==0):
print(per) | 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 tabletennis{
public static void main(String[] args) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int people = Integer.parseInt(st.nextToken());
Long wins = Long.parseLong(st.nextToken());
StringTokenizer st2 = new StringTokenizer(br.readLine());
int counter = 0;
int max = Integer.parseInt(st2.nextToken());
for(int i = 1; i < people; i++){
int temp = Integer.parseInt(st2.nextToken());
if(temp > max){
max = temp;
counter = 1;
}
else counter++;
if(wins == counter)break;
}
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 | #include <bits/stdc++.h>
using namespace std;
int main() {
long long n, k;
cin >> n >> k;
int arr[n];
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
if (k >= n - 1) {
int ans = INT_MIN;
for (int i = 0; i < n; i++) {
ans = max(ans, arr[i]);
}
cout << ans << endl;
} else {
deque<int> q;
for (int i = 0; i < n; i++) {
q.push_back(arr[i]);
}
while (1) {
int y = q.front();
q.pop_front();
int z = q.front();
q.pop_front();
q.push_back(min(y, z));
int x = k - 1;
while (x > 0) {
if (q.front() < max(y, z)) {
q.push_back(q.front());
q.pop_front();
x--;
} else {
q.push_front(max(y, z));
break;
}
}
if (x == 0) {
cout << max(y, z) << 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 | p=input().rstrip().split(' ')
q=input().rstrip().split(' ')
if int(p[1])>=len(q):
q.sort(key=int,reverse=True)
print(int(q[0]))
else:
i=0;
K=0;
D=0;
while(1):
if K<int(p[1]) and D<int(p[1]):
if int(q[i]) > int(q[i+1]):
if D==0:
K+=1;
q.append(int(q[i+1]))
del(q[i+1])
else:
K=0;
D+=1;
q.append(int(q[i+1]))
del(q[i+1])
else:
if D!=0:
D=0;
K+=1;
q.append(q[i])
del(q[i])
else:
K=0;
q.append(int(q[i]))
del(q[i])
D+=1;
else:
break;
print(q[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 main() {
ios::sync_with_stdio(0);
cin.tie(0);
long long n, k;
cin >> n >> k;
queue<int> q;
for (int i = 0; i < n; i++) {
int x;
cin >> x;
q.push(x);
}
int tmp = q.front(), c = 0;
q.pop();
while (true) {
if (tmp == n) {
cout << n;
return 0;
}
if (tmp > q.front()) {
q.push(q.front());
q.pop();
c++;
} else {
tmp = q.front();
q.push(tmp);
q.pop();
c = 1;
}
if (c == k) {
cout << tmp;
return 0;
}
}
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 sys import stdin, stdout
n, k = map(int, stdin.readline().split())
values = list(map(int, stdin.readline().split()))
if k >= n - 1:
stdout.write(str(max(values)))
else:
for i in range(n):
if values[i] == max(values):
stdout.write(str(max(values)))
break
if not i and values[i] > max(values[i + 1: i + 1 + k]):
stdout.write(str(values[i]))
break
if i and values[i] > max(values[i + 1: i + k]):
stdout.write(str(values[i]))
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 | n, k = map(int, input().split())
a = list(map(int, input().split()))
if k >= n:
print(max(a))
else:
a = a + a
q = 0
pre = a[0]
ind = 0
for i in range(1, n * 2):
if pre > a[i]:
q += 1
if q >= k:
print(pre)
exit()
else:
q = 1
pre = a[i]
| 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
n, k = map(int, raw_input().split())
a = map(int, raw_input().split())
if (k > n):
print max(a)
else:
q = deque(a)
b = q.popleft()
w = 0
while True:
top = q.popleft()
if b > top:
q.append(top)
w += 1
else:
q.append(b)
b = top
w = 1
if w >= k:
print(b)
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.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
public class Main {
static BufferedReader br;
static StringTokenizer sc;
static PrintWriter out;
public static void main(String[] args) throws IOException {
//br = new BufferedReader(new FileReader("input.in"));
br = new BufferedReader(new InputStreamReader(System.in));
//out = new PrintWriter("output.out");
out = new PrintWriter(System.out);
sc = new StringTokenizer("");
TaskD solver = new TaskD();
solver.solve();
br.close();
out.close();
//System.out.println("Finished");
}
static class TaskD {
public void solve() throws IOException {
int n = nxtInt();
long k = nxtLng();
long wins = 0;
//int winner = 1;
int power = nxtInt();
for (int i = 1; i < n; i++) {
int a = nxtInt();
if (power > a) {
wins++;
if (wins == k) {
break;
}
} else {
power = a;
wins = 1;
}
}
out.println(power);
}
class Trie {
Node root;
Trie() {
root = new Node();
}
void add(long x) {
Node cur = root;
// int depth = 0;
while (x > 0) {
int ls = (int)(x % 10);
x /= 10;
cur.cnt[ls]++;
if (cur.nodes[ls] == null)
cur.nodes[ls] = new Node();
cur = cur.nodes[ls];
}
}
void sub(long x) {
Node cur = root;
while (x > 0) {
int ls = (int)(x % 10);
x /= 10;
cur.cnt[ls]--;
cur = cur.nodes[ls];
}
}
long[] getcnt(long x) {
int res = 0;
int prev = 0;
int depth = 0;
Node cur = root;
while (x > 0 && depth < 10) {
int ls = (int)(x % 10);
x /= 10;
res = cur.cnt[9 - ls];
if (res != 0) {
depth++;
prev = res;
cur = cur.nodes[9 - ls];
} else
break;
}
return new long[]{depth, prev};
}
}
class Node {
Node[] nodes;
int[] cnt;
Node() {
nodes = new Node[10];
cnt = new int[10];
}
}
}
static String nxtTok() throws IOException {
while (!sc.hasMoreTokens()) {
String s = br.readLine();
if (s == null) {
return null;
}
sc = new StringTokenizer(s.trim());
}
return sc.nextToken();
}
static int nxtInt() throws IOException {
return Integer.parseInt(nxtTok());
}
static long nxtLng() throws IOException {
return Long.parseLong(nxtTok());
}
static double nxtDbl() throws IOException {
return Double.parseDouble(nxtTok());
}
static int[] nxtIntArr(int n) throws IOException {
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = nxtInt();
}
return a;
}
static long[] nxtLngArr(int n) throws IOException {
long[] a = new long[n];
for (int i = 0; i < n; i++) {
a[i] = nxtLng();
}
return a;
}
static char[] nxtCharArr() throws IOException {
return nxtTok().toCharArray();
}
static int getMin(int arr[], int count)
{
int min = arr[0];
for (int i = 1; i < count; i++)
if (arr[i] < min)
min = arr[i];
return min;
}
static int getMax(int arr[], int count)
{
int max = arr[0];
for (int i = 1; i < count; i++)
if (arr[i] > max)
max = arr[i];
return max;
}
static void sortAsc(int arr[], int count)
{
int temp;
for (int i = 0; i < count - 1; i++)
{
for (int j = i + 1; j < count; j++)
{
if (arr[i] > arr[j])
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
}
static void sortDesc(int arr[], int count)
{
int temp;
for (int i = 0; i < count - 1; i++)
{
for (int j = i + 1; j < count; j++)
{
if (arr[i] < arr[j])
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
}
} | 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, defaultdict
power_map = defaultdict(lambda: 0)
n, k = map(int, input().split(' '))
ai = list(map(int, input().split(' ')))
if k > n:
print(max(ai))
else:
a = deque(ai)
win = 0
while True:
win = max(a[0], a[1])
loose = min(a[0], a[1])
a.remove(loose)
a.append(loose)
power_map[win] += 1
if max(power_map.values()) == k:
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 | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayDeque;
import java.util.StringTokenizer;
public class test{
public static class MyScanner {
BufferedReader br;
StringTokenizer st;
public MyScanner() {
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());
}
void nextArrayInt(int[] array,int n){
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) { }
for(int a=0;a<n;a++){
array[a] = Integer.parseInt(st.nextToken());
}
}
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;
}
}
private static void doSort(int[][] array,int start, int end) {
if (start >= end)
return;
int i = start, j = end;
int cur = i - (i - j) / 2;
while (i < j) {
while (i < cur && (array[i][0] <= array[cur][0])) {
i++;
}
while (j > cur && (array[cur][0] <= array[j][0])) {
j--;
}
if (i < j) {
int temp = array[i][0];
array[i][0] = array[j][0];
array[j][0] = temp;
temp = array[i][1];
array[i][1] = array[j][1];
array[j][1] = temp;
if (i == cur)
cur = j;
else if (j == cur)
cur = i;
}
}
doSort(array,start, cur);
doSort(array,cur+1, end);
}
static void reply(int[][] array){
int last = 0,temp=0;
boolean change = true;
while(change){
change = false;
for(int a=0;a<array.length;a++){
if(last==array[a][0]){
if(array[a][1]<array[a-1][1]){
temp = array[a-1][1];
array[a-1][1] = array[a][1];
array[a][1] = temp;
change = true;
}
}
last = array[a][0];
}
}
}
static void transf(int[][] array){
int last = 0;
for(int a=0;a<array.length;a++){
if(last==array[a][0]){
array[a][0] += array[a][1];
}
last = array[a][0];
}
}
public static void main(String[] args) {
MyScanner sc = new MyScanner();
int n = sc.nextInt();
long k = sc.nextLong();
/*int[] mas = new int[n];
sc.nextArrayInt(mas, n);*/
ArrayDeque<Integer> states = new ArrayDeque<Integer>();
for(int a=0;a<n;a++){
states.addLast(sc.nextInt());
}
//System.out.println(states);
if(k>n) k = n;
//int woner = 0;
int win = 0;
int temp = 0,temp2=0;
while(win!=k){
temp = states.poll();
temp2 = states.poll();
if(temp<temp2){
states.addFirst(temp2);
states.addLast(temp);
win = 1;
}else{
states.addFirst(temp);
states.addLast(temp2);
win++;
}
}
/*for(int a=1;a<n;a++){
temp = states.poll();
System.out.println(temp);
if(win==k) break;
}*/
System.out.println(states.getFirst());
}
} | 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()))
if k>=n:
print(max(a))
else:
if a[0]<a[1]:
a[0],a[1]=a[1],a[0]
m=0;pw=a[0];temp=0
while m!=k:
if a[1]<=pw:
temp=a[1];del a[1]
a.append(temp);m+=1
else:
m=1;pw=a[1]
temp=a[0];del a[0]
a.append(temp)
print(pw) | 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(pl, w):
i = 0
p1 = pl[0]
p2 = pl[1]
if p1 > p2:
r = pl.pop(1)
pl.append(r)
else:
p1 = p2
r = pl.pop(0)
pl.append(r)
i += 1
while i != w:
if p1 > pl[1]:
i += 1
pl.append(pl[1])
pl.pop(1)
else:
i = 1
pl.append(pl[0])
pl.pop(0)
p1 = pl[0]
if i > len(pl):
break
return p1
pw = input().split()
p = int(pw[0])
w = int(pw[1])
pl = list(map(int, input().rstrip().split()))
print (tabletennis(pl, w)) | 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 n;
long long k;
cin >> n >> k;
int *power = (int *)malloc(sizeof(int) * n);
for (int i = 0; i < n; i++) {
cin >> power[i];
}
int winner = max(power[0], power[1]);
int num_wins = 1;
for (int i = 2; i < n; i++) {
if (power[i] > winner) {
winner = power[i];
num_wins = 1;
} else {
num_wins += 1;
if (num_wins == k) {
break;
}
}
}
cout << winner << 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
def find_winner(powers_array, required_number_of_straight_wins):
if required_number_of_straight_wins > len(powers_array):
required_number_of_straight_wins = len(powers_array)
powers_queue = deque(powers_array)
number_of_straight_wins = 0
current_winning_power = powers_queue.popleft()
while number_of_straight_wins < required_number_of_straight_wins:
if current_winning_power < powers_queue[0]:
powers_queue.append(current_winning_power)
current_winning_power = powers_queue.popleft()
number_of_straight_wins = 1
else:
powers_queue.append(powers_queue.popleft())
number_of_straight_wins += 1
return current_winning_power
input_players_wins = input().split(" ")
number_of_players = int(input_players_wins[0])
required_number_of_straight_wins = int(input_players_wins[1])
powers_array = [int(power) for power in input().split(" ")]
print(find_winner(powers_array, required_number_of_straight_wins))
| 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()))
z=0
c=0
b=0
x=0
t=a[0]
m=0
while b+k < n:
if b==0:
b==1
if m==1:
u=k
else:
u=k+1
for i in range(b,b+u):
z=0
if t < a[i]:
t=a[i]
b=i
z=1
m=1
break
if z==0:
x=1
print(t)
break
for i in range(b,n):
t=max(t,a[i])
if x==0:
print(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 | def simulate(k, l):
#format per item: [power, location, no of wins]
l = [[l[i], i, 0] for i in range(len(l))]
curr = l.pop(0)
while True:
opp = l.pop(0)
if curr[0] > opp[0]:
l.append(opp)
curr[2] += 1
else:
l.append(curr)
curr = [i for i in opp]
curr[2] += 1
if curr[2] >= k:
return curr[0]
n, k = map(int, input().split())
l = list(map(int, input().split()))
if k <= n:
print(str(simulate(k, l)))
else:
l = [(l[i], i) for i in range(len(l))]
l.sort()
print(str(l[-1][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 | s = (input().split())
n = int(s[0])
k = int(s[1])
a = [int(i) for i in input().split()]
powerfull = a[0]
key = 0
if k > n : print(max(a))
else :
while (key<k) :
if(a[0] > a[1]) :
key+=1
for i in range(1,n-1):
a[i],a[i+1] = a[i+1],a[i]
if (a[0] < a[1] and k!=key):
for i in range(n-1):
a[i],a[i+1] = a[i+1],a[i]
key = 1
powerfull = a[0]
print(powerfull)
| 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.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class ProblemB {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
String[]sp = br.readLine().split(" ");
int n = Integer.parseInt(sp[0]);
long k = Long.parseLong(sp[1]);
int [] pow = new int[n];
sp = br.readLine().split(" ");
for(int i=0;i<n;i++)
{
pow[i] = Integer.parseInt(sp[i]);
}
if(n>k)
{
int start =0;
int end = (int)k;
int maxIndx = start;
while(end<n)
{
for(int i=start;i<=end;i++)
{
if(pow[i]>pow[maxIndx]){
maxIndx = i;
}
}
if(maxIndx==start || end == n-1)
{
System.out.println(pow[maxIndx]);
return;
}
start = maxIndx;
end = (int) Math.min(start+k-1, n-1);
}
}
else{
int max = 0;
for(int i=0;i<n;i++)
{
if(max<pow[i])
max = pow[i];
}
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 | import java.util.*;
import java.io.*;
import java.text.*;
public class t{
static final long mod = (long)1e8, root = 23;
static long IINF = (long)1e18+1;
static int MAX = (int)1e6+1, B = 10;
static FastReader in;
public static void main(String[] args) throws Exception{
in = new FastReader();
int n = ni();
long k = nl();
int[] a = ia(n);
int win = 0;
if(k>=n){
for(int i = 0; i< n; i++)win = Math.max(win, a[i]);
pn(win);
}else{
for(int i = 1, j = 0; i<n && j<k; i++, j++){
if(a[win] < a[i]){
win = i;
j = 0;
}
}
pn(a[win]);
}
}
static int[] ia(int n){
int[] out = new int[n];
for(int i = 0; i< n; i++)out[i] = ni();
return out;
}
static long gcd(long a, long b){
return (b==0)?a:gcd(b,a%b);
}
static void p(Object o){
System.out.print(o);
}
static void pn(Object o){
System.out.println(o);
}
static String n(){
return in.next();
}
static String nln(){
return in.nextLine();
}
static int ni(){
return Integer.parseInt(in.next());
}
static long nl(){
return Long.parseLong(in.next());
}
static double nd(){
return Double.parseDouble(in.next());
}
static class FastReader{
BufferedReader br;
StringTokenizer st;
public FastReader(){
br = new BufferedReader(new InputStreamReader(System.in));
}
public FastReader(String s) throws Exception{
br = new BufferedReader(new FileReader(s));
}
String next(){
while (st == null || !st.hasMoreElements()){
try{
st = new StringTokenizer(br.readLine());
}catch (IOException e){
e.printStackTrace();
}
}
return st.nextToken();
}
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 | n, k = map(int,raw_input().split())
ar = map(int, raw_input().split())
if(k >= n):
print max(ar)
else:
ar += ar
for l in xrange(n):
true = True
for j in xrange(l+1, k+l + 1):
if ar[l] < ar[j ]:
true = False
break
if l == 0:
k -=1
if true:
print ar[l]
exit()
| 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.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.BufferedWriter;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author Parthiv Mangukiya
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
OutputWriter out = new OutputWriter(outputStream);
TaskB solver = new TaskB();
solver.solve(1, in, out);
out.close();
}
static class TaskB {
public void solve(int testNumber, InputReader in, OutputWriter out) {
int n = in.nextInt();
long k = in.nextLong();
int max_till = in.nextInt();
long curC = 0;
for (int i = 1; i < n; i++) {
if (curC >= k) {
break;
}
int a = in.nextInt();
if (max_till > a) {
curC++;
} else {
curC = 1;
max_till = a;
}
}
out.println(max_till);
}
}
static class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void close() {
writer.close();
}
public void println(int i) {
writer.println(i);
}
}
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private InputReader.SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1) {
throw new InputMismatchException();
}
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0) {
return -1;
}
}
return buf[curChar++];
}
public int nextInt() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public long nextLong() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public boolean isSpaceChar(int c) {
if (filter != null) {
return filter.isSpaceChar(c);
}
return isWhitespace(c);
}
public static boolean isWhitespace(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
}
| 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;
long long a[1000], mx;
int main() {
long long n, k;
cin >> n >> k;
if (k >= n) {
cout << n;
return 0;
}
long long cnt = 0;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
if (cnt >= k) break;
if (mx > a[i])
cnt++;
else {
cnt = 0;
if (mx != 0) cnt++;
mx = a[i];
}
}
cout << endl << mx;
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.*;
public class TT {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
short n = sc.nextShort();
long k = sc.nextLong();
short a[] = new short[n - 1];
short x = 0;
int y = 0;
for(short i = 0; i < n; i++) {
if(i == 0) x = sc.nextShort();
else a[i - 1] = sc.nextShort();
}
if(x == n) {
System.out.println(x);
}
else {
while(y != k) {
short b = a[0];
if(x > b) {
short temp = b;
y++;
for(short i = 0; i < n - 1; i++) {
try {
a[i] = a[i + 1];
}
catch(Exception e) {
a[i] = b;
}
}
}
else {
short temp = x;
x = b;
y = 1;
for(short i = 0; i < n - 1; i++) {
try {
a[i] = a[i + 1];
}
catch(Exception e) {
a[i] = temp;
}
}
if(x == n) break;
}
}
System.out.println(x);
}
}
}
| 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
q=queue.Queue()
t1=input()
t1=t1.split(' ')
pNum=int(t1[0])
lNum=int(t1[1])
t2=input()
t2=t2.split(' ')
maxf=0
maxn=0
i=0
l=len(t2)
while i<l:
t2[i]=int(t2[i])
q.put(t2[i])
if(maxf<t2[i]):
maxf=t2[i]
maxn=i
i+=1
#print(str(maxf)+' '+str(maxn))
tadd=0
gq=0
p=0
tm=0
tm=q.get()
if lNum>pNum*2:
print(maxf)
else:
while True:
temp=q.get()
#print(temp)
if tm>temp:
q.put(temp)
tadd+=1
else:
if gq<tadd:
gq=tadd
q.put(tm)
tm=temp
tadd=1
if tadd==lNum:
break
if p>maxn+tadd:
break
print(tm)
| 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())
l=list(map(int,input().split()))
m=max(l)
count=0
p=l[0]
i=1
flag=0
if n==2:
if p>l[1]:
print(p)
else:
print(l[1])
else:
while(count!=k ):
if i==n:
p=m
flag=1
break
if(p>l[i]):
count=count+1
l.append(l[i])
else:
count=1
l.append(p)
p=l[i]
i=i+1
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;
int main() {
int n, p, p1, p2, flag, z;
long long k, c1 = 0, c2 = 0;
cin >> n >> k;
if (k > n)
z = n;
else
z = k;
list<int> powers;
for (int i = 0; i < n; ++i) {
cin >> p;
powers.push_back(p);
}
p1 = powers.front();
powers.pop_front();
p2 = powers.front();
powers.pop_front();
while (true) {
if (c1 == (z)) {
cout << p1 << "\n";
break;
}
if (c2 == (z)) {
cout << p2 << "\n";
break;
}
if (p1 > p2) {
c1 += 1;
c2 = 0;
powers.push_back(p2);
p2 = powers.front();
powers.pop_front();
} else {
c2 += 1;
c1 = 0;
powers.push_back(p1);
p1 = powers.front();
powers.pop_front();
}
}
}
| 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.*;
import java.util.*;
public class Main {
private static String s = "";
private static int max = 0;
public static void main(String[] args) throws IOException {
FastReader f = new FastReader();
int n = f.nextInt();
long k = f.nextLong();
int[] arr = new int[n];
for(int i=0;i<n;i++) {
arr[i] = f.nextInt();
}
int max = arr[0];
int cnt = 0;
for(int i=1;i<n;i++) {
if(max < arr[i]) {
max = arr[i];
cnt = 1;
} else {
cnt++;
}
if(cnt == k) {
System.out.println(max);
return;
}
}
System.out.println(n);
}
//fast input reader
static class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
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 |
def main():
n,k = map(int,input().split())
tabT = list(map(int,input().split()))
if k > n:
print(max(tabT))
else:
won = [0]*n
q = [x for x in range(n)]
while won[q[0]] < k:
t1 = tabT[q[0]]
t2 = tabT[q[1]]
if t1 > t2:
won[q[0]] += 1
q.append(q.pop(1))
else:
won[q[1]] += 1
q.append(q.pop(0))
print(tabT[q[0]])
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 | from collections import deque
playerCount, playerWins = [int(i) for i in input().strip().split()]
playerPower = [int(i) for i in input().strip().split()]
pP = deque()
breakLoop = False
for i in range(playerCount):
pP.append(playerPower[i])
pPdict = {i:0 for i in pP}
if playerCount == 1:
print(pP[0])
elif playerWins >= playerCount:
print(max(playerPower))
else:
winner = pP.popleft()
while True:
challenger = pP.popleft()
if winner > challenger:
pP.append(challenger)
pPdict[winner] += 1
else:
pP.append(winner)
winner = challenger
pPdict[challenger] += 1
for key, value in pPdict.items():
if value >= playerWins:
print(key)
breakLoop = True
if breakLoop:
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 | cin=lambda:list(map(int,input().split()))
n,k=cin()
A=cin()
C=[0 for i in range(n+1)]
while True:
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 | import java.io.*;
import java.util.*;
import java.util.regex.*;
public class vk18
{
public static void main(String[]stp) throws Exception
{
Scanner scan=new Scanner(System.in);
//BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//String []s;
int n=scan.nextInt(),i;
long k=scan.nextLong(),count=0;
int max=0;
int a[]=new int[n];
for(i=0;i<n;i++) a[i]=scan.nextInt();
max=Math.max(a[0],a[1]);
if(max==a[0]) { count=0; i=1; }
else { count=1; i=2; }
for(;i<n;i++)
{
if(a[i] < max) { count++; if(count == k) break;}
else {max=a[i]; count=1;}
}
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 | from collections import deque
n , k = map(int , input().split())
l = [int(i) for i in input().split()]
cont = 0 ; x = deque(l) ; j = l[0] ;
x.popleft()
if(k > n-1):
print(max(l))
exit()
while(cont != k):
if(j > x[0]):
cont+=1
r = x.popleft()
x.append(r)
else:
cont = 1
x.append(j)
j = x.popleft()
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 = list(map(int,input().split()))
p = list(map(int,input().split()))
tested = p[0]
wins = 0
for i in range(1, n):
if p[i] < tested:
wins+=1
else:
wins=1
tested = p[i]
if wins == k:
break
print(tested)
| 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 | nk = input().split()
n = int(nk[0])
k = int(nk[1])
powers = input().split()
wins = {}
visit = {}
winner = 0
maxPower = 0
for i in range(len(powers)):
wins[int(powers[i])] = 0
visit[int(powers[i])] = False
winnerNode = int(powers.pop(0))
visit[winnerNode] = True
while(True):
p1 = int(powers.pop(0))
if(visit[p1] == False):
visit[p1] = True
else:
break
if(p1 > winnerNode):
wins[p1] = wins[p1] + 1
powers.append(winnerNode)
winnerNode = p1
if(wins[p1] == k):
winner = p1
break
else:
wins[winnerNode] = wins[winnerNode] + 1
powers.append(p1)
maxPower = max(maxPower, wins[winnerNode])
if(wins[winnerNode] == k):
winner = winnerNode
break
winner = winnerNode
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 | linea1 = [int(x) for x in input().split()]
lista = [int(x) for x in input().split()]
total = linea1[0]
minimo = linea1[1]
#maximo = 0
ganador = 0
contador = 1
if lista[0]>lista[1]:
#maximo = lista[0]
#c = lista[1]
ganador = lista[0]
lista.remove(lista[1])
#lista.append(c)
else:
c = lista[0]
ganador = lista[1]
lista.remove(lista[0])
#lista.append(c)
while (contador < minimo) and (len(lista) != 1):
if lista[0]>lista[1]:
contador += 1
#c = lista[1]
lista.remove(lista[1])
#lista.append(c)
else:
contador = 1
c = lista[0]
ganador = lista[1]
lista.remove(lista[0])
#lista.append(c)
print(ganador)
| 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 Main {
static Scanner in = new Scanner(System.in);
void solve() {
int n = in.nextInt();
long k = in.nextLong();
Deque<P> q = new ArrayDeque<>();
for(int i = 0; i < n; i++) {
q.addLast(new P(in.nextLong()));
}
P p1 = q.pop(), p2;
while(true) {
if(p1.s == n) {
System.out.println(n);
return;
}
p2 = q.pop();
if (p1.s > p2.s) {
p1.w++;
if(p1.w == k) {
System.out.println(p1.s);
return;
}
q.addLast(p2);
}else {
p2.w++;
if(p2.w == k) {
System.out.println(p2.s);
return;
}
q.addLast(p1);
p1 = p2;
}
}
}
public static void main(String[] args) {
new Main().solve();
}
}
class P{
long s, w;
public P(long s) {
this.s = s;
w = 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 | //package org.anurag.ds;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;
public class Problem_879_B {
public static void main(String[] args) throws IOException {
Map<Integer, Long> map = new HashMap<Integer, Long>();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] strArr = br.readLine().split(" ");
int n = Integer.parseInt(strArr[0]);
long k = Long.parseLong(strArr[1]);
int[] a = new int[n];
StringTokenizer st = new StringTokenizer(br.readLine());
for(int i=0; i<n;i++){
a[i] = Integer.parseInt(st.nextToken());
}
while(true){
if(a[0] >a[1]){
inspect(0, a, k, map, 0);
}
if(map.containsKey(a[0])){
System.out.println(a[0]);
break;
}
if(a[0] < a[1]){
moveElemToLast(a,0);
inspect(0, a, k, map, 1);
}
if(map.containsKey(a[0])){
System.out.println(a[0]);
break;
}
}
}
private static void inspect(int i, int[] a,long k, Map<Integer, Long> map,long count) {
if(isIMax(a,i)){
map.put(a[i], count);
}
else{
for(int j=i+1; j<a.length && count < k;j++){
if(a[i]>a[j]){
count++;
moveElemToLast(a,j);
j--;
}else{
break;
}
}
if(count == k){
map.put(a[i], count);
}
}
}
private static boolean isIMax(int[] a, int i) {
for(int j=0; j<a.length ; j++){
if(a[i] < a[j]){
return false;
}
}
return true;
}
private static int[] moveElemToLast(int[] a, int j) {
int temp = a[j];
for(int i=j;i<a.length-1; i++){
a[i]=a[i+1];
}
a[a.length-1]=temp;
return a;
}
}
| 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 | first = input().split()
second = input().split()
intFirst = []
for x in first:
intFirst.append(int(x))
intSecond = []
for y in second:
intSecond.append(int(y))
numberPlayers = intFirst[0]
wins = intFirst[1]
counter = 0
winner = 0
realWinner = 0
if numberPlayers < wins:
print(max(intSecond))
else:
while counter < wins:
realWinner = 0
if intSecond[0] > intSecond[1]:
realWinner = intSecond[0]
intSecond.append(intSecond[1])
intSecond.pop(1)
else:
realWinner = intSecond[1]
intSecond.append(intSecond[0])
intSecond.pop(0)
if realWinner == winner:
counter = counter + 1
else:
winner = realWinner
counter = 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())
l=list(map(int,input().split()))
if(n<=k):
l.sort()
l.reverse()
print(l[0])
else:
t=0
while(t<k):
if(l[0]>l[1]):
x=l[1]
l.remove(l[1])
l.append(x)
t+=1
elif(l[1]>l[0]):
x=l[0]
l.remove(l[0])
l.append(x)
t=1
#print(t)
print(l[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 | line1 = [int(i) for i in input().split()]
n = line1[0]
k = line1[1]
players = input().split()
mapa = {}
maior = 0
result = 0
while(maior < k):
greater = players[0]
minor = players[1]
if(int(players[1]) > int(greater)):
greater = players[1]
minor = players[0]
if(len(players) == 2):
result = greater
break
if(k > 2 * len(players)):
result = max([int(j) for j in players])
break
if(mapa.get(greater) == None):
mapa[greater] = 1
else:
mapa[greater] += 1
if(mapa[greater] > maior):
maior = mapa[greater]
result = greater
players.remove(minor)
players.append(minor)
print(result) | 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 B {
static StringBuilder st = new StringBuilder();
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
PrintWriter out = new PrintWriter(System.out);
int n = sc.nextInt();
long k = sc.nextLong();
Queue<Integer> q = new LinkedList<>();
int max = 0;
for (int i = 0; i < n; i++) {
int x = sc.nextInt() ;
max = Math.max(max, x) ;
q.add(x) ;
}
int curr = q.poll() ;
int cnt = 0 ;
while(!q.isEmpty())
{
int second = q.poll() ;
if(curr < second)
{
cnt = 1 ;
int temp = curr ;
curr = second ;
q.add(temp) ;
}
else
{
cnt ++ ;
q.add(second);
}
if(curr == max)
{
System.out.println(curr);
return ;
}
if(cnt == k)
{
System.out.println(curr);
return ;
}
}
out.flush();
out.close();
}
static class Scanner {
BufferedReader br;
StringTokenizer st;
Scanner(InputStream in) {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() throws Exception {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
int nextInt() throws Exception {
return Integer.parseInt(next());
}
long nextLong() throws Exception {
return Long.parseLong(next());
}
double nextDouble() throws Exception {
return Double.parseDouble(next());
}
}
static void shuffle(int[] a) {
int n = a.length;
for (int i = 0; i < n; i++) {
int r = i + (int) (Math.random() * (n - i));
int tmp = a[i];
a[i] = a[r];
a[r] = tmp;
}
}
} | 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())
l=list(map(int,input().split()))
class Queue:
def __init__(self):
self.items=[]
def enqueue(self,item):
self.items.append(item)
def insrt(self,item):
self.items.insert(0,item)
def dequeue(self):
return self.items.pop(0)
def is_empty(self):
if self.items==[]:
return True
return False
def check(self,i):
if l[i]==max(l):
return True
return False
s=0
save=-1
q=Queue()
for i in l:
q.enqueue(i)
if k<=n:
while s<k:
a=q.dequeue()
b=q.dequeue()
if a>b:
q.insrt(a)
q.enqueue(b)
if save==a:
s+=1
else:
save=a
s=1
else:
q.insrt(b)
q.enqueue(a)
if save==b:
s+=1
else:
save=b
s=1
else:
for i in range(n):
check=q.check(i)
if check==1:
save=l[i]
break
print(save)
| 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())
v = list(map(int, input().split()))
occ = 0
pre = v[0]
for i in range(1, n):
if pre>v[i]:
occ+=1
else:
pre = v[i]
occ = 1
if occ==k:
break
print(pre) | 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 | mod = 1000000007
ii = lambda : int(input())
si = lambda : input()
dgl = lambda : list(map(int, input()))
f = lambda : map(int, input().split())
il = lambda : list(map(int, input().split()))
ls = lambda : list(input())
n,k=f()
l=il()
wn=-1
im=l.index(max(l))
if k>=im:
print(max(l))
else:
mx=max(l[0],l[1])
c=1
for i in range(2,im+1):
if mx>l[i]:
c+=1
else:
mx=l[i]
c=1
if c==k:
break
print(mx) | 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 Queue
nk = raw_input().split()
n = int(nk[0])
k = int(nk[1])
lis = raw_input().split()
# # stack = []
# q = Queue.Queue()
# for i in range(0,n):
# # stack.insert(0,int(lis[i]))
# q.put(int(lis[i]))
# p1 = q.get()
# p2 = q.get()
# winner = 0
# if p1>p2:
# winner = p1
# q.put(p2)
# else:
# winner = p2
# q.put(p1)
# ct = 1
# while ct!=k:
# newp = q.get()
# if winner > newp:
# ct+=1
# q.put(newp)
# else:
# ct = 1
# q.put(winner)
# winner = newp
# print winner
for i in range(0,n):
lis[i] = int(lis[i])
if k>n:
lis.sort()
print lis[-1]
else:
p1 = lis[0]
p2 = lis[1]
winner = 0
if p1>p2:
winner = p1
else:
winner = p2
ct = 1
i = 2 % n
while(ct != k):
if winner > lis[i]:
ct+=1
else:
winner = lis[i]
ct = 1
i = (i+1) % n
print winner | 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 | #!/usr/bin/env python3
from collections import deque
def simu(k,A):
M = max(A)
v = 0
while A[0]<M and v<k:
if A[0]>A[1]:
v += 1
A.append(A[1])
A[1] = A[0]
else:
v = 1
A.append(A[0])
A.popleft()
return A[0]
def main():
n,k = map(int,input().split())
A = deque(map(int,input().split()))
print(simu(k,A))
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 |
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.util.LinkedList;
import java.util.Queue;
//package acm_practice;
/**
*
* @author ghost
*/
public class Round_441 {
public static void main(String args[]) {
//File file= new File("COMPLETE_FILE_PATH");
//InputStream is = new FileInputStream(file);
//InputReader in = new InputReader(is);
InputReader in = new InputReader(System.in);
PrintWriter out = new PrintWriter(System.out);
int t = 1;
while (t-- > 0) {
Task solver = new Task();
solver.solve(t, in, out);
}
out.flush();
out.close();
}
static class Task {
public void solve(int testNumber, InputReader in, PrintWriter out) {
int n= in.nextInt();
long k= in.nextLong();
if(k>n) k=n;
ArrayDeque<Integer> q= new ArrayDeque<>();
for(int i=0;i<n;i++) q.add(in.nextInt());
int count=0;
int winner_index=-1;
while(count<k){
int first=q.remove();
int second=q.remove();
if(first>second){
q.addFirst(first);
q.addLast(second);
if(winner_index==first){
count++;
}
else if(winner_index<0 || winner_index!=first){
winner_index=first;
count=1;
}
}
else{
q.addFirst(second);
q.addLast(first);
if(winner_index==second){
count++;
}
else if(winner_index<0 || winner_index!=second){
winner_index=second;
count=1;
}
}
}
out.println(winner_index);
}
int LOG2(long item) {
int count = 0;
while (item > 1) {
item >>= 1;
count++;
}
return count;
}
long gcd(long a, long b) {
a = Math.abs(a);
b = Math.abs(b);
return BigInteger.valueOf(a).gcd(BigInteger.valueOf(b)).longValue();
}
}
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[8192];
private int curChar, snumChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int snext() {
if (snumChars == -1) {
throw new InputMismatchException();
}
if (curChar >= snumChars) {
curChar = 0;
try {
snumChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (snumChars <= 0) {
return -1;
}
}
return buf[curChar++];
}
public int nextInt() {
int c = snext();
while (isSpaceChar(c)) {
c = snext();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = snext();
}
int res = 0;
do {
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = snext();
} while (!isSpaceChar(c));
return res * sgn;
}
public long nextLong() {
int c = snext();
while (isSpaceChar(c)) {
c = snext();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = snext();
}
long res = 0;
do {
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = snext();
} while (!isSpaceChar(c));
return res * sgn;
}
public int[] nextIntArray(int n) {
int a[] = new int[n];
for (int i = 0; i < n; i++) {
a[i] = nextInt();
}
return a;
}
public String readString() {
int c = snext();
while (isSpaceChar(c)) {
c = snext();
}
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = snext();
} while (!isSpaceChar(c));
return res.toString();
}
public String next() {
return readString();
}
public boolean isSpaceChar(int c) {
if (filter != null) {
return filter.isSpaceChar(c);
}
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
private static void pa(Object... o) {
System.out.println(Arrays.deepToString(o));
}
}
}
| 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;
using ll = long long;
int main() {
ios::sync_with_stdio(0), cin.tie(0);
int n;
ll k;
cin >> n >> k;
vector<int> a(n);
for (int &i : a) cin >> i;
int c = 0;
while (a[0] != n && c < k)
if (a[0] > a[1]) {
int d = a[1];
a.erase(begin(a) + 1);
a.push_back(d);
++c;
} else {
int d = a[0];
a.erase(begin(a));
a.push_back(d);
c = 1;
}
cout << a[0];
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 Queue
q = Queue.Queue()
n,k = map(int,raw_input().split())
a = map(int,raw_input().split())
if (k>n):
print max(a)
else:
for i in a:
q.put(i)
tam = q.qsize()
i = a[0]
w = 0
q.get()
#cont = 0
#while cont < tam:
while True:
top = q.get()
if i > top:
s = q.put(top)
w += 1
else:
s = q.put(i)
w = 1
i = top
if w >= k:
print i
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 | n, k = map(int, input().split())
a = list(map(int, input().split()))
if n == 2:
print(max(a))
elif n - 1 <= k:
print(max(a))
else:
f = a.pop(0)
s = a.pop(0)
if f > s:
curr = f
a.append(s)
else:
curr = s
a.append(f)
f = s
row = 1
while row != k:
s = a.pop(0)
p = max(curr, s)
if p == curr:
row += 1
a.append(s)
else:
a.append(curr)
curr = s
row = 1
print(curr)
| 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.*;
//Mann Shah [ DAIICT ].
public class Main {
public static void main(String[] Args) {
Scanner in = new Scanner(System.in);
int mod = 1000000007;
int n = in.nextInt();
long k = in.nextLong();
int[] a = new int[n];
int max=0;
for(int i=0;i<n;i++) {
a[i]=in.nextInt();
if(a[i]>max) {
max=a[i];
}
}
if(k>n) {
System.out.println(max);
}
else {
int max2=a[0];
int c=0;
int f=0;
for(int i=1;i<n;i++)
{
if(a[i]>max2) {
c=1;
max2=a[i];
}
else {
c++;
}
if(c==k) {
System.out.println(max2);
f=1;
break;
}
}
if(f==0) {
System.out.println(max);
}
}
in.close();
}
}
| 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.lang.*;
import java.math.*;
import java.io.*;
/* N1k5 De5@1 */
public class codeforces implements Runnable {
static class InputReader
{
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream)
{
this.stream = stream;
}
public int read()
{
if (numChars==-1)
throw new InputMismatchException();
if (curChar >= numChars)
{
curChar = 0;
try
{
numChars = stream.read(buf);
}
catch (IOException e)
{
throw new InputMismatchException();
}
if(numChars <= 0)
return -1;
}
return buf[curChar++];
}
public String nextLine()
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
public int nextInt()
{
int c = read();
while(isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-')
{
sgn = -1;
c = read();
}
int res = 0;
do
{
if(c<'0'||c>'9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
while (!isSpaceChar(c));
return res * sgn;
}
public long nextLong()
{
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-')
{
sgn = -1;
c = read();
}
long res = 0;
do
{
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
while (!isSpaceChar(c));
return res * sgn;
}
public double nextDouble()
{
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-')
{
sgn = -1;
c = read();
}
double res = 0;
while (!isSpaceChar(c) && c != '.')
{
if (c == 'e' || c == 'E')
return res * Math.pow(10, nextInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
if (c == '.')
{
c = read();
double m = 1;
while (!isSpaceChar(c))
{
if (c == 'e' || c == 'E')
return res * Math.pow(10, nextInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
m /= 10;
res += (c - '0') * m;
c = read();
}
}
return res * sgn;
}
public String readString()
{
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do
{
res.appendCodePoint(c);
c = read();
}
while (!isSpaceChar(c));
return res.toString();
}
public boolean isSpaceChar(int c)
{
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public String next()
{
return readString();
}
public interface SpaceCharFilter
{
public boolean isSpaceChar(int ch);
}
}
public void run() {
InputReader s = new InputReader(System.in);
PrintWriter w = new PrintWriter(System.out);
int t = 1;
while(t-- > 0) {
int n = s.nextInt();
long k = s.nextLong();
int[] a = new int[n];
long max=0,p = k;
for(int i=0;i<n;i++){
a[i] = s.nextInt();
if(a[i]>max) max = a[i];
}
if(k>=n) w.println(max);
else{
int l = 1;
long count = 0;
int maxpow = a[0];
while(count<k){
if(a[l]<maxpow){
l++; l%=n; count++;
}
else{
count = 1;
maxpow = a[l];
l++; l%=n;
}
}
w.println(maxpow);
}
}
w.close();
}
public static void main(String args[]) throws Exception
{
new Thread(null, new codeforces(),"codeforces",1<<26).start();
}
} | 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;
template <typename T>
using V = vector<T>;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int64_t N, K;
cin >> N >> K;
V<int> A(N);
for (auto &e : A) {
cin >> e;
};
int m = *max_element(begin(A), end(A));
queue<int> Q;
for (int i = 1; i < N; ++i) Q.push(i);
int curr = 0;
int64_t k = K;
while (k && A[curr] != m) {
int n = Q.front();
Q.pop();
if (A[n] > A[curr]) {
k = K - 1;
Q.push(curr);
curr = n;
} else {
--k;
Q.push(n);
}
}
cout << A[curr] << 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;
void priyanshu() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
int main() {
priyanshu();
long long n, k;
cin >> n >> k;
long long a[n], maxm = 0;
for (long long i = 0; i < n; i++) {
cin >> a[i];
maxm = max(maxm, a[i]);
}
long long j = 0, flag = 0;
if (a[0] < a[1]) flag = 1;
if (n <= 2)
cout << max(a[0], a[1]);
else {
for (long long i = 1; i < n; i++) {
if (a[j] > a[i])
flag++;
else {
j = i;
flag = 1;
}
if (flag == k) {
cout << a[j] << endl;
break;
}
}
if (flag != k) cout << maxm << 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.*;
import java.lang.*;
import java.io.*;
public class Solution {
public static void main(String[] args) {
InputReader fi = new InputReader(System.in);
int n,m,i,j;
n=fi.nextInt();
long k=fi.nextLong();
long wins,p1,p2;
LinkedList<Long> al=new LinkedList<>();
long max=0;
for (i=0;i<n;i++){
al.addLast(fi.nextLong());
max=Math.max(max,al.get(i));
}
i=0;
wins=0;
while (true){
while (wins<k ){
p1=al.get(i%n);
p2=al.get((i+1)%n);
if (p1==max){
System.out.println(p1);
return;
}
else if (p1 > p2){
al.remove(p2);
al.addLast(p2);
wins++;
}
else {
al.remove(p1);
al.addLast(p1);
wins=1;
}
}
if (wins==k){
System.out.println(al.get(i));
return;
}
}
}
static class Node implements Comparable<Node>{
int day;
int doc;
Node(int day ,int doc){
this.day=day;
this.doc=doc;
}
public int compareTo(Node x){
if (this.doc!=x.doc)
return Integer.compare(this.doc,x.doc);
else
return Integer.compare(this.day,x.day);
}
}
}
class InputReader {
private InputStream stream;
private byte[] buf = new byte[8192];
private int curChar, snumChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int snext() {
if (snumChars == -1)
throw new InputMismatchException();
if (curChar >= snumChars) {
curChar = 0;
try {
snumChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (snumChars <= 0)
return -1;
}
return buf[curChar++];
}
public int nextInt() {
int c = snext();
while (isSpaceChar(c))
c = snext();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = snext();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = snext();
} while (!isSpaceChar(c));
return res * sgn;
}
public long nextLong() {
int c = snext();
while (isSpaceChar(c))
c = snext();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = snext();
}
long res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = snext();
} while (!isSpaceChar(c));
return res * sgn;
}
public int[] nextIntArray(int n) {
int a[] = new int[n+1];
for (int i = 1; i <= n; i++)
a[i] = nextInt();
return a;
}
public String readString() {
int c = snext();
while (isSpaceChar(c))
c = snext();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = snext();
} while (!isSpaceChar(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
| 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.*;
public class TestClass {
public static void main(String args[] ) throws Exception {
Scanner s1=new Scanner(System.in);
int n=s1.nextInt();
long k=s1.nextLong();
if(n==2)
{
int n1=s1.nextInt();
int m=s1.nextInt();
if(n1>m)
System.out.println(n1);
else
System.out.println(m);
return;
}
/*int a[]=new int[n];
int b[]=new int[n+1];
int max=0,max_freq=0;*/
if(k>n)
{
int a[]=new int[n],max=0;
for(int i=0;i<n;i++)
{
a[i]=s1.nextInt();
if(max<a[i])
max=a[i];
}
System.out.println(max);
}
/* if(k<=n)
{
int i=0,j=1,index=0;
while(max_freq<=k && j<n)
{
System.out.println("i "+i+" j "+j);
if(a[i]>a[j])
{
b[a[i]]++;
j++;
}
else
{
b[a[j]]++;
i=j;
j++;
}
for(int l=0;l<=n;l++)
{
if(max_freq<=b[l])
{
max_freq=b[l];
index=l;
}
}
System.out.println("max_freq "+max_freq +" index: "+index);
}
/*for(int l=0;l<=n;l++)
System.out.println(l+" "+b[l]);
System.out.println(index);
}
else
{
System.out.println(max);
}*/
else
{
LinkedList<Integer> al=new LinkedList<Integer>();
HashMap<Integer,Long> mp=new HashMap<Integer,Long>();
for(int i=0;i<n;i++)
al.add(s1.nextInt());
int index=0;
int a[]=new int[n+1];
while(true)
{
int i=al.get(0);
int j=al.get(1);
int indexy=0;
int target=i;
int winner=j;
if(j<i)
{
indexy=1;
target=j;
winner=i;
}
al.remove(indexy);
al.add(target);
if(mp.containsKey(new Integer(winner)))
{
long score=mp.get(winner);
mp.put(winner,++score);
}
else
mp.put(winner,1l);
//a[winner]++;
long max_freq=0;
index=0;
for (Integer key : mp.keySet())
{
Long value = mp.get(key);
if(max_freq<=value && index<key)
{
max_freq=value;
index=key;
}
}
/*for(int in=1;in<=n;in++)
{
if(max_freq<=a[in] && a[in]!=0)
{
max_freq=a[in];
index=in;
}
} */
if(max_freq==k)
break;
}
System.out.println(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 = map(int, input().split())
a = [int(x) for x in input().split()]
p = a[0]
c = 0
for i in range(1, n):
if p == n:
print(p)
break
if p > a[i]:
c += 1
if c == k:
print(p)
break
else:
p = a[i]
c = 1
if p == n:
print(p)
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 | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
long long k;
cin >> n >> k;
deque<int> Q;
for (int i = 0; i < n; i++) {
int power;
cin >> power;
Q.push_back(power);
}
int absoluteWinner = 0;
int lastWinner = 0;
int winsInARow;
for (int i = 0; i < n - 2; i++) {
int A = Q.front();
Q.pop_front();
int B = Q.front();
Q.pop_front();
int winner = max(A, B);
int loser = min(A, B);
if (winner == lastWinner) {
winsInARow++;
} else {
winsInARow = 1;
}
lastWinner = winner;
Q.push_back(loser);
Q.push_front(winner);
if (winsInARow == k) {
absoluteWinner = winner;
break;
}
}
if (absoluteWinner > 0) {
cout << absoluteWinner << endl;
} else {
cout << n << 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 | a,b=map(int,input().split())
l=list(map(int,input().split()))
s=0
pre=l[0]
for i in range(1,a):
if pre>l[i]:
s+=1
else:
pre=l[i]
s=1
if s==b:
break
print(pre)
| 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 | a,b=map(int,input().split())
z=list(map(int,input().split()))
r=z.index(max(z));j=0
for i in range(r):
s=0
if i<j:continue
if i!=0:
if z[i-1]<z[i]:s+=1
for j in range(i+1,r):
if z[i]>z[j]:s+=1
else:break
if s>=b:exit(print(z[i]))
print(max(z)) | 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())
po = list(map(int,input().split()))
if k>=len(po):
print(max(po))
else:
wins=0
q=po[:]
while(wins!=k):
cp = q.pop(0)
while True:
i = q[0]
if i<cp:
wins+=1
q.append(q.pop(0))
else:
wins=1
q.append(cp)
break
if wins==k:
break
print(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 | def solve():
from collections import deque
n, k = map(int, raw_input().split())
arr = map(int, raw_input().split())
mmax = max(arr)
arr = deque((i, 0) for i in arr)
while True:
p1 = arr.popleft()
p2 = arr.popleft()
if p1[0] == mmax:
return mmax
if p1[1] == k:
return p1[0]
if p1[0] > p2[0]:
arr.appendleft((p1[0], p1[1] + 1))
arr.append((p2[0], 0))
else:
arr.appendleft((p2[0], p2[1] + 1))
arr.append((p1[0], 0))
print solve()
| 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 | n,k= map(int,input().split())
a= list(map(int,input().split()))
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 | nk = input().split()
n = int(nk[0])
k = int(nk[1])
a = list(map(int, input().rstrip().split()))
index = k
count = 0
while index>0:
if a[0]>a[1]:
a.append(a[1])
a.pop(1)
else:
a.append(a[0])
a.pop(0)
index = k
index-=1
count +=1
if count==(n-1):
break
print(a[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 | # python3
# utf-8
players_nr, max_win_streak = (int(x) for x in input().split())
player_idx___power = [int(x) for x in input().split()]
if max_win_streak > players_nr:
print(max(player_idx___power))
quit()
curr_win_streak = 0
curr_power = player_idx___power[0]
next_player_idx = 1
while curr_win_streak < max_win_streak:
next_power = player_idx___power[next_player_idx]
if curr_power < next_power:
curr_power = next_power
player_idx___power.append(curr_power)
curr_win_streak = 1
else:
player_idx___power.append(next_power)
curr_win_streak += 1
next_player_idx += 1
print(curr_power)
| 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;
char c1[100005];
vector<int> v;
string s = "";
long long res = 0;
int main() {
unsigned long long n, k, mx, i(0), d;
cin >> n >> k;
n--;
cin >> d;
mx = d;
while (n--) {
cin >> d;
if (mx > d) {
i++;
if (i == k) {
cout << mx;
return 0;
}
} else {
i = 1;
mx = d;
}
}
cout << mx;
}
| 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.*;
import java.util.*;
import java.text.*;
import java.lang.*;
import java.math.BigInteger;
import java.util.regex.*;
public class Myclass {
public static void main(String[] args)
{
InputReader in = new InputReader(System.in);
PrintWriter pw = new PrintWriter(System.out);
int n=in.nextInt();
long k=in.nextLong();
long power[]=new long[n];
long ans=0;;
for(int i=0;i<n;i++)
{
power[i]=in.nextLong();
}
long a[]=power.clone();
Arrays.sort(a);
int count=0;
if(k>=n)
pw.println(a[n-1]);
else
{
ans=0;
for(int i=1;i<n;i++)
{
if(power[i]>power[(int) ans])
{
ans=i;
count=1;
}
else
{
count++;
}
//pw.println(ans+" "+count);
if(count==k)
break;
}
pw.println(power[(int) ans]);
}
pw.flush();
pw.close();
}
static class InputReader
{
private final InputStream stream;
private final byte[] buf = new byte[8192];
private int curChar, snumChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream)
{
this.stream = stream;
}
public int snext()
{
if (snumChars == -1)
throw new InputMismatchException();
if (curChar >= snumChars)
{
curChar = 0;
try
{
snumChars = stream.read(buf);
}
catch (IOException e)
{
throw new InputMismatchException();
}
if (snumChars <= 0)
return -1;
}
return buf[curChar++];
}
public int nextInt()
{
int c = snext();
while (isSpaceChar(c))
{
c = snext();
}
int sgn = 1;
if (c == '-')
{
sgn = -1;
c = snext();
}
int res = 0;
do
{
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = snext();
} while (!isSpaceChar(c));
return res * sgn;
}
public long nextLong()
{
int c = snext();
while (isSpaceChar(c))
{
c = snext();
}
int sgn = 1;
if (c == '-')
{
sgn = -1;
c = snext();
}
long res = 0;
do
{
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = snext();
} while (!isSpaceChar(c));
return res * sgn;
}
public int[] nextIntArray(int n)
{
int a[] = new int[n];
for (int i = 0; i < n; i++)
{
a[i] = nextInt();
}
return a;
}
public String readString()
{
int c = snext();
while (isSpaceChar(c))
{
c = snext();
}
StringBuilder res = new StringBuilder();
do
{
res.appendCodePoint(c);
c = snext();
} while (!isSpaceChar(c));
return res.toString();
}
public String nextLine()
{
int c = snext();
while (isSpaceChar(c))
c = snext();
StringBuilder res = new StringBuilder();
do
{
res.appendCodePoint(c);
c = snext();
} while (!isEndOfLine(c));
return res.toString();
}
public boolean isSpaceChar(int c)
{
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private boolean isEndOfLine(int c)
{
return c == '\n' || c == '\r' || c == -1;
}
public interface SpaceCharFilter
{
public boolean isSpaceChar(int ch);
}
}
public static long mod = 1000000007;
public static int d;
public static int p;
public static int q;
public static int[] suffle(int[] a,Random gen)
{
int n = a.length;
for(int i=0;i<n;i++)
{
int ind = gen.nextInt(n-i)+i;
int temp = a[ind];
a[ind] = a[i];
a[i] = temp;
}
return a;
}
public static void swap(int a, int b){
int temp = a;
a = b;
b = temp;
}
public static HashSet<Integer> primeFactorization(int n)
{
HashSet<Integer> a =new HashSet<Integer>();
for(int i=2;i*i<=n;i++)
{
while(n%i==0)
{
a.add(i);
n/=i;
}
}
if(n!=1)
a.add(n);
return a;
}
public static void sieve(boolean[] isPrime,int n)
{
for(int i=1;i<n;i++)
isPrime[i] = true;
isPrime[0] = false;
isPrime[1] = false;
for(int i=2;i*i<n;i++)
{
if(isPrime[i] == true)
{
for(int j=(2*i);j<n;j+=i)
isPrime[j] = false;
}
}
}
public static int GCD(int a,int b)
{
if(b==0)
return a;
else
return GCD(b,a%b);
}
public static long GCD(long a,long b)
{
if(b==0)
return a;
else
return GCD(b,a%b);
}
public static void extendedEuclid(int A,int B)
{
if(B==0)
{
d = A;
p = 1 ;
q = 0;
}
else
{
extendedEuclid(B, A%B);
int temp = p;
p = q;
q = temp - (A/B)*q;
}
}
public static long LCM(long a,long b)
{
return (a*b)/GCD(a,b);
}
public static int LCM(int a,int b)
{
return (a*b)/GCD(a,b);
}
public static int binaryExponentiation(int x,int n)
{
int result=1;
while(n>0)
{
if(n % 2 ==1)
result=result * x;
x=x*x;
n=n/2;
}
return result;
}
public static long binaryExponentiation(long x,long n)
{
long result=1;
while(n>0)
{
if(n % 2 ==1)
result=result * x;
x=x*x;
n=n/2;
}
return result;
}
public static int modularExponentiation(int x,int n,int M)
{
int result=1;
while(n>0)
{
if(n % 2 ==1)
result=(result * x)%M;
x=(x*x)%M;
n=n/2;
}
return result;
}
public static long modularExponentiation(long x,long n,long M)
{
long result=1;
while(n>0)
{
if(n % 2 ==1)
result=(result * x)%M;
x=(x*x)%M;
n=n/2;
}
return result;
}
public static long modInverse(int A,int M)
{
return modularExponentiation(A,M-2,M);
}
public static long modInverse(long A,long M)
{
return modularExponentiation(A,M-2,M);
}
public static boolean isPrime(int n)
{
if (n <= 1) return false;
if (n <= 3) return true;
if (n%2 == 0 || n%3 == 0)
return false;
for (int i=5; i*i<=n; i=i+6)
{
if (n%i == 0 || n%(i+2) == 0)
return false;
}
return true;
}
static class pair implements Comparable<pair>
{
Long x, y;
pair(long x,long y)
{
this.x=x;
this.y=y;
}
public int compareTo(pair o) {
int result = x.compareTo(o.x);
if(result==0)
result = y.compareTo(o.y);
return result;
}
public String toString()
{
return x+" "+y;
}
public boolean equals(Object o)
{
if (o instanceof pair)
{
pair p = (pair)o;
return p.x == x && p.y == y ;
}
return false;
}
public int hashCode()
{
return new Long(x).hashCode()*31 + new Long(y).hashCode();
}
}
}
| 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.*;
public class Main {
public static void main(String args[]){
Scanner reader=new Scanner(System.in);
int a=reader.nextInt();
long b=reader.nextLong();
ArrayList<Integer> l=new ArrayList<Integer>();
int max=0;
for(int n=0;n<a;n++){
int num=reader.nextInt();
if(num>max){
max=num;
}
l.add(num);
}
int k=0;
int p=l.get(0);
int st=1;
int end=a;
if(b>=a-1){
System.out.println(max);
}
else{
while(k<b){
int pp=l.get(st);
if(p>pp){
k++;
l.add(end, pp);
}
else{
l.add(end, p);
p=pp;
k=1;
}
st++;
end++;
}
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 |
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class TryB
{
static class InputReader {
private final InputStream stream;
private final byte[] buf = new byte[8192];
private int curChar, snumChars;
public InputReader(InputStream st) {
this.stream = st;
}
public int read() {
if (snumChars == -1)
throw new InputMismatchException();
if (curChar >= snumChars) {
curChar = 0;
try {
snumChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (snumChars <= 0)
return -1;
}
return buf[curChar++];
}
public int ni() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public long nl() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public int[] nia(int n) {
int a[] = new int[n];
for (int i = 0; i < n; i++) {
a[i] = ni();
}
return a;
}
public String rs() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public String nextLine() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isEndOfLine(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private boolean isEndOfLine(int c) {
return c == '\n' || c == '\r' || c == -1;
}
}
static long mod=1000000007;
static BigInteger bigInteger = new BigInteger("1000000007");
static int n = (int)1e6;
static boolean[] prime;
static ArrayList<Integer> as;
static void sieve() {
Arrays.fill(prime , true);
prime[0] = prime[1] = false;
for(int i = 2 ; i * i <= n ; ++i) {
if(prime[i]) {
for(int k = i * i; k< n ; k+=i) {
prime[k] = false;
}
}
}
}
static PrintWriter w = new PrintWriter(System.out);
public static void main(String[] args)
{
InputReader sc = new InputReader(System.in);
//PrintWriter w = new PrintWriter(System.out);
/*
prime = new boolean[n + 1];
sieve();
prime[1] = false;
*/
/*
as = new ArrayList<>();
for(int i=2;i<=1000000;i++)
{
if(prime[i])
as.add(i);
}
*/
/*
long a = sc.nl();
BigInteger ans = new BigInteger("1");
for (long i = 1; i < Math.sqrt(a); i++) {
if (a % i == 0) {
if (a / i == i) {
ans = ans.multiply(BigInteger.valueOf(phi(i)));
} else {
ans = ans.multiply(BigInteger.valueOf(phi(i)));
ans = ans.multiply(BigInteger.valueOf(phi(a / i)));
}
}
}
w.println(ans.mod(bigInteger));
*/
int x = sc.ni();
long k = sc.nl();
int []a = sc.nia(x);
int []arr = new int[x+1];
Queue<Integer> q = new LinkedList<>();
int res = 0;
for(int i=2;i<=x;i++)
{
q.add(i);
}
int temp = 1;
if(k < (long)x)
{
while(true)
{
if(a[temp-1] > a[q.peek()-1])
{
q.add(q.poll());
arr[temp]++;
if(arr[temp] == (int)k)
{
res = a[temp-1];
break;
}
}
else
{
q.add(temp);
temp = q.poll();
arr[temp]++;
if(arr[temp] == k)
{
res = a[temp-1];
break;
}
}
}
w.println(res);
}
else
{
int max = 0;
for(int i=0;i<x;i++)
{
if(max < a[i])
max = a[i];
}
w.println(max);
}
w.close();
}
static int opens(String p)
{
int c = 0;
for(int i=0;i<p.length();i++)
{
if(p.charAt(i) == '(')
{
c++;
}
else
{
c--;
if(c < 0)
return -1;
}
}
return c;
}
static int closes(String p)
{
int c = 0;
for(int i=p.length()-1;i>=0;i--)
{
if(p.charAt(i) == ')')
{
c++;
}
else
{
c--;
if(c < 0)
return -1;
}
}
return c;
}
static long modexp(long x,long n,long M)
{
long power = n;
long result=1;
while(power>0)
{
if(power % 2 ==1)
result=(result * x)%M;
x=(x*x)%M;
power = power/2;
}
return result;
}
static long modInverse(long A,long M)
{
return modexp(A,M-2,M);
}
public static long modMultiply(long one, long two) {
return (one % mod * two % mod) % mod;
}
static long fx(int m)
{
long re = 0;
for(int i=1;i<=m;i++)
{
re += (long) (i / gcd(i,m));
}
return re;
}
static long gcd(long a, long b)
{
if (a == 0)
return b;
return gcd(b%a, a);
}
static long phi(long nx)
{
// Initialize result as n
double result = nx;
// Consider all prime factors of n and for
// every prime factor p, multiply result
// with (1 - 1/p)
for (int p = 0; as.get(p) * as.get(p) <= nx; ++p)
{
// Check if p is a prime factor.
if (nx % as.get(p) == 0)
{
// If yes, then update n and result
while (nx % as.get(p) == 0)
nx /= as.get(p);
result *= (1.0 - (1.0 / (double) as.get(p)));
}
}
// If n has a prime factor greater than sqrt(n)
// (There can be at-most one such prime factor)
if (nx > 1)
result *= (1.0 - (1.0 / (double) nx));
return (long)result;
//return(phi((long)result,k-1));
}
public static int primeFactors(int n)
{
int sum = 0;
// Print the number of 2s that divide n
while (n%2==0)
{
sum = 1;
//System.out.print(2 + " ");
n /= 2;
}
// n must be odd at this point. So we can
// skip one element (Note i = i +2)
for (int i = 3; i <= Math.sqrt(n); i+= 2)
{
// While i divides n, print i and divide n
while (n%i == 0)
{
// System.out.print(i + " ");
n /= i;
}
sum++;
}
// This condition is to handle the case whien
// n is a prime number greater than 2
if (n > 2)
sum++;
return sum;
}
static int digitsum(int x)
{
int sum = 0;
while(x > 0)
{
int temp = x % 10;
sum += temp;
x /= 10;
}
return sum;
}
static int countDivisors(int n)
{
int cnt = 0;
for (int i = 1; i*i <=n; i++)
{
if (n % i == 0 && i<=1000000)
{
// If divisors are equal,
// count only one
if (n / i == i)
cnt++;
else // Otherwise count both
cnt = cnt + 2;
}
}
return cnt;
}
static boolean isprime(int n)
{
if(n == 2)
return true;
if(n == 3)
return true;
if(n % 2 == 0)
return false;
if(n % 3 == 0)
return false;
int i = 5;
int w = 2;
while(i * i <= n)
{
if(n % i == 0)
return false;
i += w;
w = 6 - w;
}
return true;
}
static long log2(long value) {
return Long.SIZE-Long.numberOfLeadingZeros(value);
}
static boolean binarysearch(int []arr,int p,int n)
{
//ArrayList<Integer> as = new ArrayList<>();
//as.addAll(0,at);
//Collections.sort(as);
boolean re = false;
int st = 0;
int end = n-1;
while(st <= end)
{
int mid = st + (end-st)/2;
if(p > arr[mid])
{
st = mid+1;
}
else if(p < arr[mid])
{
end = mid-1;
}
else if(p == arr[mid])
{
re = true;
break;
}
}
return re;
}
static class Student
{
int position;
int value;
Student(int position,int value)
{
this.position = position;
this.value = value;
}
}
/* Java program for Merge Sort */
static class MergeSort
{
// Merges two subarrays of arr[].
// First subarray is arr[l..m]
// Second subarray is arr[m+1..r]
void merge(int arr[], int l, int m, int r)
{
// Find sizes of two subarrays to be merged
int n1 = m - l + 1;
int n2 = r - m;
/* Create temp arrays */
int L[] = new int [n1];
int R[] = new int [n2];
/*Copy data to temp arrays*/
for (int i=0; i<n1; ++i)
L[i] = arr[l + i];
for (int j=0; j<n2; ++j)
R[j] = arr[m + 1+ j];
/* Merge the temp arrays */
// Initial indexes of first and second subarrays
int i = 0, j = 0;
// Initial index of merged subarry array
int k = l;
while (i < n1 && j < n2)
{
if (L[i] <= R[j])
{
arr[k] = L[i];
i++;
}
else
{
arr[k] = R[j];
j++;
}
k++;
}
/* Copy remaining elements of L[] if any */
while (i < n1)
{
arr[k] = L[i];
i++;
k++;
}
/* Copy remaining elements of R[] if any */
while (j < n2)
{
arr[k] = R[j];
j++;
k++;
}
}
// Main function that sorts arr[l..r] using
// merge()
void sort(int arr[], int l, int r)
{
if (l < r)
{
// Find the middle point
int m = (l+r)/2;
// Sort first and second halves
sort(arr, l, m);
sort(arr , m+1, r);
// Merge the sorted halves
merge(arr, l, m, r);
}
}
/* A utility function to print array of size n */
}
public static int ip(String s){
return Integer.parseInt(s);
}
public static String ips(int s){
return Integer.toString(s);
}
} | 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.*;
public class Main {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
long k = sc.nextLong();
ArrayList<Integer> arr = new ArrayList<>();
for (int i = 0; i < n; i++) {
arr.add(sc.nextInt());
}
int win = 0;
if (n < k) {
Collections.sort(arr);
Collections.reverse(arr);
} else {
while (win != k) {
if (arr.get(0) > arr.get(1)) {
win++;
arr.add(arr.get(1));
arr.remove(1);
} else if (arr.get(0) < arr.get(1)) {
win = 1;
arr.add(arr.get(0));
arr.remove(0);
}
}
}
System.out.println(arr.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 | import java.util.Scanner;
public class TableTennis {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
long k = sc.nextLong();
int arr[] = new int[n];
int max = 0;
int c = 0;
int ans = 0;
arr[0] = sc.nextInt();
max = arr[0];
for(int i=1;i<n;i++){
arr[i] = sc.nextInt();
if(arr[i]>max){
c = 1;
max = arr[i];
}
else
c++;
if(c == k && ans == 0)
ans = max;
}
if(ans == 0)
ans = max;
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 | from collections import deque
def ping_pong(wins, players):
p = deque(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.popleft()
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(' ')]
ping_pong(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 | import java.io.*;
import java.util.*;
public class Main {
static BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
static BufferedWriter output = new BufferedWriter(new OutputStreamWriter(System.out));
static StringBuilder ans = new StringBuilder();
static String line;
static int getMax(int a[], int i, int j) {
int max = -1;
int maxId = 0;
for (int k = i; k < j; k++) {
if (a[k] > max) {
max = a[k];
maxId = k;
}
}
return maxId;
}
static void solve() throws IOException {
// input = new BufferedReader(new FileReader("/home/noberel/Downloads/Software Construction/problems/src/main/java/input"));
// output = new BufferedWriter((new FileWriter("/home/noberel/Downloads/Software Construction/problems/src/main/java/output")));
StringTokenizer stringTokenizer = new StringTokenizer(input.readLine());
int n = Integer.parseInt(stringTokenizer.nextToken());
long k = Long.parseLong(stringTokenizer.nextToken());
int a[] = new int[n];
stringTokenizer = new StringTokenizer(input.readLine());
for (int i = 0; i < n; i++) {
a[i] = Integer.parseInt(stringTokenizer.nextToken());
}
if (k >= n - 1) {
ans.append(n).append('\n');
} else {
int maxId = getMax(a, 0, (int) k);
int currMax = a[maxId];
while (true) {
if (maxId == 0) maxId = getMax(a, maxId, Math.min(maxId + (int)k + 1, n));
else maxId = getMax(a, maxId, Math.min(maxId + (int)k, n));
if (currMax == a[maxId]) break;
currMax = a[maxId];
}
ans.append(currMax).append('\n');
}
output.write(ans.toString());
output.flush();
output.close();
}
public static void main(String[] args) throws IOException {
new Main().solve();
}
}
| JAVA |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.