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.ArrayDeque;
import java.util.Queue;
import java.util.Scanner;
public class codeforces {
public static void main(String[] args) {
TaskB Solver = new TaskB();
Solver.Solve();
}
public static class TaskB {
public void Solve() {
Scanner in = new Scanner(System.in);
long n = in.nextLong();
long k = in.nextLong();
Queue <Integer> Q = new ArrayDeque ();
for (int i = 0; i < n; i++)
Q.add(in.nextInt());
if (k >= n) {
System.out.println(n);
return;
}
int p1 = Q.poll();
int p2, len = 0;
while (true) {
p2 = Q.poll();
if (p1 > p2) {
Q.add(p2);
len++;
}
else {
Q.add(p1);
p1 = p2;
len = 1;
}
if (len == k)
break;
}
System.out.println(p1);
}
}
} | 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 = input().split()
jogadores = input().split()
jogadores = [int(x) for x in jogadores]
if int(k) > int(n):
vencedor = max(jogadores)
else:
vitorias = 0
while vitorias < int(k) :
vencedor = jogadores[0]
if vencedor > jogadores[1]:
jogadores.append(jogadores.pop(1))
vitorias += 1
else:
jogadores.append(jogadores.pop(0))
vitorias = 1
print (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.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class JavaStructures {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
Queue<Integer> queue = new LinkedList<>();
String[] nk = br.readLine().split(" ");
int n = Integer.parseInt(nk[0]);
long k = Long.parseLong(nk[1]);
String[] players = br.readLine().split(" ");
int first = Integer.parseInt(players[0]);
int second = Integer.parseInt(players[1]);
int max = 0;
for(int i = 2; i < n; i++) {
int ele = Integer.parseInt(players[i]);
queue.add(ele);
max = Math.max(max, ele);
}
if(k > n - 2) {
max = Math.max(max, first);
max = Math.max(max, second);
System.out.println(max);
}
else {
int count = 0;
while (count < k) {
if (first > second) {
queue.add(second);
second = queue.remove();
count++;
} else {
queue.add(first);
first = second;
second = queue.remove();
count = 1;
}
}
System.out.println(first);
}
}
}
| JAVA |
879_B. Table Tennis | n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner.
For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner.
Input
The first line contains two integers: n and k (2 β€ n β€ 500, 2 β€ k β€ 1012) β the number of people and the number of wins.
The second line contains n integers a1, a2, ..., an (1 β€ ai β€ n) β powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct.
Output
Output a single integer β power of the winner.
Examples
Input
2 2
1 2
Output
2
Input
4 2
3 1 2 4
Output
3
Input
6 2
6 5 3 1 2 4
Output
6
Input
2 10000000000
2 1
Output
2
Note
Games in the second sample:
3 plays with 1. 3 wins. 1 goes to the end of the line.
3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner. | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
struct temp {
int inp, vicCount;
};
int main() {
long long n, k;
cin >> n >> k;
struct temp ss[n + 2];
int maxn = 0;
deque<temp> dq;
for (int i = 0; i < n; i++) {
cin >> ss[i].inp;
ss[i].vicCount = 0;
dq.push_back(ss[i]);
maxn = max(maxn, ss[i].inp);
}
if (k >= n - 1)
cout << maxn << endl;
else {
int i = 1;
while (1) {
struct temp cur = dq.front();
if (cur.vicCount == k) {
cout << cur.inp << endl;
break;
}
struct temp first, second;
first = dq.front();
dq.pop_front();
second = dq.front();
dq.pop_front();
if (first.inp < second.inp) {
second.vicCount++;
dq.push_back(first);
dq.push_front(second);
} else {
first.vicCount++;
dq.push_back(second);
dq.push_front(first);
}
}
}
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 | #http://codeforces.com/problemset/problem/879/B
inp = list(map(int,input().split() ))
games = inp[1]
players = list(map(int,input().split()))
best= players[0]
victory = 0
if(games>=len(players)-1):
print(max(players))
else:
for x in players[1:]:
if x < best:
victory += 1
else:
best = x
victory = 1
if victory == games: break
print(best) | 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 | num_wins = int(input().split(" ")[1])
num_players = input().split(" ")
sub = int(num_players[0])
num_players.pop(0)
wins = 0
check = int(num_players[0])
while wins < num_wins:
main = int(num_players[0])
if sub > main:
wins+=1
if wins == num_wins:
print(sub)
break
num_players.pop(0)
num_players.append(main)
if num_players[0] == check:
print(sub)
break
else:
wins = 1
loser = sub
sub = main
check = loser
num_players.pop(0)
num_players.append(loser) | 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;
long long n, k, m, shu[6000];
int main() {
scanf("%lld%lld", &n, &k);
long long imax = 0;
queue<long long> q;
for (long long i = 1; i <= n; i++) {
scanf("%lld", &shu[i]);
q.push(shu[i]);
imax = max(imax, shu[i]);
}
if (n <= k) {
printf("%d\n", imax);
} else {
long long winer = q.front();
q.pop();
long long summ = 0;
while (summ < k) {
long long bettle = q.front();
q.pop();
if (winer >= bettle) {
summ = summ + 1;
q.push(bettle);
} else {
summ = 1;
q.push(winer);
winer = bettle;
}
}
printf("%lld\n", winer);
}
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 Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
long k = sc.nextLong();
int[] P = new int[n];
long cnt = 0;
for(int i=0;i<n;i++){
P[i] = sc.nextInt();
}
int max = P[0];
for(int i=1;i<n;i++){
if(P[i] > max){
if(cnt >= k){
break;
}else{
max = P[i];
cnt = 1;
}
}else{
cnt++;
}
}
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.io.*;
import java.util.*;
import java.math.*;
import java.lang.*;
import static java.lang.Math.*;
public class P1 implements Runnable
{
static class InputReader
{
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
private BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
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()
{
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);
}
}
// if modulo is required set value accordingly
public static long[][] matrixMultiply2dL(long t[][],long m[][])
{
long res[][]= new long[t.length][m[0].length];
for(int i=0;i<t.length;i++)
{
for(int j=0;j<m[0].length;j++)
{
res[i][j]=0;
for(int k=0;k<t[0].length;k++)
{
res[i][j]+=t[i][k]+m[k][j];
}
}
}
return res;
}
static long combination(long n,long r)
{
long ans=1;
for(long i=0;i<r;i++)
{
ans=(ans*(n-i))/(i+1);
}
return ans;
}
public static void main(String args[]) throws Exception
{
new Thread(null, new P1(),"P1",1<<27).start();
}
// **just change the name of class from Main to reuquired**
public void run()
{
InputReader sc = new InputReader(System.in);
PrintWriter w = new PrintWriter(System.out);
HashMap<Integer,Long> hm=new HashMap<Integer,Long>();
LinkedList<Integer> l=new LinkedList<Integer>();
int n=sc.nextInt();
long k=sc.nextLong();
boolean flag=false;
int temp=0,temp2=0,max=0;
long freq=0;
for (int i=0;i<n;++i) {
temp=sc.nextInt();
if (temp>max) {
max=temp;
}
hm.put(temp,freq);
l.add(temp);
}
for(int i=0;i<n-1;++i){
temp=l.removeFirst();
temp2=l.removeFirst();
if (temp>temp2) {
l.addFirst(temp);
l.addLast(temp2);
freq=hm.get(temp)+1;
if (freq==k) {
System.out.println(temp);
flag=true;
break;
}
hm.put(temp,freq);
}
else{
l.addFirst(temp2);
l.addLast(temp);
freq=hm.get(temp2)+1;
if (freq==k) {
flag=true;
System.out.println(temp2);
break;
}
hm.put(temp2,freq);
}
}
if (!flag) {
System.out.println(max);
}
System.out.flush();
w.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 | g, f = map(int, input().split())
mas = list(map(int, input().split()))
k = max(mas)
q = 0
n = mas[0]
for i in range(g):
if mas[i] == k:
print(mas[i])
break
if mas[i] > n:
n = mas[i]
q = 1
else:
if i != 0:
q += 1
if q == f:
print(n)
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.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
public class B {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] l = br.readLine().split(" ");
int n = Integer.parseInt(l[0]);
long k = Long.parseLong(l[1]);
LinkedList<Integer> power = new LinkedList<Integer>();
l = br.readLine().split(" ");
int maxPower = -1;
for(int i=0; i<n; i++) {
int p = Integer.parseInt(l[i]);
power.addLast(p);
if(p > maxPower) {
maxPower = p;
}
}
if(k >= (long)n-1) {
System.out.println(maxPower);
}else {
int a = power.get(0);
int b = power.get(1);
if(a < b) {
power.remove(1);
power.addFirst(b);
}
int consecutive = 0;
while(consecutive < k) {
a = power.get(0);
b = power.get(1);
if(a < b) {
power.remove(0);
power.addLast(a);
consecutive = 1;
}else {
power.remove(1);
power.addLast(b);
consecutive++;
}
}
System.out.println(power.peekFirst());
}
}
}
| 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())
power=list(map(int,input().split()))
if k>=n-1:
maxx=max(power)
print(maxx)
else:
myMap={}
done=0
for i in range(n):
myMap[power[i]]=0
while(True):
if power[0]>power[1]:
myMap[power[0]]+=1
temp=power.pop(1)
power.append(temp)
else:
myMap[power[1]]+=1
temp=power.pop(0)
power.append(temp)
for i in range(n):
if myMap[power[i]]>=k:
print(power[i])
done=1
break
if done==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 | import math
# def p(case, res):
# print "Case #"+str(case+1)+": "+str(res)
n, k = map(int, raw_input().split())
powers = map(int, raw_input().split())
currPower = powers[0]
count = 0
for power in powers[1:]:
if count == k:
break
if power < currPower:
count += 1
else:
count = 1
currPower = power
print currPower | 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 | '''input
5 2
1 4 3 5 2
'''
n, k = map(int, input().split())
a = list(map(int, input().split()))
if k > len(a):
print(max(a))
else:
w = 0
while w < k:
if a[0] < a[1]:
a.append(a.pop(0))
w = 1
else:
w += 1
a.append(a.pop(1))
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 | import java.util.*;
public class B {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
long n = sc.nextLong();
long k = sc.nextLong();
long p = sc.nextLong();
long c = 0;
for (long i = 0; i < n-1; i++) {
long newP = sc.nextLong();
if (newP > p) {
p = newP;
c = 1;
} else {
c++;
if (c >= k) {
break;
}
}
}
System.out.println(p);
}
} | JAVA |
879_B. Table Tennis | n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner.
For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner.
Input
The first line contains two integers: n and k (2 β€ n β€ 500, 2 β€ k β€ 1012) β the number of people and the number of wins.
The second line contains n integers a1, a2, ..., an (1 β€ ai β€ n) β powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct.
Output
Output a single integer β power of the winner.
Examples
Input
2 2
1 2
Output
2
Input
4 2
3 1 2 4
Output
3
Input
6 2
6 5 3 1 2 4
Output
6
Input
2 10000000000
2 1
Output
2
Note
Games in the second sample:
3 plays with 1. 3 wins. 1 goes to the end of the line.
3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner. | 2 | 8 | n, k = map(int, input().split())
power = list(map(int, input().split()))
wins = 0
for i in range(n):
for j in range(i+1, n):
if power[j] < power[i]:
wins += 1
else:
break
if wins >= k:
print(power[i])
exit(0)
wins = 1
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 | #include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 1;
long long k, cnt, tmp;
long long a[MAXN];
int main() {
int i, n;
cin >> n >> k;
for (i = 0; i < n; i++) {
cin >> a[i];
}
if (n == 2) {
cout << max(a[0], a[1]);
} else if (k < n) {
while (cnt < k) {
if (a[0] > a[1]) {
cnt++;
if (cnt == k) {
cout << a[0];
return 0;
}
} else {
cnt = 1;
swap(a[0], a[1]);
}
tmp = a[1];
for (i = 1; i < n - 1; i++) {
a[i] = a[i + 1];
}
a[n - 1] = tmp;
}
} else if (k >= n) {
sort(a, a + n);
cout << a[n - 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 |
first = input()
n,k = [(int)(x) for x in first.split()]
n = (int)(n)
k = (int)(k)
second = input()
nums = [(int)(x) for x in second.split()]
players = [nums[0],nums[1]]
nums.pop(0)
nums.pop(0)
if(n==2):
print(max(players))
else:
while(1):
winner = max(players)
if(len(nums) <= k-1 and max(nums) < winner):
print(winner)
break
elif(max(nums[:(k-1)]) < winner):
print(winner)
break
else:
nums.append(min(players))
players.remove(min(players))
players.append(nums[0])
nums.pop(0)
| PYTHON3 |
879_B. Table Tennis | n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner.
For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner.
Input
The first line contains two integers: n and k (2 β€ n β€ 500, 2 β€ k β€ 1012) β the number of people and the number of wins.
The second line contains n integers a1, a2, ..., an (1 β€ ai β€ n) β powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct.
Output
Output a single integer β power of the winner.
Examples
Input
2 2
1 2
Output
2
Input
4 2
3 1 2 4
Output
3
Input
6 2
6 5 3 1 2 4
Output
6
Input
2 10000000000
2 1
Output
2
Note
Games in the second sample:
3 plays with 1. 3 wins. 1 goes to the end of the line.
3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner. | 2 | 8 | import math
import os
import random
import re
import sys
playerWins = input().split()
player = int(playerWins[0])
wins = int(playerWins[1])
powerValue = list(map(int, input().rstrip().split()[:player]))
a=0
checker = []
if wins > 1000:
wins = 1000
while len(checker) < wins:
if powerValue[a] > powerValue[a+1]:
checker.append(powerValue[a+1])
powerValue.append(powerValue[a+1])
del powerValue[a+1]
elif powerValue[a] < powerValue[a+1]:
powerValue.append(powerValue[a])
del powerValue[a]
checker.clear()
checker.append(powerValue[a+1])
print(powerValue[a])
| PYTHON3 |
879_B. Table Tennis | n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner.
For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner.
Input
The first line contains two integers: n and k (2 β€ n β€ 500, 2 β€ k β€ 1012) β the number of people and the number of wins.
The second line contains n integers a1, a2, ..., an (1 β€ ai β€ n) β powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct.
Output
Output a single integer β power of the winner.
Examples
Input
2 2
1 2
Output
2
Input
4 2
3 1 2 4
Output
3
Input
6 2
6 5 3 1 2 4
Output
6
Input
2 10000000000
2 1
Output
2
Note
Games in the second sample:
3 plays with 1. 3 wins. 1 goes to the end of the line.
3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner. | 2 | 8 | n, k = map(int, input().split())
powers = list(map(int, input().split()))
max_power = max(powers)
def get_winner():
i = 0
size = len(powers)
extra = 1
while True:
if powers[i] == max_power:
return powers[i]
for j in range(i + 1, min(i + k + extra, size)):
if powers[i] < powers[j]:
extra = 0
i = j
break
else:
return powers[i]
print(get_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() {
int n;
cin >> n;
long long k;
cin >> k;
vector<int> a(n);
for (int i = 0; i < n; i++) cin >> a[i];
int x = a[0], c = 0;
for (int i = 1; i < n; i++) {
if (a[i] < x)
c++;
else {
x = a[i];
c = 1;
}
if (c == k || i == n - 1) {
cout << x;
break;
}
}
return 0;
}
| CPP |
879_B. Table Tennis | n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner.
For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner.
Input
The first line contains two integers: n and k (2 β€ n β€ 500, 2 β€ k β€ 1012) β the number of people and the number of wins.
The second line contains n integers a1, a2, ..., an (1 β€ ai β€ n) β powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct.
Output
Output a single integer β power of the winner.
Examples
Input
2 2
1 2
Output
2
Input
4 2
3 1 2 4
Output
3
Input
6 2
6 5 3 1 2 4
Output
6
Input
2 10000000000
2 1
Output
2
Note
Games in the second sample:
3 plays with 1. 3 wins. 1 goes to the end of the line.
3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner. | 2 | 8 | n,k=map(int,raw_input().split())
a=list(map(int,raw_input().split()))
if a[0]>a[1]:
a[0],a[1]=a[1],a[0]
def cal(a,n,k):
maxi=0
cnt=0
for i in a:
if i>maxi:
cnt=1
maxi=i
else:
cnt+=1
if cnt==k:
print maxi
return
print maxi
cal(a,n,k) | PYTHON |
879_B. Table Tennis | n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner.
For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner.
Input
The first line contains two integers: n and k (2 β€ n β€ 500, 2 β€ k β€ 1012) β the number of people and the number of wins.
The second line contains n integers a1, a2, ..., an (1 β€ ai β€ n) β powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct.
Output
Output a single integer β power of the winner.
Examples
Input
2 2
1 2
Output
2
Input
4 2
3 1 2 4
Output
3
Input
6 2
6 5 3 1 2 4
Output
6
Input
2 10000000000
2 1
Output
2
Note
Games in the second sample:
3 plays with 1. 3 wins. 1 goes to the end of the line.
3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner. | 2 | 8 | from math import*
n, k = [int(i) for i in input().split()]
gg = [int(i) for i in input().split()]
cnt = 0
comper = gg[0]
for i in range(1, len(gg)):
if cnt >= k:
print(comper)
break
if comper > gg[i]:
cnt += 1
else:
comper = gg[i]
cnt = 1
else:
print(max(gg))
| 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.LinkedList;
import java.util.Queue;
import java.util.Scanner;
public class CF_443B {
public static void main(String[] args) {
new CF_443B().run();
}
class Value {
public int value;
public long count;
public Value(int value, long count) {
this.value = value;
this.count = count;
}
public int getValue() {
return this.value;
}
public long getCount() {
return this.count;
}
public void addCount() {
this.count++;
}
}
public void run() {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
long k = sc.nextLong();
int i = 1;
Value first = new Value(sc.nextInt(), 0);
Queue<Value> queue = new LinkedList<Value>();
int max = first.getValue();
while (i < n) {
int next = sc.nextInt();
max = Math.max(max, next);
queue.add(new Value(next, 0));
i++;
}
if (k > n) {
System.out.println(max);
return;
}
while (true) {
if (first.getCount() >= k) {
System.out.println(first.getValue());
break;
}
Value top = queue.peek();
if (first.getValue() > top.getValue()) {
queue.poll();
queue.add(top);
first.addCount();
} else {
queue.add(first);
first = queue.poll();
first.addCount();
}
}
}
}
| 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 math
import re
import os
import string
import sys
def ria():
return [int(i) for i in input().split()]
def ri():
return int(input())
def rfa():
return [float(i) for i in input().split()]
eps = 1e-9
def is_equal(a, b):
return abs(a - b) <= eps
def distance(p0, p1):
return math.sqrt((p0[0] - p1[0]) ** 2 + (p0[1] - p1[1]) ** 2)
def distance_sqr(p0, p1):
return (p0[0] - p1[0]) ** 2 + (p0[1] - p1[1]) ** 2
n, k = ria()
ar = ria()
mx = max(ar)
keka = 0
cur = ar[0]
mp = {}
for i in ar:
mp[i] = 0
for i in ar:
if cur == mx:
print(mx)
exit(0)
if i == cur:
continue
if mp[cur] == k:
print(cur)
exit(0)
if i > cur:
mp[i] += 1
cur = i
else:
mp[cur] += 1
if mp[cur] == k:
print(cur)
exit(0)
keka += 1
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 | numofplayers, neededwins = [int(x) for x in input().split()]
line = [int(x) for x in input().split()]
players = list(line)
champ = line.pop(0)
#print(champ)
#print(line)
wins = 0
for x in range(numofplayers-1):
newbie = line.pop(0)
if champ > newbie:
wins = wins + 1
else:
champ = newbie
wins = 1
if wins == neededwins:
break
#print(line)
if line:
print(champ)
else:
print(max(players)) | 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 a[111111];
int main() {
int n;
long long m;
scanf("%d%lld", &n, &m);
int d = 0;
for (int i = 1; i <= n; ++i) scanf("%d", &a[i]);
int ans = a[1];
long long st = 0;
for (int i = 2; i <= n; ++i) {
if (st >= m) break;
if (ans > a[i])
st++;
else {
st = 1;
ans = a[i];
}
}
printf("%d\n", ans);
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()))
high = max(a.pop(0), a.pop(0))
if n == 2:
run = False
else:
run = True
try:
while run:
for i in range(k-1):
if a[i] > high:
high = a[i]
a = a[i+1::]
break
elif i+2 == k:
run = False
break
except IndexError:
pass
print(high) | PYTHON3 |
879_B. Table Tennis | n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner.
For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner.
Input
The first line contains two integers: n and k (2 β€ n β€ 500, 2 β€ k β€ 1012) β the number of people and the number of wins.
The second line contains n integers a1, a2, ..., an (1 β€ ai β€ n) β powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct.
Output
Output a single integer β power of the winner.
Examples
Input
2 2
1 2
Output
2
Input
4 2
3 1 2 4
Output
3
Input
6 2
6 5 3 1 2 4
Output
6
Input
2 10000000000
2 1
Output
2
Note
Games in the second sample:
3 plays with 1. 3 wins. 1 goes to the end of the line.
3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner. | 2 | 8 | import java.util.Scanner;
public class TableTennis {
private int n;
private long k;
private int a[];
private Scanner sc;
public TableTennis() {
sc=new Scanner(System.in);
do{
n=sc.nextInt();
k=sc.nextLong();
}while(n<2 || n>500 || k>Math.pow(10, 12));
a=new int[n];
for(int i=0; i<n; i++){
do{
a[i]=sc.nextInt();
}while(a[i]<1 || a[i]>n);
}
}
public boolean max(int a, int t[]){
for(int i=0; i<n; i++){
if(t[i]>a)
return false;
}
return true;
}
public void play(){
int wins=0;
int player=0;
int nextPlayer=player+1;
while(wins<k){
boolean t=max(a[player],a);
if(a[player]>a[nextPlayer]) {
if(t){
System.out.println(a[player]);
return;
}else{
wins++;
nextPlayer++;
}
if(nextPlayer==n){
nextPlayer=0;
}
}else{
player=nextPlayer;
wins=1;
nextPlayer=player+1;
if(nextPlayer==n)
nextPlayer=0;
}
if(nextPlayer==n && player!=0){
nextPlayer=0;
}
}
System.out.println(a[player]);
}
public static void main(String[] args) {
TableTennis t=new TableTennis();
t.play();
}
} | 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.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayDeque;
import java.util.StringTokenizer;
public class Table_Tennis {
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));
StringTokenizer tk = new StringTokenizer(in.readLine());
ArrayDeque<Integer> q = new ArrayDeque<>();
int num = Integer.parseInt(tk.nextToken());
long k = Long.parseLong(tk.nextToken());
tk = new StringTokenizer(in.readLine());
int maxx = -1;
for (int i = 0; i < num; i++) {
q.add(Integer.parseInt(tk.nextToken()));
if (q.getLast() > maxx) {
maxx = q.getLast();
}
}
int count = 0;
int max = 0;
if (k >= num) {
out.write(maxx + "\n");
out.flush();
return;
}
while (count < k) {
int num1 = q.poll();
int num2 = q.poll();
max = num1;
if (max > num2) {
max = num1;
q.addFirst(max);
q.addLast(num2);
count++;
} else {
max = num2;
q.addLast(num1);
q.addFirst(max);
count = 1;
}
}
out.write(max + "\n");
out.flush();
}
} | JAVA |
879_B. Table Tennis | n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner.
For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner.
Input
The first line contains two integers: n and k (2 β€ n β€ 500, 2 β€ k β€ 1012) β the number of people and the number of wins.
The second line contains n integers a1, a2, ..., an (1 β€ ai β€ n) β powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct.
Output
Output a single integer β power of the winner.
Examples
Input
2 2
1 2
Output
2
Input
4 2
3 1 2 4
Output
3
Input
6 2
6 5 3 1 2 4
Output
6
Input
2 10000000000
2 1
Output
2
Note
Games in the second sample:
3 plays with 1. 3 wins. 1 goes to the end of the line.
3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner. | 2 | 8 | import java.util.*;
import java.io.*;
public class B
{
Reader in;
PrintWriter out;
int i = 0, j = 0;
void solve()
{
//START//
int n2 = in.nextInt();
long n = (long)n2;
long k = in.nextLong();
long top = 0;
ArrayDeque<Long> line = new ArrayDeque<Long>();
long cur = 0;
for (i = 0; i < n2; i++)
{
cur = in.nextLong();
top = Math.max(top, cur);
line.addLast(cur);
}
if (k > n-2)
{
out.println(top);
return;
}
long curWins = 0;
long winner = line.removeFirst();
long opp = 0;
while (!line.isEmpty())
{
opp = line.removeFirst();
if (opp == top)
{
out.println(top);
return;
}
if (opp > winner)
{
curWins = 1;
winner = opp;
if (curWins == k)
{
out.println(winner);
return;
}
}
else
{
curWins++;
if (curWins == k)
{
out.println(winner);
return;
}
}
}
//END
}
void runIO()
{
in = new Reader();
out = new PrintWriter(System.out, false);
solve();
out.close();
}
public static void main(String[] args)
{
new B().runIO();
}
// input/output
static class Reader
{
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public Reader()
{
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public final String next()
{
int c = read();
while (isSpaceChar(c))
{
c = read();
}
StringBuilder res = new StringBuilder();
do
{
res.append((char) c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
private boolean isSpaceChar(int c)
{
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public int nextInt()
{
int ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do
{
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public long nextLong()
{
long ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do
{
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public double nextDouble()
{
double ret = 0, div = 1;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do
{
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (c == '.')
while ((c = read()) >= '0' && c <= '9')
ret += (c - '0') / (div *= 10);
if (neg)
return -ret;
return ret;
}
public int[] readIntArray(int size)
{
int[] arr = new int[size];
for (int i = 0; i < size; i++)
arr[i] = nextInt();
return arr;
}
public long[] readLongArray(int size)
{
long[] arr = new long[size];
for (int i = 0; i < size; i++)
arr[i] = nextInt();
return arr;
}
private void fillBuffer()
{
try
{
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
}
catch (IOException e)
{
}
if (bytesRead == -1)
buffer[0] = -1;
}
private byte read()
{
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
}
}
}
| 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 n, A[505], mx = -1, B[505], tut, vis[505], tp[505];
vector<int> v[50][50];
long long int k;
int main() {
scanf("%d %lld", &n, &k);
for (int i = 1; i <= n; i++) scanf("%d", &A[i]);
for (int i = 1; i <= n; i++) {
if (A[i] > mx) {
mx = A[i];
tut = i;
}
}
for (int i = 1; i <= tut - 1; i++) {
if (vis[i] == 1) continue;
long long int cev = 0;
for (int j = i + 1; j <= n; j++) {
if (A[i] > A[j]) {
tp[i]++;
vis[j] = 1;
} else {
tp[j]++;
break;
}
}
if (tp[i] >= k) {
printf("%d\n", A[i]);
return 0;
}
}
printf("%d\n", 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 | #include <bits/stdc++.h>
using namespace std;
long long k;
int pre, a[500], wins[501], mx = 0, n, i;
int main() {
scanf("%d%lld", &n, &k);
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
mx = max(a[i], mx);
}
if (k >= n) {
printf("%d\n", mx);
return 0;
}
pre = a[0];
for (i = 1; wins[pre] < k; i++) {
if (a[i] > pre) {
wins[a[i]]++;
pre = a[i];
} else {
wins[pre]++;
}
if (i == n - 1) i = -1;
}
if (wins[pre] == k)
cout << pre << endl;
else
cout << a[i] << endl;
return 0;
}
| CPP |
879_B. Table Tennis | n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner.
For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner.
Input
The first line contains two integers: n and k (2 β€ n β€ 500, 2 β€ k β€ 1012) β the number of people and the number of wins.
The second line contains n integers a1, a2, ..., an (1 β€ ai β€ n) β powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct.
Output
Output a single integer β power of the winner.
Examples
Input
2 2
1 2
Output
2
Input
4 2
3 1 2 4
Output
3
Input
6 2
6 5 3 1 2 4
Output
6
Input
2 10000000000
2 1
Output
2
Note
Games in the second sample:
3 plays with 1. 3 wins. 1 goes to the end of the line.
3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner. | 2 | 8 | n, k = map(int, input().split())
L = list(map(int, input().split()))
d = {}
mx = L[0]
for i in range(1, n):
mx = max(mx, L[i])
if mx in d:
d[mx] += 1
else:
d[mx] = 1
if d[mx] == k:
print(mx)
exit()
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 | #include <bits/stdc++.h>
using namespace std;
string s;
long long n, a[100005], b[100005], m;
void solve() {
cin >> n >> m;
for (int i = 1; i <= n; i++) cin >> a[i];
long long k = max(a[1], a[2]), ans = 0, p = 1;
for (int i = 3; i <= n; i++) {
if (a[i] > k) {
k = a[i];
p = 1;
} else {
p++;
if (p == m) break;
}
}
cout << k;
}
int main() { solve(); }
| CPP |
879_B. Table Tennis | n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner.
For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner.
Input
The first line contains two integers: n and k (2 β€ n β€ 500, 2 β€ k β€ 1012) β the number of people and the number of wins.
The second line contains n integers a1, a2, ..., an (1 β€ ai β€ n) β powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct.
Output
Output a single integer β power of the winner.
Examples
Input
2 2
1 2
Output
2
Input
4 2
3 1 2 4
Output
3
Input
6 2
6 5 3 1 2 4
Output
6
Input
2 10000000000
2 1
Output
2
Note
Games in the second sample:
3 plays with 1. 3 wins. 1 goes to the end of the line.
3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner. | 2 | 8 | tamanho, quantidades_vit = map(int, input().split())
desafiantes = list(map(int, input().split()))
maior = max(desafiantes)
if quantidades_vit >= tamanho:
print(maior)
else:
vencedor_atual = None
cont = 0
while True:
if cont >= quantidades_vit or vencedor_atual == maior:
print(vencedor_atual)
break
vencedor_anterior = vencedor_atual
if desafiantes[0] > desafiantes[1]:
perdedor = desafiantes.pop(1)
desafiantes.append(perdedor)
vencedor_atual = desafiantes[0]
cont += 1
else:
perdedor = desafiantes.pop(0)
desafiantes.append(perdedor)
vencedor_atual = desafiantes[1]
cont = 1
| PYTHON3 |
879_B. Table Tennis | n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner.
For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner.
Input
The first line contains two integers: n and k (2 β€ n β€ 500, 2 β€ k β€ 1012) β the number of people and the number of wins.
The second line contains n integers a1, a2, ..., an (1 β€ ai β€ n) β powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct.
Output
Output a single integer β power of the winner.
Examples
Input
2 2
1 2
Output
2
Input
4 2
3 1 2 4
Output
3
Input
6 2
6 5 3 1 2 4
Output
6
Input
2 10000000000
2 1
Output
2
Note
Games in the second sample:
3 plays with 1. 3 wins. 1 goes to the end of the line.
3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner. | 2 | 8 | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
public class CF8 {
final static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
final static PrintWriter pw = new PrintWriter(System.out);
public static void main(String[] args) {
long[]ar=readLongArr();
long n=ar[0];
long k=ar[1];
int[]arr=readIntArr();
int gsf=0;
l:for(int i=0;i<n;i++) {
if(arr[i]>gsf) {
int tk=0;
if(i>0)tk=1;
for(int j=i+1;j<Math.min(i+k+1-tk,n);j++) {
if(arr[i]<arr[j]) {
continue l;
}
}
gsf=arr[i];
break l;
}
}
pw.println(gsf);
pw.close();
}
static String readLine() {
String line = null;
try {
line = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return line;
}
static String readString() {
return readLine();
}
static public long readlong() {
return Long.parseLong(readLine());
}
static public int readInt() {
return Integer.parseInt(readLine());
}
static String[] stringArray() {
StringTokenizer st = new StringTokenizer(readLine());
int n = st.countTokens();
String ret[] = new String[n];
for (int i = 0; i < n; i++) {
ret[i] = st.nextToken();
}
return ret;
}
static public int[] readIntArr() {
String[] str = stringArray();
int arr[] = new int[str.length];
for (int i = 0; i < arr.length; i++)
arr[i] = Integer.parseInt(str[i]);
return arr;
}
static public double[] readDoubleArr() {
String[] str = stringArray();
double arr[] = new double[str.length];
for (int i = 0; i < arr.length; i++)
arr[i] = Double.parseDouble(str[i]);
return arr;
}
static public long[] readLongArr() {
String[] str = stringArray();
long arr[] = new long[str.length];
for (int i = 0; i < arr.length; i++)
arr[i] = Long.parseLong(str[i]);
return arr;
}
static public double readDouble() {
return Double.parseDouble(readLine());
}
} | 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())
arr=map(int,raw_input().split())
count={}
for i in arr:
count[i]=0
if k>=n-1:
print max(arr)
else:
while(1):
if arr[0]>arr[1]:
count[arr[0]]+=1
count[arr[1]]=0
if count[arr[0]]>=k:
print arr[0]
break
arr=[arr[0]]+arr[2:]+[arr[1]]
else:
count[arr[1]] += 1
count[arr[0]] = 0
if count[arr[1]] >= k:
print arr[1]
break
arr=arr[1:]+[arr[0]]
| PYTHON |
879_B. Table Tennis | n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner.
For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner.
Input
The first line contains two integers: n and k (2 β€ n β€ 500, 2 β€ k β€ 1012) β the number of people and the number of wins.
The second line contains n integers a1, a2, ..., an (1 β€ ai β€ n) β powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct.
Output
Output a single integer β power of the winner.
Examples
Input
2 2
1 2
Output
2
Input
4 2
3 1 2 4
Output
3
Input
6 2
6 5 3 1 2 4
Output
6
Input
2 10000000000
2 1
Output
2
Note
Games in the second sample:
3 plays with 1. 3 wins. 1 goes to the end of the line.
3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner. | 2 | 8 | import org.omg.CORBA.INTERNAL;
import org.omg.PortableInterceptor.INACTIVE;
import java.io.*;
import java.util.*;
import java.lang.*;
import java.math.*;
public class Solution {
static class TaskG {
private void solve(int test, FastScanner in, PrintWriter out) {
int n = in.nextInt();
long k = in.nextLong();
if(k > n) k = n;
int wins = 0; int max = in.nextInt();
for(int i = 0; i < n-1; i++){
int a = in.nextInt();
if(a > max) {
max = a;
wins = 1;
}else{
wins++;
}
if(wins == k || i == n-2){
out.println(max);
return;
}
}
}
}
public static void main(String[] args) throws IOException {
FastScanner in = new FastScanner(System.in);
PrintWriter out = new PrintWriter(System.out);
// FastScanner in = new FastScanner("sochi.in");
// PrintWriter out = new PrintWriter(new FileWriter("sochi.out"));
new TaskG().solve(1, in, out);
out.close();
}
static class FastScanner {
BufferedReader br;
StringTokenizer token;
public FastScanner(InputStream is) {
br = new BufferedReader(new InputStreamReader(System.in));
}
public FastScanner(String s) {
try {
br = new BufferedReader(new FileReader(s));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public String nextToken() {
while (token == null || !token.hasMoreTokens()) {
try {
token = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return token.nextToken();
}
public int nextInt() {
return Integer.parseInt(nextToken());
}
public long nextLong() {
return Long.parseLong(nextToken());
}
public double nextDouble() {
return Double.parseDouble(nextToken());
}
}
} | JAVA |
879_B. Table Tennis | n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner.
For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner.
Input
The first line contains two integers: n and k (2 β€ n β€ 500, 2 β€ k β€ 1012) β the number of people and the number of wins.
The second line contains n integers a1, a2, ..., an (1 β€ ai β€ n) β powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct.
Output
Output a single integer β power of the winner.
Examples
Input
2 2
1 2
Output
2
Input
4 2
3 1 2 4
Output
3
Input
6 2
6 5 3 1 2 4
Output
6
Input
2 10000000000
2 1
Output
2
Note
Games in the second sample:
3 plays with 1. 3 wins. 1 goes to the end of the line.
3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner. | 2 | 8 | ar = map(int, raw_input().split())
flag = 0
fl = 0
ii = 0
a = map(int, raw_input().split())
i = 0
ma = 0
while i< ar[0]:
count = 0
if i >0 :
count = 1
if ma < a[i]:
ma = a[i]
for j in range(i+1,ar[0]):
if a[j] < a[i]:
count+=1
else:
break
if count >=ar[1]:
print a[i]
flag = 1
break
else:
if i > 0:
i+= count
else:
i+= count+1
if flag !=1:
print ma
| PYTHON |
879_B. Table Tennis | n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner.
For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner.
Input
The first line contains two integers: n and k (2 β€ n β€ 500, 2 β€ k β€ 1012) β the number of people and the number of wins.
The second line contains n integers a1, a2, ..., an (1 β€ ai β€ n) β powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct.
Output
Output a single integer β power of the winner.
Examples
Input
2 2
1 2
Output
2
Input
4 2
3 1 2 4
Output
3
Input
6 2
6 5 3 1 2 4
Output
6
Input
2 10000000000
2 1
Output
2
Note
Games in the second sample:
3 plays with 1. 3 wins. 1 goes to the end of the line.
3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner. | 2 | 8 | import java.util.Scanner;
import java.util.*;
public class Solution
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
long n = sc.nextInt();
String kk = sc.next();
long k = Long.parseLong(kk);
Queue<Integer> intQueue = new LinkedList();
for(int i = 0; i < n; i ++){
intQueue.offer(sc.nextInt());
}
long win = 0;
int player = intQueue.remove();
int comp;
while(win != k && win < n){
comp = intQueue.remove();
if(player > comp){
win++;
intQueue.add(comp);
}
else {
win = 1;
intQueue.add(player);
player = comp;
}
}
System.out.println(player);
}
}
| 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[100005];
int main() {
long long n, k, maxx = -1, flag = 0;
scanf("%lld %lld", &n, &k);
for (long long i = 1; i <= n; i++) {
scanf("%lld", &a[i]);
if (a[i] > maxx) {
maxx = a[i];
}
}
long long s = 1;
for (long long i = 1; i <= n; i++) {
s++;
if (i != 1 && a[i] > a[i - 1]) {
flag = 1;
} else {
flag = 0;
}
for (long long j = i + 1; j <= n; j++) {
if (a[i] > a[j]) {
flag++;
} else {
break;
}
}
if (flag >= k) {
printf("%lld\n", a[i]);
break;
}
}
if (s > n) {
printf("%lld\n", maxx);
}
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 | /**
* DA-IICT
* Author : PARTH PATEL
*/
import java.io.*;
import java.math.*;
import java.util.*;
import static java.util.Arrays.fill;
import static java.lang.Math.*;
import static java.util.Arrays.sort;
import static java.util.Collections.sort;
public class B879
{
public static int mod = 1000000007;
static FasterScanner in = new FasterScanner();
static PrintWriter out = new PrintWriter(System.out);
public static void main(String[] args)
{
int n=in.nextInt();
long k=in.nextLong();
int[] arr=new int[n+1];
int maxi=-1;
for(int i=1;i<=n;i++)
{
arr[i]=in.nextInt();
maxi=max(maxi, arr[i]);
}
if(k>=n)
{
out.println(maxi);
out.flush();
return;
}
else
{
int max=arr[1];
int win=0;
for(int i=2;i<=n;i++)
{
if(arr[i]>max)
{
max=arr[i];
win=0;
}
win++;
if(win>=k)
break;
}
out.println(max);
}
out.close();
}
public static long pow(long x, long n, long mod)
{
long res = 1;
for (long p = x; n > 0; n >>= 1, p = (p * p) % mod)
{
if ((n & 1) != 0)
{
res = (res * p % mod);
}
}
return res;
}
public static long gcd(long n1, long n2)
{
long r;
while (n2 != 0)
{
r = n1 % n2;
n1 = n2;
n2 = r;
}
return n1;
}
public static long lcm(long n1, long n2)
{
long answer = (n1 * n2) / (gcd(n1, n2));
return answer;
}
static class FasterScanner
{
private byte[] buf = new byte[1024];
private int curChar;
private int snumChars;
public int read()
{
if (snumChars == -1)
throw new InputMismatchException();
if (curChar >= snumChars)
{
curChar = 0;
try
{
snumChars = System.in.read(buf);
} catch (IOException e)
{
throw new InputMismatchException();
}
if (snumChars <= 0)
return -1;
}
return buf[curChar++];
}
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 String nextString()
{
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 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 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 int[] nextIntArray(int n)
{
int[] arr = new int[n];
for (int i = 0; i < n; i++)
{
arr[i] = nextInt();
}
return arr;
}
public long[] nextLongArray(int n)
{
long[] arr = new long[n];
for (int i = 0; i < n; i++)
{
arr[i] = nextLong();
}
return arr;
}
private 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;
}
}
}
| 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 sys
def slist_to_int_list(l):
return [int(x) for x in l]
nbline = 0
for line in sys.stdin:
if nbline == 0:
ns,ks = line.split(" ")
n = int(ns)
k = int(ks)
nbline +=1
else:
order = slist_to_int_list(line.split(" "))
#print n,k
#print order
best = -1
i = 0
for player in order:
if i == 0:
best = player
i+=1
win = 0
else:
#print player,best,win
if player > best :
best = player
win = 1
else:
win += 1
if win == k:
print best
exit(0)
print best
| PYTHON |
879_B. Table Tennis | n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner.
For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner.
Input
The first line contains two integers: n and k (2 β€ n β€ 500, 2 β€ k β€ 1012) β the number of people and the number of wins.
The second line contains n integers a1, a2, ..., an (1 β€ ai β€ n) β powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct.
Output
Output a single integer β power of the winner.
Examples
Input
2 2
1 2
Output
2
Input
4 2
3 1 2 4
Output
3
Input
6 2
6 5 3 1 2 4
Output
6
Input
2 10000000000
2 1
Output
2
Note
Games in the second sample:
3 plays with 1. 3 wins. 1 goes to the end of the line.
3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner. | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int main() {
long long n, k;
cin >> n >> k;
if (n < k) k = n + 3;
vector<long> A(n);
for (long i = 0; i < n; i++) {
cin >> A[i];
}
long score = 0;
long champ = 0;
long chal = 1;
while (score < k) {
if (champ == chal) {
chal++;
chal = chal % n;
}
if (A[champ] > A[chal]) {
chal++;
chal = chal % n;
score++;
} else if (A[champ] < A[chal]) {
champ = chal;
score = 1;
chal = champ + 1;
chal = chal % n;
}
}
cout << A[champ];
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 n, m, k, f, current;
int flag, restart, flag2;
priority_queue<int> pr;
int main() {
cin >> n >> m;
f = m;
queue<int> qu;
cin >> flag;
pr.push(flag);
for (int i = 0; i < n - 1; i++) {
cin >> k;
qu.push(k);
pr.push(k);
}
current = qu.front();
if (n == 2) {
if (flag > current)
cout << flag << endl;
else
cout << current << endl;
} else if (m > n) {
cout << pr.top() << endl;
} else {
for (int i = 1; m != 0; i++) {
if (flag > current) {
m--;
qu.push(current);
qu.pop();
} else if (current > flag) {
m = f;
qu.push(flag);
qu.pop();
flag = current;
m--;
}
current = qu.front();
}
cout << flag << 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 mejor(int player, long long int k, int *arr, int n) {
if (player == 0) {
if (player + k >= n - 1)
cout << n << endl;
else {
for (int i = 0; i < k; i++) {
if (arr[player] < arr[player + 1 + i]) {
mejor(player + 1 + i, k, arr, n);
return;
}
}
cout << arr[player] << endl;
}
} else {
if (player + k > n - 1)
cout << n << endl;
else {
for (int i = 0; i < k - 1; i++) {
if (arr[player] < arr[player + 1 + i]) {
mejor(player + 1 + i, k, arr, n);
return;
}
}
cout << arr[player] << endl;
}
}
return;
}
int main() {
int n;
long long int k;
cin >> n >> k;
int arr[n];
for (int i = 0; i < n; i++) cin >> arr[i];
if (k >= n - 1)
cout << n << endl;
else
mejor(0, k, arr, n);
return 0;
}
| CPP |
879_B. Table Tennis | n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner.
For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner.
Input
The first line contains two integers: n and k (2 β€ n β€ 500, 2 β€ k β€ 1012) β the number of people and the number of wins.
The second line contains n integers a1, a2, ..., an (1 β€ ai β€ n) β powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct.
Output
Output a single integer β power of the winner.
Examples
Input
2 2
1 2
Output
2
Input
4 2
3 1 2 4
Output
3
Input
6 2
6 5 3 1 2 4
Output
6
Input
2 10000000000
2 1
Output
2
Note
Games in the second sample:
3 plays with 1. 3 wins. 1 goes to the end of the line.
3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner. | 2 | 8 | from collections import deque
n , k = map(int , input().split())
l = [int(i) for i in input().split()]
cont = 0 ; x = deque(l) ; j = l[0] ;
x.popleft()
x_init = x
flag = 0
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 = map(int, input().split())
a = list(map(int, input().split()))
high = max(a.pop(0), a.pop(0))
try:
if n == 2:
raise IndexError
while True:
for i in range(k-1):
if a[i] > high:
high = a[i]
a = a[i+1::]
break
elif i+2 == k:
raise IndexError
except IndexError:
print(high)
| 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()]
array = [int(x) for x in input().split()]
count = 1
count1 = 0
previous_winner = -1
while count < k and count1 < n:
winner = max(array[0], array[1])
loser = min(array[0], array[1])
if winner == previous_winner:
count += 1
else:
previous_winner = winner
count = 1
array.remove(loser)
array.append(loser)
count1 += 1
else:
print(array[0]) | PYTHON3 |
879_B. Table Tennis | n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner.
For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner.
Input
The first line contains two integers: n and k (2 β€ n β€ 500, 2 β€ k β€ 1012) β the number of people and the number of wins.
The second line contains n integers a1, a2, ..., an (1 β€ ai β€ n) β powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct.
Output
Output a single integer β power of the winner.
Examples
Input
2 2
1 2
Output
2
Input
4 2
3 1 2 4
Output
3
Input
6 2
6 5 3 1 2 4
Output
6
Input
2 10000000000
2 1
Output
2
Note
Games in the second sample:
3 plays with 1. 3 wins. 1 goes to the end of the line.
3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner. | 2 | 8 | import java.io.*;
import java.util.*;
public class d {
static pair[] seg;
static int[] a;
public static void main(String args[]) throws IOException {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader sc = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
int n=sc.nextInt();
long k=sc.nextLong();
int[]a = new int[n];
for(int i =0;i<n;i++)a[i]=sc.nextInt();
for(int i=0,l=1;i<n;i++) {
int count = 0;
while (l < n && a[i] >= a[l] && count != k) {
l++;
count++;
}
if (count == k || l == n) {
out.print(a[i]);
break;
}
}
out.close();
}
public static void shuffle(long[] arr) {
int n = arr.length;
Random rnd = new Random();
for (int i = 0; i < n; ++i) {
long tmp = arr[i];
int randomPos = i + rnd.nextInt(n - i);
arr[i] = arr[randomPos];
arr[randomPos] = tmp;
}
}
public static void shuffle(int[] arr) {
int n = arr.length;
Random rnd = new Random();
for (int i = 0; i < n; ++i) {
int tmp = arr[i];
int randomPos = i + rnd.nextInt(n - i);
arr[i] = arr[randomPos];
arr[randomPos] = tmp;
}
}
public static int gcd(int x, int y) {
if (y == 0)
return x;
return gcd(y, x % y);
}
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.valueOf(next());
}
public double nextDouble() {
return Double.valueOf(next());
}
String nextLine() throws IOException {
return reader.readLine();
}
}
}
class pair implements Comparable<pair> {
int l, r;
pair(int l, int r) {
this.l = l;
this.r = r;
}
@Override
public int compareTo(pair o) {
if (o.l == l) {
return r-o.r;
}
return l - o.l;
}
} | 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.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class b{
public static void main(String[] args) {
FastScanner in=new FastScanner();
int n=in.nextInt();
long k=in.nextLong();
int[] as=new int[n];
for(int i=0;i<n;i++) {
as[i]=in.nextInt();
}
if(k>n) {
System.out.println(n);
}
else {
int winner=as[0];
int wincnt=0;
for(int i=1;i<n;i++) {
if(wincnt==k) {
break;
}
if(as[i]>winner) {
wincnt=1;
winner=as[i];
}
else {
wincnt++;
}
}
if(wincnt!=k) {
System.out.println(n);
}
else
System.out.println(winner);
}
}
public static class FastScanner{
BufferedReader br;
StringTokenizer st;
public FastScanner(String s){
try {
br = new BufferedReader(new FileReader(s));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public FastScanner(){
br = new BufferedReader(new InputStreamReader(System.in));
}
String nextToken(){
while (st == null || !st.hasMoreElements()){
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt(){
return Integer.parseInt(nextToken());
}
long nextLong(){
return Long.parseLong(nextToken());
}
double nextDouble(){
return Double.parseDouble(nextToken());
}
String next(){
return nextToken();
}
}
}
| JAVA |
879_B. Table Tennis | n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner.
For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner.
Input
The first line contains two integers: n and k (2 β€ n β€ 500, 2 β€ k β€ 1012) β the number of people and the number of wins.
The second line contains n integers a1, a2, ..., an (1 β€ ai β€ n) β powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct.
Output
Output a single integer β power of the winner.
Examples
Input
2 2
1 2
Output
2
Input
4 2
3 1 2 4
Output
3
Input
6 2
6 5 3 1 2 4
Output
6
Input
2 10000000000
2 1
Output
2
Note
Games in the second sample:
3 plays with 1. 3 wins. 1 goes to the end of the line.
3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner. | 2 | 8 | n, needed = map(int, input().split())
players = list(map(int, input().split()))
winner = players[0]
wins = 0
for i in range(1, n):
if players[i] > winner:
winner = players[i]
wins = 1
else:
wins += 1
if wins >= needed:
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 | n,k = map(int, input().split())
a=[None]*n
a = list(map(int, input().split()))
c=0
while(c!=k):
x=0
for i in range(1,n):
if(a[0]>a[i]):
x+=1
else:
break
if(x==k or x==n-1):
c=k
break
else:
if(a[0]>a[1]):
t=a[1]
a.pop(1)
a.append(t)
c+=1
elif(a[0]<a[1]):
t=a[0]
a.pop(0)
a.append(t)
c=1
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 | n,k = map(int,input().split())
data = list(map(int,input().split()))
if k>n-2:
print(n)
else:
w=0
a=[0]*501
for j in range(1,n):
if data[j]>data[w]:
w=j
a[w]+=1
if a[w]>=k:
print(data[w])
break
else:
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 | class Solver:
def run(self):
n, k = map(int, input().split())
a = list(map(int, input().split()))
if k < n:
w = 0
j = 0
for i in range(1, n):
if a[i] > a[j]:
w = 1
j = i
else:
w += 1
if w == k:
print(a[j])
return
print(max(a))
if __name__ == '__main__':
Solver().run()
| 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())
powers = list(map(int,input().split()))
#print(powers)
wins = [0]*(len(powers)+1)
while True:
if powers[0] > powers[1]:
wins[powers[0]] += 1
t = powers[1]
del powers[1]
powers.append(t)
else:
wins[powers[1]] += 1
t = powers[0]
del powers[0]
powers.append(t)
if powers[0] == len(powers):
print(powers[0])
break
if wins[powers[0]] >= k:
print(powers[0])
break
| PYTHON3 |
879_B. Table Tennis | n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner.
For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner.
Input
The first line contains two integers: n and k (2 β€ n β€ 500, 2 β€ k β€ 1012) β the number of people and the number of wins.
The second line contains n integers a1, a2, ..., an (1 β€ ai β€ n) β powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct.
Output
Output a single integer β power of the winner.
Examples
Input
2 2
1 2
Output
2
Input
4 2
3 1 2 4
Output
3
Input
6 2
6 5 3 1 2 4
Output
6
Input
2 10000000000
2 1
Output
2
Note
Games in the second sample:
3 plays with 1. 3 wins. 1 goes to the end of the line.
3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner. | 2 | 8 | n,k = map(int, raw_input().split())
a = map(int, raw_input().split())
if(n==2):
print max(a)
exit(0)
ans = []
count = 0
for i in range(0,n):
for j in range(i+1,2*n):
if(a[j%n]<a[i]):
count+=1
else:
break
ans.append(count)
count = 1
if(k>n-2):
print max(a)
else:
for i in range(len(ans)):
if(ans[i]>=k):
print a[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 | line = input().split(" ")
n = int(line[0])
k = int(line[1])
line = input().split(" ")
powers = [int(line[i]) for i in range(n)]
winner = 0
count = 0
if k > n - 2:
winner = max(powers)
else:
while True:
if powers[0] > powers[1]:
count += 1
winner = powers[0]
powers.append(powers[1])
del powers[1]
else:
count = 1
winner = powers[1]
powers.append(powers[0])
del powers[0]
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 int n, k, count = 0, max = 0;
cin >> n >> k;
int arr[n];
for (int i = 0; i < n; i++) {
cin >> arr[i];
if (max < arr[i]) {
max = arr[i];
}
}
while (count != k) {
if (arr[0] == max) {
break;
}
if (arr[0] > arr[1]) {
int p = arr[1];
for (int i = 1; i < n - 1; i++) {
arr[i] = arr[i + 1];
}
arr[n - 1] = p;
count++;
} else {
int p = arr[0];
arr[0] = arr[1];
arr[1] = p;
for (int i = 1; i < n - 1; i++) {
arr[i] = arr[i + 1];
}
arr[n - 1] = p;
count = 1;
}
}
cout << arr[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 | n, k = [int(i) for i in raw_input().split(' ')]
powers = [int(i) for i in raw_input().split(' ')]
if n - 1 <= k:
print max(powers)
exit()
start = 0
i = 0
while start < k:
current = powers[0]
post = powers[1]
p = min(current, post)
powers.remove(p)
powers.append(p)
if current == p :
start = 1
else:
start = start + 1
print current
| PYTHON |
879_B. Table Tennis | n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner.
For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner.
Input
The first line contains two integers: n and k (2 β€ n β€ 500, 2 β€ k β€ 1012) β the number of people and the number of wins.
The second line contains n integers a1, a2, ..., an (1 β€ ai β€ n) β powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct.
Output
Output a single integer β power of the winner.
Examples
Input
2 2
1 2
Output
2
Input
4 2
3 1 2 4
Output
3
Input
6 2
6 5 3 1 2 4
Output
6
Input
2 10000000000
2 1
Output
2
Note
Games in the second sample:
3 plays with 1. 3 wins. 1 goes to the end of the line.
3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner. | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
const long long oo = (1 << 30);
int main() {
long long n, k;
cin >> n >> k;
vector<long long> data(n);
for (long long i = 0; i < n; i++) cin >> data[i];
if (k >= n - 1) {
cout << n << '\n';
return 0;
}
long long cur1 = data[0];
long long cur2 = data[1];
queue<long long> Q;
for (long long i = 2; i < n; i++) Q.push(data[i]);
long long win;
if (cur1 > cur2)
win = cur1, Q.push(cur2);
else
win = cur2, Q.push(cur1);
long long tkn = 1;
while (1) {
long long t = Q.front();
Q.pop();
if (t > win)
Q.push(win), win = t, tkn = 1;
else
Q.push(t), tkn++;
if (tkn == k) {
cout << win << '\n';
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 |
import java.util.Scanner;
public class CF879B
{
public static void main(String[] args)
{
CF879B task = new CF879B();
Problem solver = task.new Problem();
Scanner in = new Scanner(System.in);
int n = 0;
long k = 0;
int[] a = new int[0];
n = in.nextInt();
k = in.nextLong();
a = new int[n];
for(int i = 0; i < n; i++)
a[i] = in.nextInt();
System.out.println(solver.solve(n, k, a));
}
public class Problem
{
public Problem()
{
}
public class Element
{
public Element()
{
}
public Element(Element el)
{
index = el.index;
value = el.value;
leftElement = el.leftElement;
wins = el.wins;
}
public int index = 0;
public int value = 0;
public Element leftElement;
public int wins = 0;
public boolean poped = false;
public void push(Element el)
{
Element tempins = new Element(this);
value = el.value;
wins = el.wins;
if(poped == false)
{
if(leftElement != null)
{
leftElement.push(tempins);
}
}
else
{
poped = false;
}
}
public void pop()
{
poped = true;
}
}
public void swap(Element el1,Element el2)
{
int tempv = el1.value;
el1.value = el2.value;
el2.value = tempv;
tempv = el1.wins;
el1.wins = el2.wins;
el2.wins = tempv;
}
public int solve(int n,long k,int[] a)
{
int ans = 0;
long v = 0;
int lrgstWin = 0;
Element[] players = new Element[a.length];
for(int i = 0; i < a.length; i++)
{
players[i] = new Element();
players[i].index = i;
players[i].value = a[i];
if(i > 0)
players[i].leftElement = players[i - 1];
}
for(int i = 0; i < players.length && v < k; i++)
{
if(players[1].value > players[0].value)
{
swap(players[0],players[1]);
players[1].pop();
players[players.length - 1].push(players[1]);
players[0].wins = players[0].wins + 1;
v = players[0].wins;
}
else
{
players[1].pop();
players[players.length - 1].push(players[1]);
players[0].wins = players[0].wins + 1;
v = players[0].wins;
}
}
ans = players[0].value;
return 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 | #include <bits/stdc++.h>
using namespace std;
int n;
long long k;
int arr[501];
int temp = -987654321;
int main() {
cin >> n >> k;
for (int i = 1; i <= n; i++) {
scanf(" %d", &arr[i]);
temp = max(temp, arr[i]);
}
if (k > n) {
cout << temp << endl;
return 0;
}
queue<pair<int, long long>> q;
for (int i = 1; i <= n; i++) {
q.push(make_pair(i, 0));
}
int ans = -1;
int currIdx = q.front().first;
long long currTimes = q.front().second;
q.pop();
while (1) {
if (currTimes == k) {
ans = arr[currIdx];
break;
}
int nextIdx = q.front().first;
long long nextTimes = q.front().second;
int curr = arr[currIdx];
int next = arr[nextIdx];
if (curr < next) {
currIdx = nextIdx;
currTimes = nextTimes + 1;
q.pop();
q.push(make_pair(currIdx, currTimes));
} else {
currTimes++;
q.pop();
q.push(make_pair(nextIdx, nextTimes));
}
}
cout << ans << endl;
return 0;
}
| CPP |
879_B. Table Tennis | n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner.
For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner.
Input
The first line contains two integers: n and k (2 β€ n β€ 500, 2 β€ k β€ 1012) β the number of people and the number of wins.
The second line contains n integers a1, a2, ..., an (1 β€ ai β€ n) β powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct.
Output
Output a single integer β power of the winner.
Examples
Input
2 2
1 2
Output
2
Input
4 2
3 1 2 4
Output
3
Input
6 2
6 5 3 1 2 4
Output
6
Input
2 10000000000
2 1
Output
2
Note
Games in the second sample:
3 plays with 1. 3 wins. 1 goes to the end of the line.
3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner. | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long n, k;
cin >> n >> k;
long long arr[n + 1];
for (long long i = 1; i <= n; i++) {
cin >> arr[i];
}
if (k >= n) {
cout << (*max_element(arr + 1, arr + n + 1)) << endl;
} else {
for (long long i = 1; i <= n; i++) {
if (i == 1) {
bool wrong = 0;
long long index = i + 1, counter = k;
if (index > n) {
index = 1;
}
while (counter--) {
if (arr[i] < arr[index]) {
wrong = 1;
}
index++;
if (index == n + 1) index = 1;
}
if (wrong == 0) {
cout << arr[i] << endl;
return 0;
}
} else {
long long maxi = INT_MIN;
for (long long j = 1; j < i; j++) {
maxi = max(maxi, arr[j]);
}
if (arr[i] > maxi) {
bool wrong = 0;
long long index = i + 1, counter = k - 1;
if (index > n) {
index = 1;
}
while (counter--) {
if (arr[i] < arr[index]) {
wrong = 1;
}
index++;
if (index == n + 1) index = 1;
}
if (wrong == 0) {
cout << arr[i] << 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 | size, totalwins = [int(x) for x in input().split()]
line = [int(x) for x in input().split()]
players = list(line)
champ = line.pop(0)
wins = 0
for x in range(size-1):
newbie = line.pop(0)
if champ > newbie:
wins += 1
else:
champ = newbie
wins = 1
if wins == totalwins:
break
if line:
print(champ)
else:
print(max(players))
| 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
playerAndWins = input().split()
noOfPlayers = int(playerAndWins[0])
noOfWins = int(playerAndWins[1])
playerPower = list(map(int, input().rstrip().split()[:noOfPlayers]))
i=0
checker = []
if noOfWins > 1000:
noOfWins = 1000
while len(checker) < noOfWins:
if playerPower[i] > playerPower[i+1]:
checker.append(playerPower[i+1])
playerPower.append(playerPower[i+1])
del playerPower[i+1]
elif playerPower[i] < playerPower[i+1]:
playerPower.append(playerPower[i])
del playerPower[i]
checker.clear()
checker.append(playerPower[i+1])
print(playerPower[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 | n,k=map(int,input().split())
t=list(map(int,input().split()))
j=0
maxx=0
a=1
for i in t:
if i>maxx:
maxx=i
h=1
if a==1:
h=h-1
a=0
else:
h+=1
if h==k:
break
print(maxx)
| 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;
long long tmp = k;
vector<int> v;
for (int i = 0; i < n; i++) {
int x;
cin >> x;
v.push_back(x);
}
int mostRecentPower = v[0];
for (int i = 1; i < n; i++) {
if (mostRecentPower > v[i]) {
tmp--;
} else {
mostRecentPower = v[i];
tmp = k;
tmp--;
}
if (tmp == 0) break;
}
cout << mostRecentPower << 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 | def main():
n, k = map(int, input().split())
p = list(map(int, input().split()))
if k >= n:
print(max(p))
else:
for i in range(n):
t = p[i]
x = 1 if i == 0 else 0
if t == max(p[i:i+k+x]):
print(t)
return
print(max(p))
if __name__ == "__main__":
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.util.Scanner;
public class Mam {
public static long geo(int q,int n ) {
if (n==0 )return q;
else {
return (long) ( 1-Math.pow(q,n+1)/(1-q) )-1;
} }
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 t[]=new int[n];
for(int i=0;i<n;i++) {
t[i]=sc.nextInt();
}
if(n==2) {
if(t[0]>t[1]) {
System.out.println(t[0]);
}
else System.out.println(t[1]);
}
else {
int s=0;
for(int i=0;i<n;i++) {
if(i==n-1) {
System.out.println(t[i]);
break;
}
boolean vf=false;
int pos=-1;
int poss=0;
for(int j=i+1;j<n;j++) {
if(j>n) break;
if(t[i]>t[j]) {
s++;
vf=true;
poss=i;
}
//
else {
vf=false;
s=1;
poss=j;
break;
}
if(s>=k) {
break;
}
}
if(vf) {
System.out.println(t[poss]);
break;
}
/*if(vf && pos==n-1 ) {
System.out.println(t[poss]);
break;
}
*/
}
}
}
}
| 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 int N = 505;
int n;
int arr[N];
int main() {
long long int k;
cin >> n >> k;
k = min(k, 1LL * (n - 1));
for (int i = 1; i <= n; ++i) cin >> arr[i];
list<int> l;
for (int i = 2; i <= n; ++i) l.push_back(arr[i]);
int cur = arr[1];
int wins = 0;
while (true) {
if (cur > l.front()) {
wins++;
if (wins == k) {
cout << cur;
return 0;
}
int val = l.front();
l.pop_front();
l.push_back(val);
} else {
wins = 1;
l.push_back(cur);
cur = l.front();
l.pop_front();
if (wins == k) {
cout << cur;
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 queue import Queue
def find_winner(q, n, k):
p1 = q.get()
c = 0
while c < k:
p2 = q.get()
while c < k and p1 > p2:
c += 1
q.put(p2)
p2 = q.get()
if c >= k:
return p1
else:
q.put(p1)
p1 = p2
c = 1
n, k = [int(num) for num in input().strip().split()]
q = Queue()
m = 0
for val in input().strip().split():
m = max(m, int(val))
q.put(int(val))
if n < k:
print(m)
else:
print(find_winner(q, n, min(n, k)))
| PYTHON3 |
879_B. Table Tennis | n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner.
For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner.
Input
The first line contains two integers: n and k (2 β€ n β€ 500, 2 β€ k β€ 1012) β the number of people and the number of wins.
The second line contains n integers a1, a2, ..., an (1 β€ ai β€ n) β powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct.
Output
Output a single integer β power of the winner.
Examples
Input
2 2
1 2
Output
2
Input
4 2
3 1 2 4
Output
3
Input
6 2
6 5 3 1 2 4
Output
6
Input
2 10000000000
2 1
Output
2
Note
Games in the second sample:
3 plays with 1. 3 wins. 1 goes to the end of the line.
3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner. | 2 | 8 |
pg = input().split()
p = int(pg[0])
g = int(pg[1])
l = list(map(int, input().rstrip().split()))
a = 0
count = 0
while count != g:
a = int(l[0])
b = int(l[1])
if (count > p):
if (a > b):
count = g
elif (a < b):
a = b
count = g
else:
if (a > b):
count += 1
l.append(b)
l.pop(1)
elif (a < b):
l.pop(0)
l.append(a)
count = 1
print(a)
| PYTHON3 |
879_B. Table Tennis | n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner.
For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner.
Input
The first line contains two integers: n and k (2 β€ n β€ 500, 2 β€ k β€ 1012) β the number of people and the number of wins.
The second line contains n integers a1, a2, ..., an (1 β€ ai β€ n) β powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct.
Output
Output a single integer β power of the winner.
Examples
Input
2 2
1 2
Output
2
Input
4 2
3 1 2 4
Output
3
Input
6 2
6 5 3 1 2 4
Output
6
Input
2 10000000000
2 1
Output
2
Note
Games in the second sample:
3 plays with 1. 3 wins. 1 goes to the end of the line.
3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner. | 2 | 8 | n,k = map(int, raw_input().strip().split(' '))
a = map(int, raw_input().strip().split(' '))
k1=[]
x=0
w=0
x1=1
for i in range(1000):
k1.append(0)
if k>=(n):
print max(a)
else:
while k not in k1:
if a[0]>a[1]:
fa=a[1]
a.remove(a[1])
a.append(fa)
k1[a[0]]=k1[a[0]]+1
elif a[1]>a[0]:
fa=a[0]
a.remove(a[0])
a.append(fa)
k1[a[0]]=k1[a[0]]+1
for j in range(len(k1)):
if k1[j]==k:
print j
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.*;
import java.util.*;
import java.math.BigInteger;
import java.util.Map.Entry;
import static java.lang.Math.*;
public class B extends PrintWriter {
int solve(int n, long k, int[] a) {
int w = 0, id = 0;
for (int i = 1; w < k && i < n; i++, w++) {
if (a[i] > a[id]) {
id = i;
w = 0;
}
}
return a[id];
}
void run() {
int n = nextInt();
long k = nextLong();
int[] a = nextArray(n);
println(solve(n, k, a));
}
boolean skip() {
while (hasNext()) {
next();
}
return true;
}
int[][] nextMatrix(int n, int m) {
int[][] matrix = new int[n][m];
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
matrix[i][j] = nextInt();
return matrix;
}
String next() {
while (!tokenizer.hasMoreTokens())
tokenizer = new StringTokenizer(nextLine());
return tokenizer.nextToken();
}
boolean hasNext() {
while (!tokenizer.hasMoreTokens()) {
String line = nextLine();
if (line == null) {
return false;
}
tokenizer = new StringTokenizer(line);
}
return true;
}
int[] nextArray(int n) {
int[] array = new int[n];
for (int i = 0; i < n; i++) {
array[i] = nextInt();
}
return array;
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
try {
return reader.readLine();
} catch (IOException err) {
return null;
}
}
public B(OutputStream outputStream) {
super(outputStream);
}
static BufferedReader reader;
static StringTokenizer tokenizer = new StringTokenizer("");
static Random rnd = new Random();
static boolean OJ;
public static void main(String[] args) throws IOException {
OJ = System.getProperty("ONLINE_JUDGE") != null;
B solution = new B(System.out);
if (OJ) {
reader = new BufferedReader(new InputStreamReader(System.in));
solution.run();
} else {
reader = new BufferedReader(new FileReader(new File(B.class.getName() + ".txt")));
long timeout = System.currentTimeMillis();
while (solution.hasNext()) {
solution.run();
solution.println();
solution.println("----------------------------------");
}
solution.println("time: " + (System.currentTimeMillis() - timeout));
}
solution.close();
reader.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 | #include <bits/stdc++.h>
using namespace std;
int main() {
int n, k;
scanf("%d %d", &n, &k);
int count = 0;
queue<int> q;
int i = 0, x, y;
for (i = 0; i < n; i++) {
scanf("%d", &x);
q.push(x);
}
x = q.front();
q.pop();
while (count < k && count != n) {
y = q.front();
q.pop();
if (x > y) {
count++;
q.push(y);
} else {
count = 1;
q.push(x);
x = y;
}
}
cout << x;
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, m=map(int, raw_input().split())
a=map(int, raw_input().split())
x=a[0]; k=0; t=0
if m<n:
while x>=a[t]:
if t<>0:
a.append(a[t])
k+=1
if k==m:
break
if x<a[t+1]:
x=a[t+1]
k=0
a.append(x)
t+=1
print x
else:
print max(a)
| PYTHON |
879_B. Table Tennis | n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner.
For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner.
Input
The first line contains two integers: n and k (2 β€ n β€ 500, 2 β€ k β€ 1012) β the number of people and the number of wins.
The second line contains n integers a1, a2, ..., an (1 β€ ai β€ n) β powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct.
Output
Output a single integer β power of the winner.
Examples
Input
2 2
1 2
Output
2
Input
4 2
3 1 2 4
Output
3
Input
6 2
6 5 3 1 2 4
Output
6
Input
2 10000000000
2 1
Output
2
Note
Games in the second sample:
3 plays with 1. 3 wins. 1 goes to the end of the line.
3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner. | 2 | 8 | from collections import deque
n, k = map(int, input().split())
if k > 5*n:
k = 5*n
a = deque(reversed(list(map(int, input().split()))))
# print(a)
cnt = 0
while True:
if a[-1] < a[-2]:
cnt = 1
a.appendleft(a.pop())
else:
cnt += 1
x, y = a.pop(), a.pop()
a.appendleft(y)
a.append(x)
if cnt >= k:
break
print(a[-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 | from collections import deque
nums = input().split(' ')
powers = input().split(' ')
n = int(nums[0])
k = int(nums[1])
queue = []
for i in range(n):
queue.append(int(powers[i]))
ans = queue[0]
s = 0
for j in range(1,n):
if s >= k:
#print(ans)
break
elif ans > queue[j]:
s = s + 1
else:
s = 1
ans = queue[j]
print(ans)
| PYTHON3 |
879_B. Table Tennis | n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner.
For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner.
Input
The first line contains two integers: n and k (2 β€ n β€ 500, 2 β€ k β€ 1012) β the number of people and the number of wins.
The second line contains n integers a1, a2, ..., an (1 β€ ai β€ n) β powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct.
Output
Output a single integer β power of the winner.
Examples
Input
2 2
1 2
Output
2
Input
4 2
3 1 2 4
Output
3
Input
6 2
6 5 3 1 2 4
Output
6
Input
2 10000000000
2 1
Output
2
Note
Games in the second sample:
3 plays with 1. 3 wins. 1 goes to the end of the line.
3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner. | 2 | 8 | n,k=map(int,input().split())
l=list(map(int,input().split()))
prev=l[0]
c=0
if k>=(n-1):
print(max(l))
else:
while(c<k):
if(l[0]>l[1] and prev==l[0]):
lost=l.pop(1)
c=c+1
l.append(lost)
elif(l[0]<l[1] and prev==l[0]):
prev=l[1]
lost=l.pop(0)
c=1
l.append(lost)
print(prev)
| 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 TableTennic {
void solve()
{
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
long k = sc.nextLong();
long a[] = new long[n];
long p[] = new long[n];
for(int i=0;i<n;i++)
a[i] = sc.nextLong();
long m = a[0],max = a[0];
int index = 0;
boolean f = false;
for(int i=1;i<n;i++)
{
if(a[i]>m)
{
m = a[i];
index = i;
}
p[index]++;
if(p[index]==k)
{
f = true;
System.out.println(m);
break;
}
max = Math.max(a[i],max);
}
System.out.println(f==false?max:"");
}
public static void main(String[] args) {
new TableTennic().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 | # n = number of people
# k = number of wins in a row
# p = power of player
n, k = map(int, input().split())
p = list(map(int, input().split()))
current_player_power = p[0]
current_player_wins = 0
for i in range(1, n):
if p[i] < current_player_power:
current_player_wins +=1
else:
current_player_power = p[i]
current_player_wins =1
if current_player_wins == k:
break
print(current_player_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;
int n, a[505], cnt[505];
long long k;
int main() {
cin >> n >> k;
for (int i = 0; i < n; i++) cin >> a[i];
int mx = a[0], mxi = 0;
for (int i = 1; i < n; i++) {
if (a[i] > mx) mxi = i, mx = a[i];
if (++cnt[mxi] == k) return cout << mx, 0;
}
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 | n,k=[int(ii) for ii in raw_input().split(" ")]
a=[int(ii) for ii in raw_input().split(" ")]
if k>=n:
print n #[i for i in range(n) if a[i]==n][0]+1
else:
nbw=0
toplay=0
chall=1
while nbw<=k:
if a[toplay]==n or nbw==k:
print a[toplay]
break
if a[toplay]>a[chall]:
nbw+=1
else:
toplay=chall
nbw=1
chall+=1 | PYTHON |
879_B. Table Tennis | n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner.
For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner.
Input
The first line contains two integers: n and k (2 β€ n β€ 500, 2 β€ k β€ 1012) β the number of people and the number of wins.
The second line contains n integers a1, a2, ..., an (1 β€ ai β€ n) β powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct.
Output
Output a single integer β power of the winner.
Examples
Input
2 2
1 2
Output
2
Input
4 2
3 1 2 4
Output
3
Input
6 2
6 5 3 1 2 4
Output
6
Input
2 10000000000
2 1
Output
2
Note
Games in the second sample:
3 plays with 1. 3 wins. 1 goes to the end of the line.
3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner. | 2 | 8 | n,k=map(int,input().split())
b=[0]*501
a=list(map(int,input().split()))
if k>=n-1:
print(max(a))
exit()
else:
while max(b)!=k:
if a[0]>a[1]:
a.append(a[1])
del a[1]
b[a[0]]+=1
else:
a.append(a[0])
a[0]=a[1]
del a[1]
b[a[0]]+=1
print(b.index(max(b))) | PYTHON3 |
879_B. Table Tennis | n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner.
For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner.
Input
The first line contains two integers: n and k (2 β€ n β€ 500, 2 β€ k β€ 1012) β the number of people and the number of wins.
The second line contains n integers a1, a2, ..., an (1 β€ ai β€ n) β powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct.
Output
Output a single integer β power of the winner.
Examples
Input
2 2
1 2
Output
2
Input
4 2
3 1 2 4
Output
3
Input
6 2
6 5 3 1 2 4
Output
6
Input
2 10000000000
2 1
Output
2
Note
Games in the second sample:
3 plays with 1. 3 wins. 1 goes to the end of the line.
3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner. | 2 | 8 | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author toshif
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
MyReader in = new MyReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskB solver = new TaskB();
solver.solve(1, in, out);
out.close();
}
static class TaskB {
public void solve(int testNumber, MyReader in, PrintWriter out) {
int n = in.nextInt();
long k = in.nextLong();
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = in.nextInt();
}
if (k >= n - 1) {
out.println(n);
return;
}
int win = 0;
int idx = 1;
int p = a[0];
while (true) {
idx %= n;
if (p > a[idx]) {
win++;
idx++;
} else {
win = 1;
p = a[idx];
idx++;
}
if (win == k) {
out.println(p);
return;
}
}
}
}
static class MyReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public MyReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
}
}
| JAVA |
879_B. Table Tennis | n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner.
For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner.
Input
The first line contains two integers: n and k (2 β€ n β€ 500, 2 β€ k β€ 1012) β the number of people and the number of wins.
The second line contains n integers a1, a2, ..., an (1 β€ ai β€ n) β powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct.
Output
Output a single integer β power of the winner.
Examples
Input
2 2
1 2
Output
2
Input
4 2
3 1 2 4
Output
3
Input
6 2
6 5 3 1 2 4
Output
6
Input
2 10000000000
2 1
Output
2
Note
Games in the second sample:
3 plays with 1. 3 wins. 1 goes to the end of the line.
3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner. | 2 | 8 | z,zz=input,lambda:list(map(int,z().split()))
fast=lambda:stdin.readline().strip()
zzz=lambda:[int(i) for i in fast().split()]
szz,graph,mod,szzz=lambda:sorted(zz()),{},10**9+7,lambda:sorted(zzz())
from string import *
from re import *
from collections import *
from queue import *
from sys import *
from collections import *
from math import *
from heapq import *
from itertools import *
from bisect import *
from collections import Counter as cc
from math import factorial as f
from bisect import bisect as bs
from bisect import bisect_left as bsl
from itertools import accumulate as ac
def lcd(xnum1,xnum2):return (xnum1*xnum2//gcd(xnum1,xnum2))
def prime(x):
p=ceil(x**.5)+1
for i in range(2,p):
if (x%i==0 and x!=2) or x==0:return 0
return 1
def dfs(u,visit,graph):
visit[u]=1
for i in graph[u]:
if not visit[i]:
dfs(i,visit,graph)
###########################---Test-Case---#################################
"""
"""
###########################---START-CODING---##############################
quee=deque()
n,k=zzz()
quee.extend(zzz())
cnt=0
maxper=max(list(quee))
last_per=-1
while True:
u=quee.popleft()
v=quee.popleft()
loser=min(u,v)
win=max(u,v)
if win==last_per:
cnt+=1
else:
cnt=1
last_per=win
if cnt==k:
break
if win==maxper:
break
quee.append(loser)
quee.appendleft(win)
print(last_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 | n,k=map(int,input().split())
a=list(map(int,input().split()))
c=[]
b=[]
for j in range(n):
c.append(0)
for s in a:
b.append(s)
if(k>=n):
print(max(a))
else:
wins=0
while(wins<k):
if a[0]>a[1]:
c[b.index(a[0])]=c[b.index(a[0])]+1
a.append(a[1])
a.remove(a[1])
else:
c[b.index(a[1])]=c[b.index(a[1])]+1
a.append(a[0])
a.remove(a[0])
wins=max(c)
for alp in c:
if(alp==k):
print(b[c.index(alp)]) | 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()))
ans={}
maxx=l[0]
i=1
count=0
if(n<=k):
print(max(l))
else:
while(1):
if(maxx>l[1]):
ans[maxx]=ans.get(maxx,0)+1
if(ans[maxx]==k):
print(maxx)
break
l.append(l.pop(1))
else:
l.append(l.pop(0))
maxx=l[0]
ans[maxx]=ans.get(maxx,0)+1
if(ans[maxx]==k):
print(maxx)
break
i+=1
#print(ans) | PYTHON3 |
879_B. Table Tennis | n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner.
For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner.
Input
The first line contains two integers: n and k (2 β€ n β€ 500, 2 β€ k β€ 1012) β the number of people and the number of wins.
The second line contains n integers a1, a2, ..., an (1 β€ ai β€ n) β powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct.
Output
Output a single integer β power of the winner.
Examples
Input
2 2
1 2
Output
2
Input
4 2
3 1 2 4
Output
3
Input
6 2
6 5 3 1 2 4
Output
6
Input
2 10000000000
2 1
Output
2
Note
Games in the second sample:
3 plays with 1. 3 wins. 1 goes to the end of the line.
3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner. | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int main() {
int n, i, count, j, temp;
vector<int> arr;
long long int k;
scanf("%d %lld", &n, &k);
for (i = 0; i < n; i++) {
scanf("%d", &temp);
arr.push_back(temp);
}
count = 0;
if (k >= n) {
for (i = 0; i < n; i++) {
if (arr[i] > count) count = arr[i];
}
printf("%d", count);
} else {
count = 0;
while (true) {
while (arr[0] > arr[1]) {
temp = arr[1];
arr.erase(arr.begin() + 1);
arr.push_back(temp);
count++;
if (count >= k) break;
}
if (count >= k) {
printf("%d", arr[0]);
break;
} else {
count = 1;
temp = arr[0];
arr.erase(arr.begin());
arr.push_back(temp);
}
}
}
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;
inline long long bigmod(long long p, long long e, long long M) {
long long ret = 1;
for (; e > 0; e >>= 1) {
if (e & 1) ret = (ret * p) % M;
p = (p * p) % M;
}
return ret;
}
inline int read() {
int x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
inline long long inversemod(long long a, long long M) {
return bigmod(a, M - 2, M);
}
inline long long gcd(long long a, long long b) {
while (b > 0) {
a = a % b;
a ^= b;
b ^= a;
a ^= b;
;
}
return a;
}
inline long long lcm(long long m, long long n) { return m * n / gcd(m, n); }
void Main() {
long long n, k, arr[555], A, B, winner, prev_winner;
cin >> n >> k;
long long mx = 0;
deque<long long> deq;
for (int i = 0; i < n; i++) {
cin >> arr[i];
deq.push_back(arr[i]);
mx = max(mx, arr[i]);
}
if (k >= n - 1)
cout << mx;
else {
A = deq.front();
deq.pop_front();
B = deq.front();
deq.pop_front();
if (A > B) {
winner = A;
deq.push_front(A);
deq.push_back(B);
} else {
winner = B;
deq.push_front(B);
deq.push_back(A);
}
prev_winner = winner;
for (long long wins = 1; wins < k; wins++) {
A = deq.front();
deq.pop_front();
B = deq.front();
deq.pop_front();
if (A > B) {
winner = A;
deq.push_front(A);
deq.push_back(B);
} else {
winner = B;
deq.push_front(B);
deq.push_back(A);
}
if (prev_winner != winner) wins = 0;
prev_winner = winner;
}
cout << winner;
}
}
int main() {
Main();
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;
vector<long long> win;
queue<long long> q;
signed main() {
ios_base::sync_with_stdio(0);
cout.precision(20);
long long n, k;
cin >> n >> k;
for (long long i = 0; i < n; ++i) {
long long pw;
cin >> pw;
win.push_back(pw);
}
for (long long i = 0; i < n; ++i) {
q.push(win[i]);
}
long long now = 1, lead;
long long f = q.front();
q.pop();
long long s = q.front();
q.pop();
if (f > s) {
lead = f;
q.push(s);
} else {
lead = s;
q.push(f);
}
if (now == k) {
cout << lead << endl;
return 0;
}
while (now < k) {
if (lead == n) {
cout << lead << endl;
return 0;
}
long long contest = q.front();
q.pop();
if (contest > lead) {
swap(lead, contest);
now = 0;
}
q.push(contest);
++now;
}
cout << lead << 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 | #!/usr/bin/env python
#-*- coding: utf-8 -*-
from collections import defaultdict
from math import factorial as f
from fractions import gcd as g
from collections import deque
N, K = [int (i) for i in raw_input ().split ()]
l = [int (i) for i in raw_input ().split ()]
d = defaultdict (int)
dq = deque ()
dq2 = deque ()
for i in l:
dq.append (i)
dq2.append (i)
ok = False
for i in range (10 ** 6):
x = dq.popleft ()
y = dq.popleft ()
if x > y:
d [x] += 1
dq.append (y)
dq.appendleft (x)
else:
d [y] += 1
dq.append (x)
dq.appendleft (y)
if d [x] >= K:
print x
ok = True
break
if d [y] >= K:
print x
ok = True
break
if not ok:
m = max (d.values ())
for i in d:
if d [i] == m:
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())
z = k
a = list(map(int,input().split()))
if(n-1<=k):
print(max(a))
else:
while(k>0):
if(a[0]>a[1]):
a[0],a[1]=a[1],a[0]
l = a[0]
a.pop(0)
a.append(l)
k-=1
else:
l = a[0]
a.pop(0)
a.append(l)
k = z-1
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 | #include <bits/stdc++.h>
using namespace std;
const int delta = (int)1e9 + 7;
long long n, k, a[(int)1e3 + 20], ans, m[(int)1e3 + 20], x = 1, l = 1, r;
int ac() {
if (a[0] == m[k]) return a[0];
for (int i = 1; i + k - 1 < n; ++i) {
if (a[i] == m[i + k - 1]) return a[i];
}
return n;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n >> k;
for (int i = 0; i < n; ++i) {
cin >> a[i];
if (i)
m[i] = max(m[i - 1], a[i]);
else
m[i] = a[i];
}
if (k >= n)
cout << n;
else
cout << ac();
return cout << endl, 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 | # -*- coding:utf-8 -*-
#[n, m] = [int(x) for x in raw_input().split()]
def some_func():
"""
"""
n,k = [int(x) for x in raw_input().split()]
n_list = [int(x) for x in raw_input().split()]
i = 0
temp = n_list[i]
cout = 0
while True:
i+=1
if i==n:
break
if cout==k:
break
if n_list[i]>temp:
temp=n_list[i]
cout=1
else:
cout+=1
return temp
if __name__ == '__main__':
print some_func()
| 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 | def pingpong():
n,k = map(int,input().split())
powers = list(map(int,input().split()))
champ = powers[0]
winCount = 0
for i in range(1,n):
if powers[i] < champ:
winCount += 1
else:
champ = powers[i]
winCount = 1
if winCount==k:
break
print(champ)
pingpong()
| 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 = list(map(int, input().rstrip().split()))
player = list(map(int, input().rstrip().split()))
win = 0
x = player.pop(0)
def findWinner(x,player,win):
if a[0]==2:
if x > player[0]:
return x
else:
return player[0]
elif a[0] >= 20 and player == sorted(player):
return player[len(player)-1]
elif a[0] >= 20 and player == sorted(player, reverse=True):
if x > player[0]:
return x
else:
return player[0]
else:
if x > player[0]:
win+=1
if win == a[1] or win == a[0]+1:
return x
else:
player.append(player.pop(0))
return findWinner(x,player,win)
else:
win=1
player.append(x)
x = player.pop(0)
return findWinner(x,player,win)
print(findWinner(x,player,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 | min_wins = int(input().split(" ")[1])
players = input().split(" ")
sitter = int(players[0])
players.pop(0)
wins = 0
repeat_check = int(players[0])
while wins < min_wins:
head = int(players[0])
if sitter > head:
wins+=1
if wins == min_wins:
print(sitter)
quit()
players.pop(0)
players.append(head)
if players[0] == repeat_check:
print(sitter)
quit()
else:
wins = 1
loser = sitter
sitter = head
repeat_check = loser
players.pop(0)
players.append(loser)
| PYTHON3 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.