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.awt.List; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.ArrayDeque; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.InputMismatchException; import java.util.LinkedList; import java.util.Queue; import java.util.Scanner; public class Tester { InputStream is; PrintWriter out; String INPUT = ""; void solve() { int n=ni(); long k=nl(); int[] a=na(n); ArrayDeque<Integer> aq=new ArrayDeque<>(); for(int i:a) aq.add(i); int max=aq.peek(); aq.poll(); int cnt=0; for(int i=1;i<n;i++) { int t=aq.peek(); if(max>t) { cnt++; aq.add(aq.poll()); if(cnt==k) { out.println(max); return; } } else { aq.add(max); max=aq.poll(); cnt=1; } } max=a[0]; for(int i=1;i<n;i++) if(max<a[i]) max=a[i]; out.println(max); } void run() throws Exception { is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes()); out = new PrintWriter(System.out); long s = System.currentTimeMillis(); solve(); out.flush(); if(!INPUT.isEmpty())tr(System.currentTimeMillis()-s+"ms"); } public static void main(String[] args) throws Exception { new Tester().run(); } private byte[] inbuf = new byte[1024]; public int lenbuf = 0, ptrbuf = 0; private int readByte() { if(lenbuf == -1)throw new InputMismatchException(); if(ptrbuf >= lenbuf) { ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if(lenbuf <= 0)return -1; } return inbuf[ptrbuf++]; } private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; } private double nd() { return Double.parseDouble(ns()); } private char nc() { return (char)skip(); } private String ns() { int b = skip(); StringBuilder sb = new StringBuilder(); while(!(isSpaceChar(b))) { // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } private char[] ns(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while(p < n && !(isSpaceChar(b))) { buf[p++] = (char)b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } private char[][] nm(int n, int m) { char[][] map = new char[n][]; for(int i = 0;i < n;i++)map[i] = ns(m); return map; } private int[] na(int n) { int[] a = new int[n]; for(int i = 0;i < n;i++)a[i] = ni(); return a; } private int ni() { int num = 0, b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-') { minus = true; b = readByte(); } while(true) { if(b >= '0' && b <= '9') { num = num * 10 + (b - '0'); }else { return minus ? -num : num; } b = readByte(); } } private long nl() { long num = 0; int b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-') { minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9') { num = num * 10 + (b - '0'); }else { return minus ? -num : num; } b = readByte(); } } private static void tr(Object... o) { System.out.println(Arrays.deepToString(o)); } }
JAVA
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
n, k = map(int, raw_input().split()) b, c = 0, 0 for v in map(int, raw_input().split()): if b > 0: c = [1, c + 1][b > v] b = max(b, v) if c == k: break print b
PYTHON
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; long long k; cin >> k; if (n <= k) { cout << n << endl; } else { queue<int> q; for (int i = 0; i < n; i++) { int a; cin >> a; q.push(a); } int cur = q.front(), c = 0; q.pop(); while (c < k) { int next = q.front(); q.pop(); if (cur > next) { c++; q.push(next); } else { c = 1; q.push(cur); cur = next; } } cout << cur << 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
n, k = map(int, raw_input().split(' ')) power = map(int, raw_input().split(' ')) if k > n: print max(power) else: MaxSoFar = max(power[0], power[1]) OldMax = MaxSoFar counter = 1 for j in range(2,n): if MaxSoFar > power[j]: counter += 1 if counter == k: break else: MaxSoFar = power[j] counter = 1 print MaxSoFar
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
# table tennis from collections import deque def TableTennis(pw, power): wins = 0 power = deque(power) p1 = power.popleft() p2 = power[0] if len(power) == 1: if p1 > p2: print(p1) else: print(p2) else: while wins != pw[1]: if pw[1] > 500 and wins > 500 : break if p1 > p2: p2 = power.popleft() power.append(p2) p2 = power[0] wins += 1 else: power.append(p1) p2 = power.popleft() p1 = p2 p2 = power[0] wins = 1 print(p1) pw = [int(x) for x in input().split()] power = [int(x) for x in input().split()] TableTennis(pw,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
n, k = map(int, input().split()) x = list(map(int, input().split())) flag = 0 cnt = k while flag != n and cnt != 0: if x[0] > x[1]: x.append(x[1]) del x[1] cnt -= 1 flag += 1 elif x[0] < x[1]: x.append(x[0]) del x[0] cnt = k - 1 flag = 0 print(x[0])
PYTHON3
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
from copy import copy from collections import deque in_1 = input().split() n = int(in_1[0]) k = int(in_1[1]) in_2 = input().split() current_player = int(in_2[0]) current_opponent = int(in_2[1]) enemy_q = deque() current_wins = 0 if n > 2: # Add all other enemies to the enemy queue (line) for i in in_2[2:]: enemy_q.appendleft(int(i)) # Keep playing until a player wins k times while current_wins < k: # if current player wins, add current opponent to enemy queue, increase # win count by 1, and get next in line opponent as current opponent if current_player > current_opponent: enemy_q.appendleft(current_opponent) current_wins += 1 current_opponent = enemy_q.pop() # for time limits: if opponent has beaten everyone already, stop the loop # and return the current player as the inevitable winner if current_wins > n - 1: break # if opponent wins, add current player to enemy queue, convert current opponent # to current player, change win count to one, and get next in line opponent as current opponent else: winner = copy(current_opponent) enemy_q.appendleft(current_player) current_player = winner current_wins = 1 current_opponent = enemy_q.pop() # if there are only two players, the higher power player will automatically win. else: if current_player<current_opponent: current_player = current_opponent print(current_player)
PYTHON3
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
#include <bits/stdc++.h> using namespace std; long long n, k; deque<int> ciag; int ileWygryw[501]; inline int symuluj() { while (true) { if (ciag[0] > ciag[1]) swap(ciag[0], ciag[1]); ciag.push_back(ciag.front()); ciag.pop_front(); ileWygryw[ciag.front()]++; for (int i = 0; i <= 500; i++) { if (ileWygryw[i] == k) { return i; } } } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> k; int maks = 0; for (int i = 0; i < n; i++) { int pow; cin >> pow; maks = max(maks, pow); ciag.push_back(pow); } if (k >= n) { cout << maks << '\n'; } else { cout << symuluj() << '\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
import java.util.*; public class TestClass { public static void main(String args[] ) throws Exception { Scanner s1=new Scanner(System.in); int n=s1.nextInt(); long k=s1.nextLong(); if(n==2) { int n1=s1.nextInt(); int m=s1.nextInt(); if(n1>m) System.out.println(n1); else System.out.println(m); return; } if(k>n) { int a[]=new int[n],max=0; for(int i=0;i<n;i++) { a[i]=s1.nextInt(); if(max<a[i]) max=a[i]; } System.out.println(max); } else { LinkedList<Integer> al=new LinkedList<Integer>(); HashMap<Integer,Long> mp=new HashMap<Integer,Long>(); for(int i=0;i<n;i++) al.add(s1.nextInt()); int index=0; int a[]=new int[n+1]; while(true) { int i=al.get(0); int j=al.get(1); int indexy=0; int target=i; int winner=j; if(j<i) { indexy=1; target=j; winner=i; } al.remove(indexy); al.add(target); if(mp.containsKey(new Integer(winner))) { long score=mp.get(winner); mp.put(winner,++score); } else mp.put(winner,1l); long max_freq=0; index=0; for (Integer key : mp.keySet()) { Long value = mp.get(key); if(max_freq<=value && index<key) { max_freq=value; index=key; } } if(max_freq==k) break; } System.out.println(index); } } }
JAVA
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
#include <bits/stdc++.h> using namespace std; long long a, b, k, n, w; int main() { cin >> n >> k >> a; for (int i = 1; i < n && a != n; ++i) { cin >> b; if (a < b) { a = b; w = 0; } ++w; if (w == k) { cout << a << endl; return 0; } } cout << n << 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
#http://codeforces.com/problemset/problem/879/B from collections import deque inp = lambda: map(int, input().split()) n, k = inp() player = deque(list(inp())) win = 0 while win < k: if player[0] > player[1]: win += 1 winner = player.popleft() loser = player.popleft() player.appendleft(winner) player.append(loser) else: win = 1 loser = player.popleft() player.append(loser) if win > 1000: break print(player[0])
PYTHON3
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
n, k = map(int, input().split()) l = list(map(int, input().split())) wi1 = 0 wi2 = 0 #print(l) if(k > n): print(max(l)) else: w = 0 while wi1 < k and wi2 < k: #print(l[0], l[1], l) if l[0] > l[1]: wi1 += 1 t = l[1] l.remove(l[1]) l.append(t) wi2 = 0 else: wi2 += 1 t = l[0] l.remove(l[0]) l.append(t) wi1 = wi2 wi2 = 0 print(l[0]) if wi1 == k else print(l[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 queue n,k = map(int,input().split()) lista = list(map(int,input().split())) p1 = lista[0] p2 = lista[1] q = queue.Queue() for i in range(2,len(lista)): q.put(lista[i]) cnt = 0 while True: if p1 > p2: cnt+=1 q.put(p2) else: q.put(p1) p1 = p2 cnt = 1 p2 = q.get() if cnt == k or cnt >= n: print(p1) 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.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.PrintWriter; import java.util.ArrayDeque; import java.util.Arrays; import java.util.Deque; import java.util.StringTokenizer; public class A443 { public static void main(String args[]) throws FileNotFoundException { InputReader in = new InputReader(System.in); OutputStream outputStream = System.out; PrintWriter out = new PrintWriter(outputStream); StringBuilder sb = new StringBuilder(); // ----------My Code---------- int n = in.nextInt(); long k = in.nextLong(); Deque<Integer> q=new ArrayDeque<>(); int max=0; for(int i=0;i<n;i++){ int a=in.nextInt(); max=Math.max(a, max); q.addLast(a); } if(k>=n) System.out.println(max); else{ int val=0,ans=-1; while(true){ int x=q.remove(); int y=q.remove(); if(x>y){ val++; q.addFirst(x); q.addLast(y); }else{ val=1; q.addFirst(y); q.addLast(x); } if(val==k){ ans=Math.max(x, y); break; } } System.out.println(ans); } // ---------------The End------------------ out.close(); } static boolean isPossible(int x, int y) { if (x >= 0 && y >= 0 && x < 4 && y < 4) return true; return false; } // ---------------Extra Methods------------------ public static long pow(long x, long n, long mod) { long res = 1; x %= mod; while (n > 0) { if (n % 2 == 1) { res = (res * x) % mod; } x = (x * x) % mod; n /= 2; } return res; } public static boolean isPal(String s) { for (int i = 0, j = s.length() - 1; i <= j; i++, j--) { if (s.charAt(i) != s.charAt(j)) return false; } return true; } public static String rev(String s) { StringBuilder sb = new StringBuilder(s); sb.reverse(); return sb.toString(); } public static long gcd(long x, long y) { if (x % y == 0) return y; else return gcd(y, x % y); } public static int gcd(int x, int y) { if (x % y == 0) return y; else return gcd(y, x % y); } public static long gcdExtended(long a, long b, long[] x) { if (a == 0) { x[0] = 0; x[1] = 1; return b; } long[] y = new long[2]; long gcd = gcdExtended(b % a, a, y); x[0] = y[1] - (b / a) * y[0]; x[1] = y[0]; return gcd; } public static int abs(int a, int b) { return (int) Math.abs(a - b); } public static long abs(long a, long b) { return (long) Math.abs(a - b); } public static int max(int a, int b) { if (a > b) return a; else return b; } public static int min(int a, int b) { if (a > b) return b; else return a; } public static long max(long a, long b) { if (a > b) return a; else return b; } public static long min(long a, long b) { if (a > b) return b; else return a; } // ---------------Extra Methods------------------ public static void debug(Object... o) { System.out.println(Arrays.deepToString(o)); } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream inputstream) { reader = new BufferedReader(new InputStreamReader(inputstream)); tokenizer = null; } public String nextLine() { String fullLine = null; while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { fullLine = reader.readLine(); } catch (IOException e) { throw new RuntimeException(e); } return fullLine; } return fullLine; } 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 char nextChar() { return next().charAt(0); } public long nextLong() { return Long.parseLong(next()); } public int nextInt() { return Integer.parseInt(next()); } public double nextDouble() { return Double.parseDouble(next()); } public int[] nextIntArray(int n, int f) { if (f == 0) { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = nextInt(); } return arr; } else { int[] arr = new int[n + 1]; for (int i = 1; i < n + 1; i++) { arr[i] = nextInt(); } return arr; } } public long[] nextLongArray(int n, int f) { if (f == 0) { long[] arr = new long[n]; for (int i = 0; i < n; i++) { arr[i] = nextLong(); } return arr; } else { long[] arr = new long[n + 1]; for (int i = 1; i < n + 1; i++) { arr[i] = nextLong(); } return arr; } } public double[] nextDoubleArray(int n, int f) { if (f == 0) { double[] arr = new double[n]; for (int i = 0; i < n; i++) { arr[i] = nextDouble(); } return arr; } else { double[] arr = new double[n + 1]; for (int i = 1; i < n + 1; i++) { arr[i] = nextDouble(); } return arr; } } } static class Pair implements Comparable<Pair> { int a, b; Pair(int a, int b) { this.a = a; this.b = b; } public int compareTo(Pair o) { // TODO Auto-generated method stub if (this.a != o.a) return Integer.compare(this.a, o.a); else return Integer.compare(this.b, o.b); } public boolean equals(Object o) { if (o instanceof Pair) { Pair p = (Pair) o; return p.a == a && p.b == b; } return false; } public int hashCode() { return new Integer(a).hashCode() * 31 + new Integer(b).hashCode(); } } }
JAVA
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
read = lambda: tuple(map(int, input().split())) def main(): n, k = read() l = list(read()) ps = {} def add(p): if not p in ps: ps[p] = 1 else: ps[p] += 1 return (ps[p] >= k, p) newl = [] while(len(l) > 1): v1, v2 = l[0], l[1] if v1 > v2: l.pop(1) newl += [v2] addk = add(v1) else: l.pop(0) newl += [v1] addk = add(v2) if addk[0]: return addk[1] return l[0] print(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
n,k=map(int,input().split()) a=list(map(int,input().split())) d,t=a[0],0 for i in range(1,n): if a[i]<d: t+=1 else: d=a[i] t=1 if t==k:break print(d)
PYTHON3
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
n,k=list(map(int,input().split())) a=list(map(int,input().split())) b=a[::-1] p=max(a) score=[0]+[0]*p from collections import * b=deque(b) while score[b[-1]]!=k and b[-1]!=p: x=b.pop() y=b.pop() b.appendleft(min(x,y)) b.append(max(x,y)) score[max(x,y)]+=1 print(b[-1])
PYTHON3
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
#include <bits/stdc++.h> using namespace std; long long n, k, xx, maxx, lastt = -1, recans; deque<long long> q; signed main() { scanf("%I64d%I64d", &n, &k); for (long long i = 1; i <= n; i++) { scanf("%d", &xx); q.push_back(xx); maxx = max(maxx, xx); } if (k > n) { printf("%I64d\n", maxx); return 0; } while (1) { long long t1 = q.front(); q.pop_front(); long long t2 = q.front(); q.pop_front(); if (t1 < t2) swap(t1, t2); q.push_back(t2); q.push_front(t1); if (t1 == lastt) recans++; else recans = 1, lastt = t1; if (recans >= k) { printf("%I64d\n", t1); break; } } }
CPP
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
import java.util.*; import java.util.ArrayList; public class Main{ static class Pair{ int x; int y; Pair(){ } Pair(int x,int y){ this.x=x; this.y=y; } } public static void main(String[] args) { Scanner param = new Scanner(System.in); int n=param.nextInt(); long m=param.nextLong(); int arr[]=new int[n]; int max=0; for(int i=0;i<n;i++){ arr[i]=param.nextInt(); max=Math.max(max,arr[i]); } long count=0; boolean b=false; int c=arr[0]; for(int i=0;i<n;i++){ if(arr[i]==max){ b=true; System.out.println(max); break; } if(c>arr[i+1]){ count++; if(count>=m){ b=true; System.out.println(c); break; } } else{ count=1; c=arr[i+1]; if(count>=m){ b=true; System.out.println(c); break; } } } if(!b){ System.out.println(max); } } }
JAVA
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
n, k = map(int, input().split()) a = list(map(lambda x: [int(x), 0], input().split())) while True: if a[0][0] > a[1][0]: tmp = a[1] a[0][1] += 1 a.remove(tmp) a.append(tmp) else: tmp = a[0] a[1][1] += 1 a.remove(tmp) a.append(tmp) if a[0][1] == k: break if a[0][1] > 10*n: break print(a[0][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()) powers = list(map(int, input().split())) power = powers[0] wins = 0 for i in range(1, n): if power > powers[i]: wins += 1 else: wins = 1 power = powers[i] if wins >= k: break print(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 main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long int n, k; cin >> n >> k; long long int a[n]; for (int i = 0; i < n; ++i) { cin >> a[i]; } long long int ans = a[0], cnt = 0; for (int i = 1; i < n; ++i) { if (a[i] < ans) cnt++; else { cnt = 1; ans = a[i]; } if (cnt == k) break; } 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; const int INF = 1e9; const int mod = 1e9 + 7; const int MX = 1e5 + 5; int main() { ios_base::sync_with_stdio(false); int n, scores[505]; long long k; list<int> a; memset(scores, 0, sizeof(scores)); cin >> n >> k; for (int i = 0; i < n; i++) { int val; cin >> val; a.push_back(val); } for (int x = 0; x < 1000000; x++) { int first = *a.begin(); a.erase(a.begin()); int second = *a.begin(); a.erase(a.begin()); if (first > second) { a.push_front(first); a.push_back(second); scores[first]++; if (scores[first] >= k) { cout << first << endl; return 0; } } else { a.push_front(second); a.push_back(first); scores[second]++; if (scores[second] >= k) { cout << second << endl; return 0; } } } int mx_score = 0; int mx_ind = 0; for (int i = 1; i <= n; i++) { if (scores[i] > mx_score) { mx_score = scores[i]; mx_ind = i; } } cout << mx_ind << endl; return 0; }
CPP
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
import java.io.*; import java.util.*; /** * Created by Artem on 15.09.2017. */ public class Task { private String NAME = ""; private String ONLINE_JUDGE = System.getProperty("ONLINE_JUDGE"); private BufferedReader in; private PrintWriter out; private StringTokenizer tok; public void init() throws FileNotFoundException { if (ONLINE_JUDGE != null) { if (NAME == "") { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); } else { in = new BufferedReader(new FileReader(NAME + ".in")); out = new PrintWriter(NAME + ".out"); } } else { in = new BufferedReader(new FileReader("input.txt")); out = new PrintWriter("output.txt"); } tok = new StringTokenizer(""); } private String readLine() throws IOException { return in.readLine(); } private String readString() throws IOException { if (!tok.hasMoreTokens()) { tok = new StringTokenizer(in.readLine()); } return tok.nextToken(); } public int readInt() throws IOException { return Integer.parseInt(readString()); } public long readLong() throws IOException { return Long.parseLong(readString()); } public double readDouble() throws IOException { return Double.parseDouble(readString()); } public int[] readIntArray(int n) throws IOException { int[] array = new int[n]; for (int i = 0; i < array.length; i++) { array[i] = readInt(); } return array; } private long timeBegin, timeEnd; public static void main(String[] args) throws IOException { new Task().run(); } class Pair<X, Y> implements Comparable<Pair<X, Y>> { X x; Y y; public Pair(X x, Y y) { this.x = x; this.y = y; } X getX() { return x; } Y getY() { return y; } @Override public int compareTo(Pair<X, Y> o) { if (x instanceof Comparable && y instanceof Comparable) { int result = ((Comparable) x).compareTo(o.x); if (result == 0) { return ((Comparable) y).compareTo(o.y); } else { return -result; } } return 0; } } public void run() throws IOException { timeBegin = System.currentTimeMillis(); init(); solve(); if (ONLINE_JUDGE == null) { //time(); } out.close(); } public void time() { timeEnd = System.currentTimeMillis(); out.println(); out.println("Time =" + (timeEnd - timeBegin)); } //YOUR SOLVE//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// public void solve() throws IOException { int n = readInt(); long k = readLong(); if ((long)n >= k) { int count = 0; ArrayDeque<Integer> ad = new ArrayDeque<>(); for (int i = 0; i < n; i++) { ad.add(readInt()); } int current = ad.pollFirst(); while (count != k) { int con = ad.pollFirst(); if(current > con){ ad.addLast(con); count++; }else{ count = 1; ad.addLast(current); current = con; } } out.print(current); } else { int max = 0; for (int i = 0; i < n; i++) { max = Math.max(max, readInt()); } out.print(max); } } }
JAVA
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
n, k = map(int, input().split()) p = list(map(int, input().split())) current = p[0] j = 0 for i in range(1,n): if p[i] < current: j += 1 else: current = p[i] j = 1 if j == k: break print(current)
PYTHON3
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); ; long long i, j, k, t, n, x, cur, kount = 0; cin >> n >> k; vector<long long> vec; for (long long i = 0; i < n; i++) { cin >> x; vec.push_back(x); } cur = vec[0]; for (long long i = 0; i < n - 1; i++) { if (cur > vec[i + 1]) { kount++; if (kount == k) break; } else { kount = 1; cur = vec[i + 1]; } } cout << cur; }
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, k; long long arr[505]; vector<long long> v; int main() { scanf("%lld %lld", &n, &k); for (int x = 1; x <= n; x++) { scanf("%lld", &arr[x]); v.push_back(arr[x]); } if (k >= n) printf("%lld\n", n); else { long long idx = 0; long long ans = -1; long long idx2 = 1; long long now = k; for (int x = 1; x <= 500 * n; x++) { while (now > 0) { if (v[idx] < v[idx2]) { v.push_back(v[idx]); idx = idx2; idx2++; now = k - 1; break; } else { v.push_back(v[idx]); idx2++; now--; } if (now == 0) { ans = v[idx]; break; } } if (ans != -1) break; } printf("%lld\n", ans); } }
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(' ')) l = map(int,raw_input().split(' ')) count = 0 winner = 0 done =0 for i in range(1,n): if(l[winner]<l[i]): winner = i count=1 else: count+=1 if count==k: done = 1 print l[winner] break if done ==0: k = max(l) print 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 sys import stdin def sin(): return stdin.readline() n,k = map(int, sin().split()) p = list(map(int, sin().split(" "))) ans=0 for i in range(n): c=0 for j in range(i+1,n): if p[i]>p[j]: c+=1 else: break if i!=0 and p[i-1]<p[i]: c+=1 if c>=k: ans = p[i] break if ans==0: print(max(p)) else: 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
import re n, k = map(int, raw_input('').split(' ')) str = '' tab = [] str = raw_input('') tab = map(int, str.split(' ')) maks = max(tab) sila = tab[0] if sila == maks: print sila exit(0) ile_k = 0 for x in range(1, n, +1): if tab[x] == maks: print maks exit(0) if tab[x]<sila: ile_k += 1 if ile_k == k: print sila exit(0) else: sila = tab[x] ile_k = 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
#include <bits/stdc++.h> using namespace std; int main() { int n, k, m, i, r; vector<int> a; stack<int> s; cin >> n >> k; if (k >= n) cout << n; else { for (i = 0; i < n; i++) { cin >> r; a.push_back(r); } r = 0; m = 0; for (i = 0; i < n; i++) { if (s.empty()) s.push(a[i]); else { if (s.top() < a[i]) { s.pop(); s.push(a[i]); m = 1; } else m++; } if (m == k) break; } cout << s.top(); } }
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()) person = list(map(int,input().split())) def winner(person,k): winning = 0 maxp = max(person) plaA = person.pop(0) plaB = person.pop(0) while winning < k: if plaA == maxp: break if plaA > plaB: winning +=1 person.append(plaB) else: winning = 1 person.append(plaA) plaA=plaB plaB = person.pop(0) return plaA print(winner(person,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
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.lang.reflect.Array; import java.io.File; import java.io.*; import java.util.*; public class Main { // static final File ip = new File("input.txt"); // static final File op = new File("output.txt"); // static { // try { // System.setOut(new PrintStream(op)); // System.setIn(new FileInputStream(ip)); // } catch (Exception e) { // } // } static long closestInteger(long a, long b) { long c1 = a - (a % b); long c2 = (a + b) - (a % b); if (a - c1 > c2 - a) { return c2+b; } else { return c1+b; } } public static void main(String[] args) { FastReader sc = new FastReader(); int n = sc.nextInt(); long k = sc.nextLong(); ArrayList<Integer> as = new ArrayList<>(); HashMap<Integer,Integer> hm = new HashMap<>(); int max = Integer.MIN_VALUE; for(int i=0;i<n;i++) { as.add(sc.nextInt()); hm.put(as.get(i),0); if(as.get(i)>max) max = as.get(i); } if(k>=n) { System.out.println(max); } else { while(true) { if(hm.get(as.get(0))==k || hm.get(as.get(1))==k) { max = hm.get(as.get(0))==k ? as.get(0) : as.get(1); break; } if(as.get(0)>as.get(1)) { hm.put(as.get(0),hm.getOrDefault(as.get(0), 0)+1); hm.put(as.get(1),0); int temp = as.remove(1); as.add(temp); } else { hm.put(as.get(1),hm.getOrDefault(as.get(1), 0)+1); hm.put(as.get(0),0); int temp = as.remove(0); as.add(temp); } } System.out.println(max); } } static long power(long x, long y, long p) { long res = 1; x = x % p; if (x == 0) return 0; while (y > 0) { if ((y & 1) != 0) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } public static int countSetBits(long number) { int count = 0; while (number > 0) { ++count; number &= number - 1; } return count; } private static <T> void swap(T[] array, int i, int j) { if (i != j) { T tmp = array[i]; array[i] = array[j]; array[j] = tmp; } } private static long getSum(int[] array) { long sum = 0; for (int value : array) { sum += value; } return sum; } private static boolean isPrime(Long x) { if (x < 2) return false; for (long d = 2; d * d <= x; ++d) { if (x % d == 0) return false; } return true; } private static int[] getPrimes(int n) { boolean[] used = new boolean[n + 1]; used[0] = used[1] = true; int size = 0; for (int i = 2; i <= n; ++i) { if (!used[i]) { ++size; for (int j = 2 * i; j <= n; j += i) { used[j] = true; } } } int[] primes = new int[size]; for (int i = 0, cur = 0; i <= n; ++i) { if (!used[i]) { primes[cur++] = i; } } return primes; } private static long lcm(long a, long b) { return a / gcd(a, b) * b; } private static long gcd(long a, long b) { return (a == 0 ? b : gcd(b % a, a)); } static void shuffleArray(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; } } static void shuffleList(ArrayList<Long> arr) { int n = arr.size(); Random rnd = new Random(); for (int i = 0; i < n; ++i) { long tmp = arr.get(i); int randomPos = i + rnd.nextInt(n - i); arr.set(i, arr.get(randomPos)); arr.set(randomPos, tmp); } } static void factorize(long n) { int count = 0; while (!(n % 2 > 0)) { n >>= 1; count++; } if (count > 0) { // System.out.println("2" + " " + count); } for (long i = 3; i <= (long) Math.sqrt(n); i += 2) { count = 0; while (n % i == 0) { count++; n = n / i; } if (count > 0) { // System.out.println(i + " " + count); } } if (n > 2) { // System.out.println(i + " " + count); } } static void shuffleArrayL(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; } } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } public boolean hasNext() { return false; } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
JAVA
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
import java.io.*; import java.util.*; public class Solution2 { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int n = scan.nextInt(); long k = scan.nextLong(); if(k >= n) { int max = 0; for (int i = 0; i < n; i++) { int x = scan.nextInt(); if(x > max ) { max = x; } } System.out.println(max); return; } int[] x = new int[n]; for (int i = 0; i < n; i++) { x[i] = scan.nextInt(); } int ans = 0; for (int i = 0; i < x.length; i++) { int c = 0; if(i > 0) if(x[i] >x[i-1] ) c++; for (int j = 0 ; c < k; j++) { if(x[i] < x[(i + j +1) % n]) { break; } c++; } if (c == k) { System.out.println(x[i]); return; } } } }
JAVA
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
import java.io.*; import java.util.*; public class TableTennis { public static void main(String[] args) throws Exception { FastScanner sc = new FastScanner(System.in); PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); run(sc, out); out.close(); } public static void run(FastScanner sc, PrintWriter out) throws Exception { int N = sc.nextInt(); long K = sc.nextLong(); int max = Integer.MIN_VALUE; LinkedList<Integer> line = new LinkedList<>(); int[] arr = new int[N]; for (int i = 0; i < N; i++) { int power = sc.nextInt(); arr[i] = power; max = Math.max(power, max); if (i > 1) { line.add(power); } } int winner = Math.max(arr[0], arr[1]); int streak = 1; line.add(Integer.min(arr[0], arr[1])); while (winner != max) { if (streak == K) { out.println(winner); return; } int next = line.removeFirst(); if (next < winner) { streak++; line.addLast(next); } else { line.addLast(winner); winner = next; streak = 1; } } out.println(max); } static class FastScanner { final private int BUFFER_SIZE = 1 << 20; private DataInputStream din; private byte[] buffer; private int bufferPointer, bytesRead; public FastScanner(InputStream stream) { din = new DataInputStream(stream); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public FastScanner(String file_name) throws IOException { din = new DataInputStream(new FileInputStream(file_name)); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } String nextLine() throws IOException { int c = read(); while (c != -1 && isEndline(c)) c = read(); if (c == -1) { return null; } StringBuilder res = new StringBuilder(); do { if (c >= 0) { res.appendCodePoint(c); } c = read(); } while (!isEndline(c)); return res.toString(); } boolean isEndline(int c) { return c == '\n' || c == '\r' || c == -1; } String next() throws Exception { int c = readOutSpaces(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public int nextInt() throws IOException { 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() throws IOException { 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() throws IOException { 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; } private void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) buffer[0] = -1; } private byte read() throws IOException { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } private int readOutSpaces() throws IOException { while (true) { if (bufferPointer == bytesRead) fillBuffer(); int c = buffer[bufferPointer++]; if (!isSpaceChar(c)) { return c; } } } public void close() throws IOException { if (din == null) return; din.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; bool cmp2(int a, int b) { return a > b; } int main() { int n; long long k; int ma = 0; list<int> l; cin >> n >> k; for (int i = 0; i < n; i++) { int a; cin >> a; l.push_back(a); ma = ma > a ? ma : a; } if (k > 500) { cout << ma; return 0; } int max = l.front(); l.pop_front(); long long m = 0; while (m < k) { int b = l.front(); l.pop_front(); if (max > b) { m++; l.push_back(b); } else { l.push_back(max); max = b; m = 1; } } cout << max; return 0; }
CPP
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
import java.util.*; public class TabTen { public static void main(String[] args) { Scanner in = new Scanner(System.in); int a = in.nextInt(); long j = in.nextLong(); int b = in.nextInt(); long k = 0; for(int x = 1; x < a; x++) { int c = in.nextInt(); if(c > b) { k = 1; b = c; } else { k++; } if(k >= j) { break; } } System.out.println(b); } }
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; long long k; cin >> n >> k; int a, lar = 0, co = 0; cin >> a; lar = a; for (int i = 1; i < n; i++) { cin >> a; if (lar > a) { co++; } else { lar = a; co = 1; } if (co == k) { cout << lar; return 0; } } cout << lar; }
CPP
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
from sys import stdin,stdout n,k = map(int,stdin.readline().split()) p = list(map(int,stdin.readline().split())) if k >= (n-1): stdout.write(str(max(p))) else: n1 = p[0] del p[0] c = 0 for item in p: if item > n1: n1 = item c = 1 else: c = c+1 if c == k: break stdout.write(str(n1))
PYTHON3
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
a,b=map(int,input().split()) z=list(map(int,input().split())) r=z.index(max(z)) for i in range(r): s=0 if i!=0: if z[i-1]<z[i]:s+=1 for j in range(i+1,r): if z[i]>z[j]:s+=1 else:break if s>=b:exit(print(z[i])) print(max(z))
PYTHON3
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
from queue import Queue n, k = map(int, input().split()) a = list(map(int, input().split())) q = Queue() for i in a: q.put(i) if k > n: print(max(a)) else: best, pontos = q.get(), 0 while True: f = q.get() if best > f: pontos += 1 q.put(f) else: q.put(best) best, pontos = f, 1 if pontos >= k: print(best) break
PYTHON3
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
def pingpong(n, k): power = [int(i) for i in input().split()] wins = 0 for i in range(1, n): if wins >= k: return power[0] elif power[0] > power[i]: wins += 1 else: wins = 1 power[0] = power[i] return power[0] n, k = map(int, input().split()) print(pingpong(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
#include <bits/stdc++.h> using namespace std; static const int MAXN = 1e3 + 10; int a[MAXN]; int main() { int n; long long k; scanf("%d%lld", &n, &k); for (int i = 1; i <= n; ++i) { scanf("%d", &a[i]); } if (k > n) return printf("%d\n", *max_element(a + 1, a + 1 + n)) * 0; int now = a[1], cnt = 0; for (int i = 2; i <= n; ++i) { if (cnt >= k) return printf("%d\n", now) * 0; if (now > a[i]) ++cnt; else cnt = 1, now = a[i]; } printf("%d\n", *max_element(a + 1, a + 1 + n)); }
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 B879 { public static void main(String[] args) { // TODO Auto-generated method stub Scanner in = new Scanner (System.in); Queue <Integer> q = new LinkedList <Integer>(); int n = in.nextInt(); long k = in.nextLong(); int wp; int p1,p2; long count; for(int i=0;i<n;i++) { q.add(in.nextInt()); } //System.out.println(k+" "+n); if(k>n-1) { int max = 0; while(!q.isEmpty()) max = Math.max(max, q.remove()); System.out.println(max); } else { p1 = q.remove(); p2 = q.remove(); wp = Math.max(p1, p2); q.add(Math.min(p1, p2)); count = 1; while(k>count) { p1 = q.remove(); if(p1>wp) { count = 1; wp = p1; q.add(wp); } else { count++; q.add(p1); } //System.out.println("Winner is " + wp); } System.out.println(wp); } } }
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; long long k; cin >> n >> k; queue<int> q; for (int i = 0; i < n; i++) { int x; cin >> x; q.push(x); } int a = q.front(); q.pop(); int b = q.front(); q.pop(); int curr = max(a, b); q.push(min(a, b)); int c = 1; while (c < k && curr < n) { int nd = q.front(); q.pop(); if (curr > nd) { c++; q.push(nd); } else { c = 1; q.push(curr); curr = nd; } } cout << curr; }
CPP
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
from collections import deque def ping_pong(wins, players): p = deque(players) idx = 0 while idx < len(p): win_count = 0 has_fought = [] fight_count = 0 while True: fight_count += 1 # handles edge case if(p[0] > p[1]): if fight_count == 1 and p[0] > p[-1]: win_count += 2 else: win_count += 1 has_fought.append(p[1]) if win_count == wins or (len(has_fought) + 1 == len(p) and max(p) == p[0]): print(p[0]) return else: temp = p[1] p.remove(temp) p.append(temp) else: val = p.popleft() p.append(val) break idx += 1 def main(): first_line = [int(x) for x in input().split(' ')] second_line = [int(x) for x in input().split(' ')] ping_pong(first_line[1], second_line) main()
PYTHON3
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
n, k = map(int, input().split()) a = list(map(int, input().split())) if k >= n - 1: print(n) else: if a[1] > a[0]: index = 1 max_value = a[1] else: index = 0 max_value = a[0] for i in range(2, k): if a[i] > max_value: max_value = a[i] index = i for i in range(1, n): if (i + k - 1) < n and a[(i + k - 1)] > max_value: max_value = a[(i + k - 1)] index = i + k - 1 if index == i or (i == 1 and index == 0): break print(max_value)
PYTHON3
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
#include <bits/stdc++.h> using namespace std; int main() { long long n, k; cin >> n >> k; int cnt[505] = {0}; long long tem, mx = 0; queue<pair<long long, int> > q; for (int i = 1; i <= n; i++) { cin >> tem; q.push({tem, i}); mx = max(mx, tem); } pair<long long, int> x = q.front(); q.pop(); while (1) { pair<long long, int> y = q.front(); q.pop(); if (x.first == mx) { cout << x.first << endl; break; } if (x.first > y.first) { cnt[x.second]++; q.push(y); } else { cnt[y.second]++; q.push(x); x = y; } if (cnt[x.second] == k) { cout << x.first << endl; break; } } return 0; }
CPP
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
#include <bits/stdc++.h> using namespace std; const double PI = acos(-1); long long power(long long a, long long b, long long m = 1000000007) { long long ans = 1; a = a % m; while (b > 0) { if (b & 1) ans = (1ll * a * ans) % m; b >>= 1; a = (1ll * a * a) % m; } return ans; } long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } long long lcm(long long x, long long y) { return (x * y) / gcd(x, y); } bool isprime(long long n) { if (n < 2) return 0; long long i = 2; while (i * i <= n) { if (n % i == 0) return 0; i++; } return 1; } bool isPowerOfTwo(int x) { return x && (!(x & (x - 1))); } struct cmp { bool operator()(const pair<int, int>& lhs, const pair<int, int>& rhs) const { return lhs.first * rhs.second < lhs.second * rhs.first; } }; bool cmps(pair<int, int> p1, pair<int, int> p2) { return p1.second > p2.second; } double distform(int x, int y, int z, int w) { return sqrt(1. * pow(x - z, 2) + 1. * pow(y - w, 2)); } struct custom_hash { static uint64_t splitmix64(uint64_t x) { x += 0x9e3779b97f4a7c15; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; x = (x ^ (x >> 27)) * 0x94d049bb133111eb; return x ^ (x >> 31); } size_t operator()(uint64_t x) const { static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(x + FIXED_RANDOM); } }; const int MAXN = 2e5 + 10; int32_t main() { auto start = std::chrono::high_resolution_clock::now(); ios::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); long long n, k; cin >> n >> k; long long a[2 * n]; long long maxEle = INT_MIN; for (long long i = 0; i < n; ++i) { cin >> a[i]; a[n + i] = a[i]; maxEle = max(maxEle, a[i]); } long long ans = 0; if (k >= n - 1) { cout << maxEle << "\n"; } else { for (long long i = 0; i < n; ++i) { bool flag = true; if (i > 0) { for (long long j = i - 1; j <= i + k - 1; ++j) { if (i != j) if (a[i] <= a[j]) { flag = false; break; } } if (flag) { cout << a[i] << "\n"; break; } } else { for (long long j = i + 1; j <= i + k; ++j) { if (a[i] <= a[j]) { flag = false; break; } } if (flag) { cout << a[i] << "\n"; break; } } } } auto stop = std::chrono::high_resolution_clock::now(); auto duration = std::chrono::duration_cast<std::chrono::nanoseconds>(stop - start); cerr << "Time taken : " << fixed << ((long double)duration.count()) / ((long double)1e9) << "s " << "\n"; }
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())) maxa = max(a) if len(a) <= k: print(maxa) else: qq = a[0] j = 0 for i in range(1, len(a)): if j == k: print(qq) break if a[i] == maxa: print(maxa) break if a[i] > qq: qq = a[i] j = 1 else: j += 1
PYTHON3
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
import java.util.Scanner; public class Tabletennis { public static int max(int a,int b){ return a>=b?a:b; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); long nbwin = sc.nextLong(); int Players[] = new int[500]; for(int i=0;i<n;i++){ Players[i]= sc.nextInt(); } if(n == 2){ System.out.println(max(Players[0],Players[1])); System.exit(0); } int i = 1 ; int j = 0 ; int winner = Players[0]; while(j<nbwin && j != n-1 && i != n){ if(max(winner,Players[i]) == winner){ i++; j++; } else{ winner = Players[i]; j = 0; } } System.out.println(winner); } }
JAVA
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
# coding=utf-8 import pdb nk = map(int, raw_input().split()) n = nk[0] k = nk[1] players = map(int, raw_input().split()) winner = players[0] #pdb.set_trace() ############### if k >= n - 1: print max(players) else: wins = 0 while wins < k: if players[0] > players[1]: wins += 1 else: w = players[1] players[1] = players[0] players[0] = w wins = 1 p = players[1] players.remove(p) players.append(p) print players[0]
PYTHON
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
#include <bits/stdc++.h> using namespace std; int main() { int n; long long k; scanf("%d", &n); scanf("%I64d", &k); int a; scanf("%d", &a); int ans = a; int count = 0; if (k > n - 2) { for (int i = 1; i < n; ++i) { scanf("%d", &a); if (a > ans) ans = a; } } else { for (int i = 1; i < n; ++i) { scanf("%d", &a); if (count == k) break; if (ans > a) count++; else { count = 1; ans = a; } } } 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
#include <bits/stdc++.h> using namespace std; long long a, b, c, d, i, e, f, g, n, m, k, l, A[100005], maxx; map<int, int> mp; int main() { cin >> n >> k >> A[1]; maxx = A[1]; for (i = 2; i <= n; i++) { cin >> A[i]; maxx = max(maxx, A[i]); mp[maxx]++; if (mp[maxx] == k) { cout << maxx; return 0; } } cout << maxx; }
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 Code { public static void main(String[] args) { Scanner sc = new Scanner(System.in); short n = sc.nextShort(),s=0,a; long k=sc.nextLong(); short temp = sc.nextShort(); for(int i=1;i<n;i++){ a = sc.nextShort(); if(temp<a){ temp=a; s=1;} else s++; if(s==k) break; } System.out.println(temp); } }
JAVA
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
l=lambda:map(int,raw_input().split()) n,k=l() a=l() maxi=a[0] c=0 for v in a[1:]: if maxi>v: c+=1 if c==k: break else: maxi=v c=1 print maxi
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()) powers = list(map(int,input().split())) max_power = powers[0] for a in powers: if max_power < a: max_power = a if n == 2 or k > n -2 or powers[0] == max_power: print(max_power) else: temp_max = 0 number_of_wins = 0 for a in powers: if a > temp_max: if not temp_max: number_of_wins = 0 else: number_of_wins = 1 temp_max = a else: number_of_wins += 1 if number_of_wins == k: print(temp_max) break if(number_of_wins != k): print(max_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 A[1001]; int main() { int n; long long k; int mx = 0; cin >> n >> k; for (int i = 1; i <= n; i++) { cin >> A[i]; mx = max(mx, A[i]); } int z1 = 1; int z2 = 2; long long p = 0; if (k >= n - 1) { cout << mx; return 0; } while (p < k) { if (A[z1] > A[z2]) { p++; swap(A[z1], A[z2]); swap(z1, z2); } else { p = 1; swap(z1, z2); } z2 = z1 + 1; if (z2 > n) { z2 = 1; } } cout << A[z1]; return 0; }
CPP
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
nk = input().split() p = input().split() playerCount = int(nk[0]) winsRequired = int(nk[1]) winnerWins = 0 newWinner = 0 currentWinner = 0 if int(playerCount) < int(winsRequired): currentWinner = playerCount winnerWins = winsRequired while winnerWins < winsRequired: newWinner = 0 if int(p[0]) > int(p[1]): newWinner = int(p[0]) p.append(int(p[1])) p.pop(1) else: newWinner = int(p[1]) p.append(int(p[0])) p.pop(0) if newWinner == currentWinner: winnerWins += 1 else: currentWinner = newWinner winnerWins = 1 print(str(currentWinner))
PYTHON3
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
#include <bits/stdc++.h> using namespace std; template <typename T> T gcd(T x, T y) { if (x < y) swap(x, y); while (y > 0) { T f = x % y; x = y; y = f; } return x; } template <typename T> pair<T, T> exgcd(T x, T y) { int sw = 0; if (x < y) sw = true, swap(x, y); pair<T, T> r = make_pair(1, 0); while (y > 0) { T f = x % y; r = make_pair(r.second, r.first - (x / y) * r.second); x = y; y = f; }; if (sw) swap(r.first, r.second); return r; } int main(int argc, char *argv[]) { std::cin.sync_with_stdio(false); std::cin.tie(nullptr); long long n, k; cin >> n >> k; { vector<int> p; vector<int> tmp; p.reserve(n); p.resize(n, 0); tmp.resize(n, 0); for (int i = 0; i < n; i++) cin >> p[i]; int res = n; for (int i = 0; i < n; i++) { int kk = 0; if (p[0] == n) break; int j = 1; while (j < n && p[0] > p[j]) j++; if (j + (i ? 1 : 0) > k) { res = p[0]; break; } for (int x = 0; x < j; x++) tmp[x] = p[x]; p.erase(p.begin(), p.begin() + j); for (int x = 1; x < j; x++) p.push_back(tmp[x]); p.push_back(tmp[0]); } cout << res << endl; } return 0; }
CPP
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Collection; import java.util.Deque; import java.util.Iterator; import java.util.LinkedList; import java.util.Queue; import java.util.StringTokenizer; public class Main { public static void main(String[] args) { FastReader read = new FastReader () ; int n = read.nextInt() ; long k = read.nextLong() ; int max = 0 ; LinkedList<Integer> p = new LinkedList<>(); int plr = 0 ; for (int i = 0; i < n; i++) { plr = read.nextInt(); p.add(plr); max = Math.max(max, plr); } if(k>=n){ k=n-1; } int w = 0 ; int pl = 0 ; int y = 0 ; while (w < k && y < n){ pl = p.poll(); w = 0 ; if(y>0){ w=1; } for (int i = 0; i < p.size(); i++) { // System.out.println(pl +" " + p.get(i)); if(pl > p.get(i)){ w++; if(k==w) break; } else { p.add(pl); break;} } // System.out.println(w+""); y++; } System.out.println(pl); } } class Pair implements Comparable<Pair> { int x , y ; public Pair(int x , int y ) { this.x = x ; this.y = y ; } @Override public int compareTo(Pair p) { if(this.x!=p.x) return this.x - p.x ; else return p.y - this.y; } } class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } }
JAVA
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
import java.util.Arrays; import java.util.Scanner; public class B { static long[] och = new long[6000]; public static void main(String[] args) { Scanner in = new Scanner(System.in); Arrays.fill(och, 0); int n = in.nextInt(); long k = in.nextLong(); int[] a = new int[n + 2]; int ans_id = 0; boolean f = false; int max = -1 ; for (int i = 1; i <= n; i++) { a[i] = in.nextInt(); max = Math.max(max, a[i]); if (max == a[i]) ans_id = i; } if (max == a[1]){ System.out.println(a[1]); }else { while(a[1] != max && k != och[1]){ int cnt = 0; if (a[1]<a[2]){ och[2]++; if (och[2]>= k){ System.out.println(a[2]); return; } int a_1 = a[1]; long hisob = och[1]; for (int i = 1; i < n; i++) { a[i] = a[i+1]; och[i] = och[i+1]; } a[n] = a_1; och[n] = hisob; }else{ och[1]++; if (och[1]>= k){ System.out.println(a[1]); return; } int a_2 = a[2]; long hisob2 = och[2]; for (int i = 2; i < n; i++) { a[i] = a[i+1]; och[i] = och[i+1]; } a[n] = a_2; och[n] = hisob2; } if (och[1] >= k){ f = true; break; } } System.out.println(a[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
n,k=map(int,input().split()) a=[int(i) for i in input().split()] if n==2: print(max(a)) exit() if k>n: print(max(a)) exit() k+=1 a=a+a a.insert(0,10**9) for i in range(1,n+1): if a[i]==max(a[i-1:i+k-1]) or a[i]==max(a[i:i+k]): print(a[i]) exit() print(max(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 = list(map(int,input().split())) powers = list(map(int,input().split())) current_streak=0 if k>n-1: print(max(powers)) exit(0) while True: if powers[0]>powers[1]: current_streak+=1 temp = powers.pop(1) powers.append(temp) else: current_streak=1 temp = powers.pop(0) powers.append(temp) if current_streak==k: break print(powers[0])
PYTHON3
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
import java.util.*; import java.io.*; import java.math.*; public class mmm { static class InputReader { public BufferedReader br; public StringTokenizer token; public InputReader(InputStream stream) { br=new BufferedReader(new InputStreamReader(stream),32768); token=null; } public String next() { while(token==null || !token.hasMoreTokens()) { try { token=new StringTokenizer(br.readLine()); } catch(IOException e) { throw new RuntimeException(e); } } return token.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } } /*static class card{ long up; int down; public card(long u,int d) { this.up=u; this.down=d; } } static class sort implements Comparator<card> { public int compare(card o1,card o2) { if(o1.up!=o2.up) return (int)(o1.up-o2.up); else return (int)(o1.down-o2.down); } } static void shuffle(long a[]) { List<Long> l=new ArrayList<>(); for(int i=0;i<a.length;i++) l.add(a[i]); Collections.shuffle(l); for(int i=0;i<a.length;i++) a[i]=l.get(i); } static long gcd(long a,long b) { if(b==0) return a; else return gcd(b,a%b); } static int ans1=Integer.MAX_VALUE,ans2=Integer.MAX_VALUE,ans3=Integer.MAX_VALUE,ans4=Integer.MAX_VALUE; static boolean v[]=new boolean[101]; static void dfs(Integer so,Set<Integer> s[]){ if(!v[so.intValue()]) { v[so]=true; for(Integer h:s[so.intValue()]) { if(!v[h.intValue()]) dfs(h,s); } } } static class Print{ public PrintWriter out; Print(OutputStream o) { out=new PrintWriter(o); } } static int CeilIndex(int A[], int l, int r, int key) { while (r - l > 1) { int m = l + (r - l) / 2; if (A[m] >= key) r = m; else l = m; } return r; } static int LongestIncreasingSubsequenceLength(int A[], int size) { // Add boundary case, when array size is one int[] tailTable = new int[size]; int len; // always points empty slot tailTable[0] = A[0]; len = 1; for (int i = 1; i < size; i++) { if (A[i] < tailTable[0]) // new smallest value tailTable[0] = A[i]; else if (A[i] > tailTable[len - 1]) // A[i] wants to extend largest subsequence tailTable[len++] = A[i]; else // A[i] wants to be current end candidate of an existing // subsequence. It will replace ceil value in tailTable tailTable[CeilIndex(tailTable, -1, len - 1, A[i])] = A[i]; } return len; }*/ /*static int binary(int n) { int s=1; while(n>0) { s=s<<1; n--; } return s-1; } static StringBuilder bin(int i,int n) { StringBuilder s=new StringBuilder(); while(i>0) { s.append(i%2); i=i/2; } while(s.length()!=n) { s.append(0); } return s.reverse(); }*/ public static void main(String args[]) { InputReader sc=new InputReader(System.in); int n=sc.nextInt(); long k=sc.nextLong(); int a[]=new int[n]; int ans=0; for(int i=0;i<n;i++) { a[i]=sc.nextInt(); ans=Math.max(a[i],ans); } int f[]=new int[505]; int w=0; if(a[0]>a[1]) w=a[0]; else w=a[1]; f[w]++; if(f[w]==k) System.out.println(ans); else { for(int i=2;i<n;i++) { if(w>a[i]) { f[w]++; if(f[w]==k) { ans=w; break; } } else { w=a[i]; f[w]++; if(f[w]==k) { ans=w; break; } } } } System.out.println(ans); } }
JAVA
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
nw = input().split() playersCount = int(nw[0]) victoriesNeeded = int(nw[1]) powP = list(map(int, input().rstrip().split())) if (playersCount < victoriesNeeded): print(max(powP)) else: win = 0 while win < victoriesNeeded: if int(powP[0]) > int(powP[1]): powP.append(powP.pop(1)) win = win + 1 else: win = 0 powP.append(powP.pop(0)) win = win + 1 print(powP[0])
PYTHON3
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
import java.util.*; import java.io.*; public class 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 streak = 0; long winner = in.nextLong(); long opp = 0; for (i = 1; i < n2; i++) { opp = in.nextLong(); if (opp > winner) { winner = opp; streak = 1; } else streak++; if (streak == k) { out.println(winner); return; } } out.println(winner); //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
import java.io.*; import java.util.*; /* TASK: CFB LANG: JAVA */ public class CFB { static int n; static long k; static int[] power; public static void main(String[] args) throws IOException { FastScanner in = new FastScanner(System.in); n = in.nextInt(); k = in.nextLong(); power = new int[n]; for(int i = 0;i < n;i++)power[i] = in.nextInt(); int cnt = 0; int max = power[0]; for(int i = 1; i< n; i++){ if(power[i] > max){ cnt = 1; max = power[i]; } else{ cnt++; } if(cnt == k){ System.out.println(max); return; } } System.out.println(n); } private static class FastScanner { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; public FastScanner(InputStream stream) { this.stream = stream; } 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++]; } boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } boolean isEndline(int c) { return c == '\n' || c == '\r' || c == -1; } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } public String next() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public String nextLine() { int c = read(); while (isEndline(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isEndline(c)); return res.toString(); } } }
JAVA
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
import java.util.LinkedList; import java.util.Scanner; public class p3 { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int n = scan.nextInt(); long k = scan.nextLong(); LinkedList<Integer> list = new LinkedList<Integer>(); long[] a = new long[501]; for (int i = 0; i < n; i++) { list.addLast(scan.nextInt()); } int max = -1; for (int i = 0; i < n; i++) { if (list.get(i) > max) { max = list.get(i); } } scan.close(); int sol = -1; while (true) { int x = list.get(0); int y = list.get(1); if (x == max) { sol = x; break; } if (y == max) { sol = y; break; } if (x > y) { list.remove(1); list.add(1); a[x]++; } else { list.remove(0); list.add(0); a[y]++; } if (a[x] == k) { sol = x; break; } if (a[y] == k) { sol = y; break; } } System.out.println(sol); } }
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[100010], mx; long long k; queue<int> q; int main() { cin >> n >> k; for (int i = 1; i <= n; i++) { cin >> a[i]; if (mx < a[i]) mx = a[i]; } if (k >= n) cout << mx; else { int times = 0, winner = 1, fighter = 2, last = n; while (times < k) { if (a[winner] > a[fighter]) { times++; a[++last] = a[fighter]; fighter++; } else { a[++last] = a[winner]; times = 1; winner = fighter; fighter++; } } cout << a[winner]; } 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 Tennis { public static void main (String[] args) { Scanner scan = new Scanner(System.in); int noPeople = scan.nextInt(); long noWins = scan.nextLong(); int p1 = scan.nextInt(); long tmp = 0; for (int i = 1; i < noPeople; i++) { int p2 = scan.nextInt(); if (p2 > p1) { tmp = 1; p1 = p2; } else { tmp++; } if (noWins <= tmp) { //power of winner 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
import Queue n, k = map(int, raw_input().split()) a = map(int, raw_input().split()) q = Queue.Queue() if k >= n-1: print max(a) else: for i in range(1, n): q.put(a[i]) b, w = a[0], 0 while w < k: top = q.get() if b > top: q.put(top) w += 1 else: q.put(b) b, w = top, 1 print b
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()) l=list(map(int,input().split())) cnt=[0]*n queue=l[:] for i in range(n): a,b=queue[0],queue[1] m=max(a,b) mi=min(a,b) cnt[l.index(m)]+=1 queue.remove(mi) queue[0]==m queue.append(mi) if max(cnt)==k: print(l[cnt.index(max(cnt))]) quit() print(max(l))
PYTHON3
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
(n, k) = [int(x) for x in input().split(' ')] a = [ int(x) for x in input().split(' ')] if k >= 10 * (n - 1): print(max(a)) else: winner = a.pop(0) conseq_win = 0 while conseq_win != k: u = winner v = a.pop(0) if u > v: a.append(v) conseq_win = conseq_win + 1 else: a.append(u) winner = v conseq_win = 1 print(winner)
PYTHON3
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
pessoaVitoria = map(int,raw_input().split()) jogadas = map(int,raw_input().split()) numeroVitoria = pessoaVitoria[1] jogador = jogadas[0] A = 1 contVitoria = 0 jogadas.append('fim') while(True): if(contVitoria >= pessoaVitoria[1] or jogadas[A] == 'fim'): break else: if(jogador > jogadas[A]): contVitoria += 1 A += 1 else: jogador = jogadas[A] A += 1 contVitoria = 1 print jogador
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 n; long long k; int main() { cin >> n >> k; int *a = new int[n + 1]; int *win = new int[n + 1]; int max_v = 0; int x; for (int i = 0; i < n; i++) { cin >> a[i]; win[i] = 0; max_v = max(max_v, a[i]); } if (k >= n) { cout << max_v; return 0; } for (int i = 0; i < n; i++) { int ok = 2; for (int j = (i + 1) % n; j < n && a[i] > a[j]; j = (j + 1) % n) { if (j == (i) % n) { j++; ok--; } else { win[i]++; if (win[i] >= k) { cout << a[i]; return 0; } } } win[i + 1]++; if (win[i] >= k) { cout << a[i]; 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
#include <bits/stdc++.h> using namespace std; long long i, j, k, l, mx, n, a[505], cnt[505]; int main() { cin >> n >> k; for (i = 1; i <= n; i++) { cin >> a[i]; mx = max(mx, a[i]); } i = 1, j = 2; while (i < n) { while (a[i] > a[j]) { cnt[a[i]]++; if (cnt[a[i]] == k) { cout << a[i]; return 0; } if (j < n) j++; else { cout << mx; return 0; } } cnt[a[j]]++; if (cnt[a[j]] == k) { cout << a[j]; return 0; } i = j; j++; } 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=map(int,input().split()) a=list(map(int,input().split())) t=a[0] c=0 i=1 while i<len(a): if t>a[i]: a.append(a[i]) c+=1 else: c=1 a.append(t) t=a[i] if c>=k: print(t) break if i>2*(n-1): print(max(a)) break i+=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
R=list(map(int,input().split())) n=R[0] k=R[1] a=list(map(int,input().split())) c=[0 for i in range(n+1)] while 1>0: if a[0]>a[1]: a[0],a[1]=a[1],a[0] a=a[1:]+a[:1] c[a[0]]+=1 if a[0]==n or c[a[0]]>=k: print(a[0]) break
PYTHON3
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
import queue import sys n, k = map(int, input().split()) ar = list(map(int, input().split())) if n == 2: print(2) sys.exit(0) q = queue.Queue() maxi = max(ar) for i in ar: q.put(i) wins = 0 a = q.get() if a == maxi: print(a) sys.exit(0) while True: b = q.get() if b == maxi: print(b) sys.exit(0) if b > a: q.put(a) a = b wins = 1 else: wins += 1 q.put(b) if wins >= k: print(a) sys.exit(0)
PYTHON3
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
import java.util.Deque; import java.util.LinkedList; import java.util.Scanner; public class B879 { public static void main(String[] args) { Scanner in = new Scanner(System.in); int N = in.nextInt(); long K = in.nextLong(); if (K > N-2) { System.out.println(N); } else { Deque<Integer> deque = new LinkedList<>(); for (int n=0; n<N; n++) { deque.add(in.nextInt()); } int wins = 0; int winner; do { winner = deque.poll(); while (wins < K) { int player = deque.poll(); if (player < winner) { deque.add(player); wins++; } else { deque.push(player); deque.add(winner); wins = 1; break; } } } while (wins < K); System.out.println(winner); } } }
JAVA
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
a = input().split() b = input().split() players = [int(i) for i in b] n = int(a[0]) k = int(a[1]) winA = 0 while True: if players[0] > players[1]: winA += 1 players.append(players.pop(1)) elif players[1] > players[0]: winA = 1 players.append(players.pop(0)) if winA == k or max(players) == players[0]: print(players[0]) break
PYTHON3
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
# -*- coding: utf-8 -*- def main(): n, k = map(int, input().split()) x = [int(x) for x in input().split()] if (k >= n): print(max(x)) return else: for i in range(0,n): if ((i+k)<n): if(i==0): temp = max(x[i:i+k+1]) else: temp = max(x[i:i+k]) if (temp == x[i]): print(temp) break else: continue else: temp1 = max(x[i:n]) tempx = k - (n-i) +1 temp2 = max(x[0:tempx]) temp = max(temp1,temp2) if (temp == x[i]): print(temp) break else: continue 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
#include <bits/stdc++.h> using namespace std; int main() { long long int n, k, c = 0, m, p, q; cin >> n >> k; long long int a[n]; for (int i = 0; i < n; i++) { cin >> a[i]; } if (n == 2) { if (a[0] > a[1]) cout << a[0] << endl; else cout << a[1] << endl; } else { for (int i = 0; i < n; i++) { while (1) { if (a[i] > a[i + 1]) { if (i + 2 == n) { cout << a[i] << endl; p = 1; break; } else { m = a[i]; q = a[i + 1]; for (int j = i + 1; j < n; j++) { a[j] = a[j + 1]; } a[n - 1] = q; c++; } } else { if (i + 2 == n) { cout << a[i + 1] << endl; p = 1; break; } else { m = a[i + 1]; q = a[i]; for (int j = i; j < n; j++) { a[j] = a[j + 1]; } a[n - 1] = q; c = 1; } } if (c == k || c > n) { cout << m << endl; p = 1; break; } } if (p == 1) break; } } }
CPP
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
#http://codeforces.com/problemset/problem/879/B #solved n, k = list(map(int, input().split())) array = list(map(int, input().split())) atak = 0 bb = array[0] if max(array) < k: print(max(array)) quit() else: for i in range(n - 1): if bb < array[i + 1]: bb = array[i + 1] atak = 0 atak += 1 else: atak += 1 if atak == k: print(bb) quit() print(bb)
PYTHON3
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
n,k=map(int,input().split()) arr=list(map(int,input().split())) a=arr[0] b=arr[1] arr.pop(0) arr.pop(0) sumi=0 if(a>b): arr.append(b) maxp=a sumi+=1 else: arr.append(a) sumi+=1 maxp=b while(1): if(sumi==n-1 or sumi==k): print(maxp) break; if(max(maxp,arr[0])==maxp): c=arr[0] arr.pop(0) arr.append(c) sumi+=1 else: arr.append(maxp) maxp=arr[0] arr.pop(0) sumi=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 io, os input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline n, k = map(int, input().split()) a = list(map(int, input().split())) t = a[0] temp = 0 if k >= n-1: print(max(a)) else: while temp != k: x, y = a[0], a[1] if x > y: a.append(y) del a[1] else: a.append(x) del a[0] if t == a[0]: temp += 1 else: t = a[0] temp = 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()) a, i, v, c = list(map(int, input().split())), 0, 0, 0 while v < n and c < k: w, l = max(a[i], a[i + 1]), min(a[i], a[i + 1]) a[i + 1] = w a.append(l) c = c + 1 if w == v else 1 v = w i += 1 print(v)
PYTHON3
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
#include <bits/stdc++.h> using namespace std; int main() { long long int n, k, a[5006]; while (cin >> n >> k) { for (int i = 1; i <= n; i++) cin >> a[i]; long long int mx = 0; map<int, int> mp; mx = a[1]; for (int i = 2; i <= n; i++) { mx = max(mx, a[i]); mp[mx]++; if (mp[mx] == k) break; } cout << mx << endl; } return 0; }
CPP
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
from queue import deque n, k = map(int, input().split()) m, *l = map(int, input().split()) q, c = deque(l), 0 while m < n: a = q.popleft() if m < a: q.append(m) m, c = a, 1 else: q.append(a) c += 1 if c == k: break print(m)
PYTHON3
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
n, k = map(int, raw_input().split()) powers = map(int, raw_input().split()) winner = powers[0] wins = 0 index = 1 while (wins < k and len(powers) > index): if (powers[index] > winner): winner = powers[index] wins = 1 else: wins += 1 index += 1 print(winner)
PYTHON
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
import java.util.ArrayDeque; import java.util.Scanner; public class Patient { public static void main(String[] args) { // TODO code application logic here Scanner sc=new Scanner(System.in); int n=sc.nextInt(); long k=sc.nextLong(); int count=0; int[] arr=new int[n]; ArrayDeque<Integer> ad=new ArrayDeque<>(); for(int i=0;i<n;i++){ arr[i]=sc.nextInt(); ad.add(arr[i]); } while(true){ int p1=(int) ad.poll(); int p2=(int) ad.poll(); if(p1>p2){ count++; if(count==k || count>=499){ System.out.println(p1); break; } ad.addFirst(p1); ad.add(p2); } else { ad.addFirst(p2); ad.add(p1); count=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 java.util.*; import java.math.*; import static java.lang.System.out; public class Main { public static void main(String[] args) { Scanner cin = new Scanner(System.in); ArrayDeque<Integer> line = new ArrayDeque<>(); int n = cin.nextInt(); long k = cin.nextLong(); for (int i = 0; i < n; i++) { line.addLast(cin.nextInt()); } if (k <= n-1) { int winTimes = 0; while (winTimes != k) { Integer fri = line.removeFirst(), sec = line.removeFirst(); if (fri > sec) { line.offerLast(sec); line.offerFirst(fri); winTimes += 1; } else { line.offerLast(fri); line.offerFirst(sec); winTimes = 1; } } out.println(line.peekFirst()); } else { int max = Integer.MIN_VALUE; for (Integer i : line) { max = Math.max(max, i); } out.println(max); } cin.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; const int P = 1e9 + 7; long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } long long qpow(long long a, long long n) { long long r = 1 % P; for (a %= P; n; a = a * a % P, n >>= 1) if (n & 1) r = r * a % P; return r; } void exgcd(long long a, long long b, long long &d, long long &x, long long &y) { b ? exgcd(b, a % b, d, y, x), y -= a / b *x : x = 1, y = 0, d = a; } long long inv(long long x) { return x <= 1 ? 1 : inv(P % x) * (P - P / x) % P; } const int N = 4e5 + 10; int a[N], b[N], c[N], vis[N], f[N], n, m, k, t; vector<int> g[N]; int main() { long long k; cin >> n >> k; if (k >= n) return printf("%d\n", n), 0; int now, cnt = 0; scanf("%d", &now); for (int i = 2; i <= n; ++i) { scanf("%d", &t); if (now <= t) now = t, cnt = 1; else ++cnt; if (cnt >= k) return printf("%d\n", now), 0; } printf("%d\n", n); }
CPP
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
##a = list(map(int, input().split())) ##print(' '.join(map(str, res))) [n, k] = list(map(int, input().split())) a = list(map(int, input().split())) wins = [0 for i in range(n+1)] if k < 1000: while True: x = a[0] y = a[1] if x > y: a.remove(y) a.append(y) wins[x] += 1 if wins[x] == k: print(x) exit(0) else: a.remove(x) a.append(x) wins[y] += 1 if wins[y] == k: print(y) exit(0) amax = max(a) print(amax)
PYTHON3
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
n, k = map(int, input().split()) A = list(map(int, input().split())) if k == n - 1: ans = max(A) else: ans = A[0] cnt = 0 for i in range(1, n): if ans > A[i]: cnt += 1 if cnt == k: break else: ans = A[i] cnt = 1 print(ans)
PYTHON3
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
n,k = list(map(int, input().split())) if k > n: print(n) else: cwinner, wins = (0, 0) a = list(map(int, input().split())) while wins < k: if a[0] > a[1]: a[1:] = a[2:] + [a[1]] if cwinner == a[0]: wins += 1 else: cwinner = a[0] wins = 1 else: a[0], a[1] = a[1], a[0] a[1:] = a[2:] + [a[1]] if cwinner == a[0]: wins += 1 else: cwinner = a[0] wins = 1 print(cwinner)
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
inputParameter = input() inputArrayContentFinal = [int(x) for x in input().split()] counter = 0 anothercounter = 0 inputParameterFinal = inputParameter.split(" ") arrayLength = inputParameterFinal[0] gamesToWin = int(inputParameterFinal[1]) while counter < gamesToWin: if len(inputArrayContentFinal) == 1: print(inputArrayContentFinal[0]) break if counter == gamesToWin: print(inputArrayContentFinal[0]) if inputArrayContentFinal[0] > inputArrayContentFinal[1]: counter = counter+1 inputArrayContentFinal.pop(1) if counter == gamesToWin: print(inputArrayContentFinal[0]) elif inputArrayContentFinal[0] < inputArrayContentFinal[1]: inputArrayContentFinal[0] = inputArrayContentFinal[1] inputArrayContentFinal.pop(1) counter = 1 if counter == gamesToWin: print(inputArrayContentFinal[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
number_of_players, win_requirement = input().split() number_of_players = int(number_of_players) win_requirement = int(win_requirement) powers = input().split(' ') powers = [int(power) for power in powers] if powers[0] > max(powers[1:min(number_of_players, win_requirement + 1)]): print(powers[0]) else: powers = powers[1:] + [powers[0]] for i in range(number_of_players - 1): if powers[0] > max(powers[1:min(number_of_players, win_requirement)]): print(powers[0]) break else: powers = powers[1:] + [powers[0]]
PYTHON3
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
#include <bits/stdc++.h> using namespace std; int main() { long long n, k; int fg = 0, a[505]; cin >> n >> k; for (int i = 0; i < n; i++) cin >> a[i]; if (k >= n) cout << n << endl; else { for (int i = 0; i < n; i++) { int s = 0; if (i != 0 && a[i] > a[i - 1]) s = 1; for (int j = 1; j <= k - s; j++) { int pos = (i + j) % n; if (a[i] < a[pos]) break; if (j == k - s) { fg = 1; cout << a[i] << endl; break; } } if (fg) break; } } return 0; }
CPP