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
#include <bits/stdc++.h> using namespace std; int n, i, j, a[505], mx = 0, f[1000005], head, tail, t; long long k; int main() { cin >> n >> k; for (i = 1; i <= n; i++) { scanf("%d", &a[i]); mx = max(mx, a[i]); f[i] = a[i]; } if (k > n) { cout << mx; return 0; } t = 0; head = 1; tail = n; while (t < k) { if (f[head] < f[head + 1]) { t = 1; f[++tail] = f[head]; head++; } else { t++; f[++tail] = f[head + 1]; f[head + 1] = f[head]; head++; } } cout << f[head]; 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 cnt[505]; int main() { long long n, k, x, ans; long long A[505]; while (cin >> n >> k) { for (int i = 0; i < n; i++) cnt[i] = 0; for (int i = 0; i < n; i++) cin >> A[i]; x = 0; if (k >= n) { sort(A, A + n); cout << A[n - 1] << endl; } else { while (1) { int x = A[0]; int y = A[1]; if (x > y) { cnt[x]++; if (cnt[x] == k) { ans = x; break; } for (int i = 1; i < n - 1; i++) A[i] = A[i + 1]; A[n - 1] = y; } else { cnt[y]++; if (cnt[y] == k) { ans = y; break; } for (int i = 0; i < n - 1; i++) A[i] = A[i + 1]; A[n - 1] = x; } } cout << ans << endl; } } return 0; }
CPP
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; public class ProblemB { static Scanner sc = new Scanner(System.in); static PrintWriter pw = new PrintWriter(System.out); public static void main(String[] args) throws IOException { int n = sc.nextInt(), max = -1, maxpos = -1; long k = sc.nextLong(); int power[] = new int[n]; for (int i = 0; i < n; i++) { power[i] = sc.nextInt(); if (max < power[i]) { max = power[i]; maxpos = i; } } int counter = 0, winner = power[0]; for (int i = 1; i < n; i++) { if (i == maxpos) { winner = max; break; } if (winner > power[i]) { counter++; } else { winner = power[i]; counter = 1; } if (counter == k) break; } pw.println(winner); pw.flush(); } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(String s) throws FileNotFoundException { br = new BufferedReader(new FileReader(new File(s))); } public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } } }
JAVA
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
import sys n,k=map(int,raw_input().split()) a=map(int,raw_input().split()) scr={} for i in a:scr[i]=0 p=a p1=p.pop(0) p2=p.pop(0) for i in range(n): if p1>p2: p.append(p2) scr[p1]+=1 p2=p.pop(0) if scr[p1]==k: print p1 sys.exit() break else: p.append(p1) scr[p2]+=1 p1=p.pop(0) if scr[p2]==k: print p2 sys.exit() break p.append(p1) p.append(p2) ma=0 ind=0 for i in a: if scr[i]>ma: ma=scr[i] ind=i ma2=scr[max(a)] #print ma,ma2,k-ma-ma2 if k-ma>-ma2+k: print ind else:print max(a)
PYTHON
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
#include <bits/stdc++.h> using namespace std; int m, a[505]; long long k; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> m >> k; for (int i = 0; i < m; i++) { cin >> a[i]; } for (int i = 0; i < m; i++) { bool win = 1; for (int j = 1; j <= min((long long)m - 1, k) - (i > 0); j++) { if (a[(i + j) % m] > a[i]) { win = 0; break; } } if (win) { cout << a[i] << '\n'; return 0; } } return 0; }
CPP
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
n, k = map(int, input().rstrip().split(' ')) player = list(map(int, input().rstrip().split(' '))) if n <= k: k = n - 1 playWin = 0 count = 0 for i in range(1, n): tempComp = max(player[i], player[playWin]) if player[playWin] == tempComp: count += 1 else: count = 1 playWin = i if count == k: break print(player[playWin])
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.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.*; public class Soly { static final int INF = Integer.MAX_VALUE; static void mergeSort(int[] a,int [] c, int b, int e) { if(b < e) { int q = (b + e) >>1; mergeSort(a,c, b, q); mergeSort(a, c,q + 1, e); merge(a,c, b, q, e); } } static void merge(int[] a,int[]c, int b, int mid, int e) { int n1 = mid - b + 1; int n2 = e - mid; int[] L = new int[n1+1], R = new int[n2+1]; int[] L1 = new int[n1+1], R1 = new int[n2+1]; for(int i = 0; i < n1; i++) { L[i] = a[b + i]; L1[i] = c[b + i]; } for(int i = 0; i < n2; i++) { R[i] = a[mid + 1 + i]; R1[i] = c[mid + 1 + i]; } L[n1] = R[n2] = INF; for(int k = b, i = 0, j = 0; k <= e; k++) if(L[i] <= R[j]){ a[k] = L[i]; c[k] = L1[i++]; } else { a[k] = R[j]; c[k] = R1[j++]; } } public static void main(String[] args) throws IOException { Scanner in = new Scanner(System.in); try (PrintWriter or = new PrintWriter(System.out)) { int n = in.nextInt(); long k=in.nextLong(); int [] a= new int[n]; int max=0; for (int i = 0; i < n; i++)max=Math.max(max, a[i]=in.nextInt()); for (int i = 0; i < n; i++) { int ans=0; if (a[i]==max){ or.println(max);break; } if (i!=0){ if (a[i]>a[i-1])++ans; } for (int j = i+1; ;++j) { if (a[i]>a[(j)%n])++ans; else break; } if (ans>=k){ or.println(a[i]);break; } } } } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public String nextLine() throws IOException { return br.readLine(); } public double nextDouble() throws IOException { String x = next(); StringBuilder sb = new StringBuilder("0"); double res = 0, f = 1; boolean dec = false, neg = false; int start = 0; if (x.charAt(0) == '-') { neg = true; start++; } for (int i = start; i < x.length(); i++) { if (x.charAt(i) == '.') { res = Long.parseLong(sb.toString()); sb = new StringBuilder("0"); dec = true; } else { sb.append(x.charAt(i)); if (dec) { f *= 10; } } } res += Long.parseLong(sb.toString()) / f; return res * (neg ? -1 : 1); } public boolean ready() throws IOException { return br.ready(); } } } class coooo implements Comparable<coooo> { int x,y; public coooo(int x, int y) { this.x = x; this.y = y; //this.ind = ind; } @Override public int compareTo(coooo o) { if (x!=o.x)return x-o.x; return y-o.y; } }
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, r; long long k; cin >> n; cin >> k; int i, a[10000], m = 0; for (i = 0; i < n; i++) { cin >> a[i]; if (a[i] > m) m = a[i]; } if (a[0] == m || k >= n) r = m; else { int c, j, p = 0; for (i = 0; i < n - 1; i++) { c = 0; int x = 0; for (j = i + 1; j < n; j++) { if (a[i] < a[j]) break; if (a[j] < a[i]) { c++; if (i > 0 && x == 0) { c += 1; x = 1; } } } if (c >= k) { r = a[i]; p = 1; break; } } if (p == 0) r = m; } cout << r << 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.util.ArrayList; import java.util.Scanner; public class p879B { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int n = scan.nextInt(); long k = scan.nextLong(); ArrayList<Integer> list = new ArrayList<Integer>(); for (int i = 0; i < n; i++) list.add(scan.nextInt()); int cons = 0; while (true) { if (list.get(0) > list.get(1)) { list.add(list.remove(1)); cons++; } else { list.add(list.remove(0)); cons = 1; } if (cons == k || cons == n) { break; } } System.out.println(list.get(0)); } }
JAVA
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
#include <bits/stdc++.h> using namespace std; const int N = 555; int n, i; int a[N]; int cnt[N]; int Q[N * N * 10]; long long k; int main() { cin >> n >> k; k = min(k, n - 1LL); for (i = (1); i <= (n); i++) cin >> a[i], Q[i] = i; int L = 2, R = n; int c = 1; while (1) { if (a[c] < a[Q[L]]) { Q[++R] = c; c = Q[L++]; } else Q[++R] = Q[L++]; if (++cnt[c] == k) { cout << a[c]; 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
b=(list(map(int, input().split()))) a=(list(map(int, input().split()))) n=b[0] k=b[1] l=0 p=0 q=1 while 1: if q%n==0: q=0 if a[p]>=a[q]: if a[p]!=a[q]: l+=1 if l%n==0 or l==k: print(a[p]) break else: p+=1 q=p l=1 q+=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.*; import java.math.*; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; //--------------->>>>IF YOU ARE HERE FOR QUICKSORT HACK THEN SORRY NO HACK FOR YOU<<<------------------- public class a{ static long[] count,count1,count2; static Node[] nodes; static int[] arr; static int[] dp; static char[] ch,ch1; static long[] darr,farr; static char[][] mat,mat1; static long x,h; static long maxl; static double dec; static long mx = (long)1e9+7; static String s; static long minl; static int start_row; static int start_col; static int end_row; static int end_col; static long mod = (long)1e7+7; // static int minl = -1; // static long n; static int n,n1,n2; static long a; static long b; static long c; static long d; static long y,z; static int m; static long k; static String[] str,str1; static Set<Long> set,set1,set2; static SortedSet<Long> ss; static List<Integer> list,list1,list2,list3; static LinkedList<Integer> ll; static Map<Integer,List<Integer>> map,map1; static StringBuilder sb,sb1,sb2; static int index; static int[] dx = {0,-1,0,1,-1,1,-1,1}; static int[] dy = {-1,0,1,0,-1,-1,1,1}; // public static void solve(){ // FastScanner sc = new FastScanner(); // // int t = sc.nextInt(); // int t = 1; // for(int tt = 0 ; tt < t ; tt++){ // int n = sc.nextInt(); // int m = sc.nextInt(); // int prev = 0; // for(int i = 0 ; i < n ; i++){ // int s = sc.nextInt(); // int e = sc.nextInt(); // if(s > prev){ // System.out.println("NO"); // return; // } // prev = Math.max(prev,e); // } // if(prev == m) // System.out.println("YES"); // else // System.out.println("NO"); // // System.out.println(sb); // } // } //--------------->>>>IF YOU ARE HERE FOR QUICKSORT HACK THEN SORRY NO HACK FOR YOU<<<------------------- public static void solve(){ if(k > (long)n){ System.out.println(n); return; } ll = new LinkedList<>(); for(int i = 0; i < n ; i++) ll.addLast(arr[i]); long streak = 1; Integer player1 = ll.removeFirst(); Integer player2 = ll.removeFirst(); Integer winner; if(player1 > player2){ ll.addLast(player2); winner = player1; } else{ ll.addLast(player1); winner = player2; } while(true){ if(streak == k){ System.out.println(winner); return; } player2 = ll.removeFirst(); if(winner > player2){ ll.addLast(player2); streak += 1; } else{ ll.addLast(winner); streak = 1; winner = player2; } } } public static void main(String[] args) { FastScanner sc = new FastScanner(); // Scanner sc = new Scanner(System.in); // int t = sc.nextInt(); int t = 1; // int l = 1; while(t > 0){ // n = sc.nextInt(); // n = sc.nextLong(); // a = sc.nextLong(); // b = sc.nextLong(); // c = sc.nextLong(); // d = sc.nextLong(); // x = sc.nextLong(); // y = sc.nextLong(); // n = sc.nextLong(); n = sc.nextInt(); // n1 = sc.nextInt(); // m = sc.nextInt(); k = sc.nextLong(); // s = sc.next(); // ch = sc.next().toCharArray(); // ch1 = sc.next().toCharArray(); arr = new int[n]; for(int i = 0 ; i < n ; i++){ arr[i] = sc.nextInt(); } // // m = n; // darr = new long[n]; // for(int i = 0; i < n ; i++){ // darr[i] = sc.nextLong(); // } // farr = new int[n]; // for(int i = 0; i < n ; i++){ // farr[i] = sc.nextInt(); // } // mat = new char[n][m]; // for(int i = 0 ; i < n ; i++){ // String s = sc.next(); // for(int j = 0 ; j < m ; j++){ // mat[i][j] = s.charAt(j); // } // } // str = new String[n]; // for(int i = 0 ; i < n ; i++) // str[i] = sc.next(); // nodes = new Node[n]; // for(int i = 0 ; i < n ;i++) // nodes[i] = new Node(sc.nextInt(),sc.nextInt()); // System.out.println(solve()?"YES":"NO"); solve(); // System.out.println(solve()); t -= 1; } } public static int log(long n){ if(n == 0 || n == 1) return 0; if(n == 2) return 1; double num = Math.log(n); double den = Math.log(2); if(den == 0) return 0; return (int)(num/den); } public static boolean isPrime(long n) { // Corner cases if (n <= 1) return false; if (n <= 3) return true; // This is checked so that we can skip // middle five numbers in below loop if (n%2 == 0 || n%3 == 0) return false; for (int i=5; i*i<=n; i=i+6) if (n%i == 0 || n%(i+2) == 0) return false; return true; } public static long gcd(long a,long b){ if(b%a == 0){ return a; } return gcd(b%a,a); } // public static void swap(int i,int j){ // long temp = arr[j]; // arr[j] = arr[i]; // arr[i] = temp; // } static final Random random=new Random(); static void ruffleSort(long[] a) { int n=a.length;//shuffle, then sort for (int i=0; i<n; i++) { int oi=random.nextInt(n); long temp=a[oi]; a[oi]=a[i]; a[i]=temp; } Arrays.sort(a); } static class Node{ Integer first; Integer second; Node(Integer f,Integer s){ this.first = f; this.second = s; } } static class FastScanner { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } } }
JAVA
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
#!/usr/bin/env python3 def main(): import collections n, k = map(int, input().split()) k = min(n, k) dq = collections.deque(map(int, input().split())) best = dq.popleft() streak = 0 while streak < k: opp = dq.popleft() if best > opp: dq.append(opp) streak += 1 else: dq.append(best) best = opp streak = 1 print(best) try: while True: main() except EOFError: pass
PYTHON3
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
n,k=map(int,input().split()) a=list(map(int,input().split())) if k>=n: print(max(a)) else: res,z=0,0 for i in range(n+k): if a[res % n]>a[(i+1)%n]: z+=1 else: res,z=i+1,1 if z==k: print(a[res]) break
PYTHON3
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
#include <bits/stdc++.h> using namespace std; const int M = 1e9 + 7; vector<string> vec; long long int sd(long long int n) { long long int sum = 0; while (n != 0) { sum += n % 10; n = n / 10; } return sum; } long long int nd(long long int n) { long long int ans = 0; while (n > 0) { n = n / 10; ans++; } return ans; } long long int power(long long int a, long long int b) { long long int ans = 1ll; while (b) { if (b & 1) ans = (ans * a) % 1000000007; a = (a * a) % 1000000007; b = b / 2; } return ans; } int main() { long long int n, a, b, r, c, d, i, k, j, temp, sum, ct = 0, flag = -1e13, start = 0, next = 1; string st, ans; long long int ar[1005] = {0}; vector<long long int> v; vector<pair<long long int, long long int> > v2; cin >> n >> k; for (i = 0; i < n; i++) { cin >> temp; v.push_back(temp); } if (k > n) { sort(v.begin(), v.end(), greater<long long int>()); cout << v[0]; return 0; } for (i = 0; i < n; i++) { start = (i + 1) % n; ct = 0; if (i != 0) if (v[start] > v[i - 1]) { ct += 1; } while (ct < k) { if (v[start] > v[i]) break; ct++; start++; if (start == n) start = 0; } if (ct == k) { cout << v[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
n, k = map(int, input().split()) s = list(map(int, input().split())) if k>=n-1: print(max(s)) else: dic = {} m = s[0] while(1): a, b = max(s[0], s[1]), min(s[0], s[1]) s[1] = a; s.append(b); s = s[1:] dic[a] = dic.get(a, 0) + 1 if dic[a] == k: print(a); break
PYTHON3
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
n, k = map(int, input().split()) data = list(map(int, input().split())) if k > 3 * n: print(max(data)) exit() q = data cnt = 0 c1, c2 = q[0], q[1] q = q[2:] while cnt < k: if c1 > c2: cnt += 1 else: c1, c2 = c2, c1 cnt = 1 q.append(c2) c2 = q[0] q = q[1:] print(c1)
PYTHON3
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
#include <bits/stdc++.h> using namespace std; const long long mod = 998244353; const long long K = 2; long long add(long long a, long long b, long long mod) { a %= mod; b %= mod; a += b; if (a >= mod) a -= mod; if (a < 0) { a += mod; a %= mod; } return a; } long long mul(long long a, long long b, long long mod) { a %= mod; b %= mod; a *= b; a += mod; return a % mod; } long long po(long long a, long long b, long long mod) { if (b == 0) return 1; if (b % 2 == 0) return po(mul(a, a, mod), b / 2, mod); return mul(a, po(mul(a, a, mod), (b - 1) / 2, mod), mod); } long long fact[1000003]; long long inv[1000003]; void fact0() { long long i, j; fact[0] = 1; for (i = 1; i <= 200000; i++) { fact[i] = i * fact[i - 1] % mod; } inv[0] = 1; inv[1] = 1; long long p = mod; for (i = 2; i <= 200000; i++) inv[i] = (p - (p / i) * inv[p % i] % p) % p; for (i = 2; i <= 200000; i++) { inv[i] *= inv[i - 1]; inv[i] %= mod; } } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long n, k; cin >> n >> k; long long a[n]; for (long long i = 0; i < n; i++) { cin >> a[i]; } long long st = a[0], cc = 0, an = n; vector<long long> v; for (long long i = 1; i < n; i++) { if (st > a[i]) { cc++; v.push_back(a[i]); } else { st = a[i]; v.push_back(st); cc = 1; } if (cc == k) { an = st; break; } } cout << an; return 0; }
CPP
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
n, k = map(int, input().split()) players = list(map(int, input().split())) flag = players[0] winCount = k i = 0 while(winCount > 0): if(k > len(players)): maior = 0 for i in range(len(players)): if(players[i] > maior): maior = players[i] flag = maior break if(flag == players[i]): i += 1 continue else: if(flag > players[i]): aux = players[i] players.remove(aux) players.append(aux) winCount -= 1 else: players.remove(flag) players.append(flag) winCount = k - 1 flag = players[i - 1] i = 0 print(flag)
PYTHON3
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
n, m = map(int, input().split()) powers = list(map(int, input().split())) max_power = powers[0] wins = 0 if(m < n): for power in powers[1:n]: if(max_power > power): wins +=1 else: max_power = power wins = 1 if(wins == m): break if(m >= n or wins < m): max_power = max(powers) 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
n, k = map(int, input().split()) a = list(map(int, input().split())) wins = 0 m = max(a) while(wins < k): if a[0] == m: break if(a[0] > a[1]): if a[0] > m: m = a[0] wins += 1 loser = a[1] a.pop(1) a.append(loser) elif(a[1] > a[0]): if a[1] > m: m = a[1] wins = 1 loser = a[0] a.pop(0) a.append(loser) 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
def ii(): return int(input()) def mi(): return map(int, input().split()) def li(): return list(mi()) n, k = mi() a = li() from collections import deque q = deque(a) c = q.popleft() w = 0 while 1: x = q.popleft() if c > x: w += 1 if w >= k or w >= n: ans = c break q.append(x) else: q.append(c) c = x w = 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
import java.io.*; import java.math.BigInteger; import java.util.*; import java.util.function.BiConsumer; import java.util.function.Consumer; import java.util.function.Function; import java.util.function.Supplier; import java.util.stream.Stream; public class B_443 { public static final long[] POWER2 = generatePOWER2(); public static final IteratorBuffer<Long> ITERATOR_BUFFER_PRIME = new IteratorBuffer<>(streamPrime(1000000).iterator()); private static BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); private static StringTokenizer stringTokenizer = null; private static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); static class Array<Type> implements Iterable<Type> { private final Object[] array; public Array(int size) { this.array = new Object[size]; } public Array(int size, Type element) { this(size); Arrays.fill(this.array, element); } public Array(Array<Type> array, Type element) { this(array.size() + 1); for (int index = 0; index < array.size(); index++) { set(index, array.get(index)); } set(size() - 1, element); } public Array(List<Type> list) { this(list.size()); int index = 0; for (Type element : list) { set(index, element); index += 1; } } public Type get(int index) { return (Type) this.array[index]; } @Override public Iterator<Type> iterator() { return new Iterator<Type>() { int index = 0; @Override public boolean hasNext() { return this.index < size(); } @Override public Type next() { Type result = Array.this.get(index); index += 1; return result; } }; } public Array set(int index, Type value) { this.array[index] = value; return this; } public int size() { return this.array.length; } public List<Type> toList() { List<Type> result = new ArrayList<>(); for (Type element : this) { result.add(element); } return result; } @Override public String toString() { return "[" + B_443.toString(this, ", ") + "]"; } } static class BIT { private static int lastBit(int index) { return index & -index; } private final long[] tree; public BIT(int size) { this.tree = new long[size]; } public void add(int index, long delta) { index += 1; while (index <= this.tree.length) { tree[index - 1] += delta; index += lastBit(index); } } private long prefix(int end) { long result = 0; while (end > 0) { result += this.tree[end - 1]; end -= lastBit(end); } return result; } public int size() { return this.tree.length; } public long sum(int start, int end) { return prefix(end) - prefix(start); } } static abstract class Edge<TypeVertex extends Vertex<TypeVertex, TypeEdge>, TypeEdge extends Edge<TypeVertex, TypeEdge>> { public final TypeVertex vertex0; public final TypeVertex vertex1; public final boolean bidirectional; public Edge(TypeVertex vertex0, TypeVertex vertex1, boolean bidirectional) { this.vertex0 = vertex0; this.vertex1 = vertex1; this.bidirectional = bidirectional; this.vertex0.edges.add(getThis()); if (this.bidirectional) { this.vertex1.edges.add(getThis()); } } public abstract TypeEdge getThis(); public TypeVertex other(Vertex<TypeVertex, TypeEdge> vertex) { TypeVertex result; if (vertex0 == vertex) { result = vertex1; } else { result = vertex0; } return result; } public void remove() { this.vertex0.edges.remove(getThis()); if (this.bidirectional) { this.vertex1.edges.remove(getThis()); } } @Override public String toString() { return this.vertex0 + "->" + this.vertex1; } } public static class EdgeDefault<TypeVertex extends Vertex<TypeVertex, EdgeDefault<TypeVertex>>> extends Edge<TypeVertex, EdgeDefault<TypeVertex>> { public EdgeDefault(TypeVertex vertex0, TypeVertex vertex1, boolean bidirectional) { super(vertex0, vertex1, bidirectional); } @Override public EdgeDefault<TypeVertex> getThis() { return this; } } public static class EdgeDefaultDefault extends Edge<VertexDefaultDefault, EdgeDefaultDefault> { public EdgeDefaultDefault(VertexDefaultDefault vertex0, VertexDefaultDefault vertex1, boolean bidirectional) { super(vertex0, vertex1, bidirectional); } @Override public EdgeDefaultDefault getThis() { return this; } } public static class FIFO<Type> { public SingleLinkedList<Type> start; public SingleLinkedList<Type> end; public FIFO() { this.start = null; this.end = null; } public boolean isEmpty() { return this.start == null; } public Type peek() { return this.start.element; } public Type pop() { Type result = this.start.element; this.start = this.start.next; return result; } public void push(Type element) { SingleLinkedList<Type> list = new SingleLinkedList<>(element, null); if (this.start == null) { this.start = list; this.end = list; } else { this.end.next = list; this.end = list; } } } static class Fraction implements Comparable<Fraction> { public static final Fraction ZERO = new Fraction(0, 1); public static Fraction fraction(long whole) { return fraction(whole, 1); } public static Fraction fraction(long numerator, long denominator) { Fraction result; if (denominator == 0) { throw new ArithmeticException(); } if (numerator == 0) { result = Fraction.ZERO; } else { int sign; if (numerator < 0 ^ denominator < 0) { sign = -1; numerator = Math.abs(numerator); denominator = Math.abs(denominator); } else { sign = 1; } long gcd = gcd(numerator, denominator); result = new Fraction(sign * numerator / gcd, denominator / gcd); } return result; } public final long numerator; public final long denominator; private Fraction(long numerator, long denominator) { this.numerator = numerator; this.denominator = denominator; } public Fraction add(Fraction fraction) { return fraction(this.numerator * fraction.denominator + fraction.numerator * this.denominator, this.denominator * fraction.denominator); } @Override public int compareTo(Fraction that) { return Long.compare(this.numerator * that.denominator, that.numerator * this.denominator); } public Fraction divide(Fraction fraction) { return multiply(fraction.inverse()); } public boolean equals(Fraction that) { return this.compareTo(that) == 0; } public boolean equals(Object that) { return this.compareTo((Fraction) that) == 0; } public Fraction getRemainder() { return fraction(this.numerator - getWholePart() * denominator, denominator); } public long getWholePart() { return this.numerator / this.denominator; } public Fraction inverse() { return fraction(this.denominator, this.numerator); } public Fraction multiply(Fraction fraction) { return fraction(this.numerator * fraction.numerator, this.denominator * fraction.denominator); } public Fraction neg() { return fraction(-this.numerator, this.denominator); } public Fraction sub(Fraction fraction) { return add(fraction.neg()); } @Override public String toString() { String result; if (getRemainder().equals(Fraction.ZERO)) { result = "" + this.numerator; } else { result = this.numerator + "/" + this.denominator; } return result; } } static class IteratorBuffer<Type> { private Iterator<Type> iterator; private List<Type> list; public IteratorBuffer(Iterator<Type> iterator) { this.iterator = iterator; this.list = new ArrayList<Type>(); } public Iterator<Type> iterator() { return new Iterator<Type>() { int index = 0; @Override public boolean hasNext() { return this.index < list.size() || IteratorBuffer.this.iterator.hasNext(); } @Override public Type next() { if (list.size() <= this.index) { list.add(iterator.next()); } Type result = list.get(index); index += 1; return result; } }; } } public static class MapCount<Type> extends SortedMapAVL<Type, Long> { private int count; public MapCount(Comparator<? super Type> comparator) { super(comparator); this.count = 0; } public long add(Type key, Long delta) { long result; if (delta > 0) { Long value = get(key); if (value == null) { value = delta; } else { value += delta; } put(key, value); result = delta; } else { result = 0; } this.count += result; return result; } public int count() { return this.count; } public List<Type> flatten() { List<Type> result = new ArrayList<>(); for (Entry<Type, Long> entry : entrySet()) { for (long index = 0; index < entry.getValue(); index++) { result.add(entry.getKey()); } } return result; } @Override public SortedMapAVL<Type, Long> headMap(Type keyEnd) { throw new UnsupportedOperationException(); } @Override public void putAll(Map<? extends Type, ? extends Long> map) { throw new UnsupportedOperationException(); } public long remove(Type key, Long delta) { long result; if (delta > 0) { Long value = get(key) - delta; if (value <= 0) { result = delta + value; remove(key); } else { result = delta; put(key, value); } } else { result = 0; } this.count -= result; return result; } @Override public Long remove(Object key) { Long result = super.remove(key); this.count -= result; return result; } @Override public SortedMapAVL<Type, Long> subMap(Type keyStart, Type keyEnd) { throw new UnsupportedOperationException(); } @Override public SortedMapAVL<Type, Long> tailMap(Type keyStart) { throw new UnsupportedOperationException(); } } public static class MapSet<TypeKey, TypeValue> extends SortedMapAVL<TypeKey, SortedSetAVL<TypeValue>> implements Iterable<TypeValue> { private Comparator<? super TypeValue> comparatorValue; public MapSet(Comparator<? super TypeKey> comparatorKey, Comparator<? super TypeValue> comparatorValue) { super(comparatorKey); this.comparatorValue = comparatorValue; } public MapSet(Comparator<? super TypeKey> comparatorKey, SortedSetAVL<Entry<TypeKey, SortedSetAVL<TypeValue>>> entrySet, Comparator<? super TypeValue> comparatorValue) { super(comparatorKey, entrySet); this.comparatorValue = comparatorValue; } public boolean add(TypeKey key, TypeValue value) { SortedSetAVL<TypeValue> set = computeIfAbsent(key, k -> new SortedSetAVL<>(comparatorValue)); return set.add(value); } public TypeValue firstValue() { TypeValue result; Entry<TypeKey, SortedSetAVL<TypeValue>> firstEntry = firstEntry(); if (firstEntry == null) { result = null; } else { result = firstEntry.getValue().first(); } return result; } @Override public MapSet<TypeKey, TypeValue> headMap(TypeKey keyEnd) { return new MapSet<>(this.comparator, this.entrySet.headSet(new AbstractMap.SimpleEntry<>(keyEnd, null)), this.comparatorValue); } public Iterator<TypeValue> iterator() { return new Iterator<TypeValue>() { Iterator<SortedSetAVL<TypeValue>> iteratorValues = values().iterator(); Iterator<TypeValue> iteratorValue = null; @Override public boolean hasNext() { return iteratorValues.hasNext() || (iteratorValue != null && iteratorValue.hasNext()); } @Override public TypeValue next() { if (iteratorValue == null || !iteratorValue.hasNext()) { iteratorValue = iteratorValues.next().iterator(); } return iteratorValue.next(); } }; } public TypeValue lastValue() { TypeValue result; Entry<TypeKey, SortedSetAVL<TypeValue>> lastEntry = lastEntry(); if (lastEntry == null) { result = null; } else { result = lastEntry.getValue().last(); } return result; } public boolean removeSet(TypeKey key, TypeValue value) { boolean result; SortedSetAVL<TypeValue> set = get(key); if (set == null) { result = false; } else { result = set.remove(value); if (set.size() == 0) { remove(key); } } return result; } @Override public MapSet<TypeKey, TypeValue> tailMap(TypeKey keyStart) { return new MapSet<>(this.comparator, this.entrySet.tailSet(new AbstractMap.SimpleEntry<>(keyStart, null)), this.comparatorValue); } } public static class Matrix { public final int rows; public final int columns; public final Fraction[][] cells; public Matrix(int rows, int columns) { this.rows = rows; this.columns = columns; this.cells = new Fraction[rows][columns]; for (int row = 0; row < rows; row++) { for (int column = 0; column < columns; column++) { set(row, column, Fraction.ZERO); } } } public void add(int rowSource, int rowTarget, Fraction fraction) { for (int column = 0; column < columns; column++) { this.cells[rowTarget][column] = this.cells[rowTarget][column].add(this.cells[rowSource][column].multiply(fraction)); } } private int columnPivot(int row) { int result = this.columns; for (int column = this.columns - 1; 0 <= column; column--) { if (this.cells[row][column].compareTo(Fraction.ZERO) != 0) { result = column; } } return result; } public void reduce() { for (int rowMinimum = 0; rowMinimum < this.rows; rowMinimum++) { int rowPivot = rowPivot(rowMinimum); if (rowPivot != -1) { int columnPivot = columnPivot(rowPivot); Fraction current = this.cells[rowMinimum][columnPivot]; Fraction pivot = this.cells[rowPivot][columnPivot]; Fraction fraction = pivot.inverse().sub(current.divide(pivot)); add(rowPivot, rowMinimum, fraction); for (int row = rowMinimum + 1; row < this.rows; row++) { if (columnPivot(row) == columnPivot) { add(rowMinimum, row, this.cells[row][columnPivot(row)].neg()); } } } } } private int rowPivot(int rowMinimum) { int result = -1; int pivotColumnMinimum = this.columns; for (int row = rowMinimum; row < this.rows; row++) { int pivotColumn = columnPivot(row); if (pivotColumn < pivotColumnMinimum) { result = row; pivotColumnMinimum = pivotColumn; } } return result; } public void set(int row, int column, Fraction value) { this.cells[row][column] = value; } public String toString() { String result = ""; for (int row = 0; row < rows; row++) { for (int column = 0; column < columns; column++) { result += this.cells[row][column] + "\t"; } result += "\n"; } return result; } } public static class Node<Type> { public static <Type> Node<Type> balance(Node<Type> result) { while (result != null && 1 < Math.abs(height(result.left) - height(result.right))) { if (height(result.left) < height(result.right)) { Node<Type> right = result.right; if (height(right.right) < height(right.left)) { result = new Node<>(result.value, result.left, right.rotateRight()); } result = result.rotateLeft(); } else { Node<Type> left = result.left; if (height(left.left) < height(left.right)) { result = new Node<>(result.value, left.rotateLeft(), result.right); } result = result.rotateRight(); } } return result; } public static <Type> Node<Type> clone(Node<Type> result) { if (result != null) { result = new Node<>(result.value, clone(result.left), clone(result.right)); } return result; } public static <Type> Node<Type> delete(Node<Type> node, Type value, Comparator<? super Type> comparator) { Node<Type> result; if (node == null) { result = null; } else { int compare = comparator.compare(value, node.value); if (compare == 0) { if (node.left == null) { result = node.right; } else { if (node.right == null) { result = node.left; } else { Node<Type> first = first(node.right); result = new Node<>(first.value, node.left, delete(node.right, first.value, comparator)); } } } else { if (compare < 0) { result = new Node<>(node.value, delete(node.left, value, comparator), node.right); } else { result = new Node<>(node.value, node.left, delete(node.right, value, comparator)); } } result = balance(result); } return result; } public static <Type> Node<Type> first(Node<Type> result) { while (result.left != null) { result = result.left; } return result; } public static <Type> Node<Type> get(Node<Type> node, Type value, Comparator<? super Type> comparator) { Node<Type> result; if (node == null) { result = null; } else { int compare = comparator.compare(value, node.value); if (compare == 0) { result = node; } else { if (compare < 0) { result = get(node.left, value, comparator); } else { result = get(node.right, value, comparator); } } } return result; } public static <Type> Node<Type> head(Node<Type> node, Type value, Comparator<? super Type> comparator) { Node<Type> result; if (node == null) { result = null; } else { int compare = comparator.compare(value, node.value); if (compare == 0) { result = node.left; } else { if (compare < 0) { result = head(node.left, value, comparator); } else { result = new Node<>(node.value, node.left, head(node.right, value, comparator)); } } result = balance(result); } return result; } public static int height(Node node) { return node == null ? 0 : node.height; } public static <Type> Node<Type> insert(Node<Type> node, Type value, Comparator<? super Type> comparator) { Node<Type> result; if (node == null) { result = new Node<>(value, null, null); } else { int compare = comparator.compare(value, node.value); if (compare == 0) { result = new Node<>(value, node.left, node.right); ; } else { if (compare < 0) { result = new Node<>(node.value, insert(node.left, value, comparator), node.right); } else { result = new Node<>(node.value, node.left, insert(node.right, value, comparator)); } } result = balance(result); } return result; } public static <Type> Node<Type> last(Node<Type> result) { while (result.right != null) { result = result.right; } return result; } public static int size(Node node) { return node == null ? 0 : node.size; } public static <Type> Node<Type> tail(Node<Type> node, Type value, Comparator<? super Type> comparator) { Node<Type> result; if (node == null) { result = null; } else { int compare = comparator.compare(value, node.value); if (compare == 0) { result = new Node<>(node.value, null, node.right); } else { if (compare < 0) { result = new Node<>(node.value, tail(node.left, value, comparator), node.right); } else { result = tail(node.right, value, comparator); } } result = balance(result); } return result; } public static <Type> void traverseOrderIn(Node<Type> node, Consumer<Type> consumer) { if (node != null) { traverseOrderIn(node.left, consumer); consumer.accept(node.value); traverseOrderIn(node.right, consumer); } } public final Type value; public final Node<Type> left; public final Node<Type> right; public final int size; private final int height; public Node(Type value, Node<Type> left, Node<Type> right) { this.value = value; this.left = left; this.right = right; this.size = 1 + size(left) + size(right); this.height = 1 + Math.max(height(left), height(right)); } public Node<Type> rotateLeft() { Node<Type> left = new Node<>(this.value, this.left, this.right.left); return new Node<>(this.right.value, left, this.right.right); } public Node<Type> rotateRight() { Node<Type> right = new Node<>(this.value, this.left.right, this.right); return new Node<>(this.left.value, this.left.left, right); } } public static class SingleLinkedList<Type> { public final Type element; public SingleLinkedList<Type> next; public SingleLinkedList(Type element, SingleLinkedList<Type> next) { this.element = element; this.next = next; } public void toCollection(Collection<Type> collection) { if (this.next != null) { this.next.toCollection(collection); } collection.add(this.element); } } public static class SmallSetIntegers { public static final int SIZE = 20; public static final int[] SET = generateSet(); public static final int[] COUNT = generateCount(); public static final int[] INTEGER = generateInteger(); private static int count(int set) { int result = 0; for (int integer = 0; integer < SIZE; integer++) { if (0 < (set & set(integer))) { result += 1; } } return result; } private static final int[] generateCount() { int[] result = new int[1 << SIZE]; for (int set = 0; set < result.length; set++) { result[set] = count(set); } return result; } private static final int[] generateInteger() { int[] result = new int[1 << SIZE]; Arrays.fill(result, -1); for (int integer = 0; integer < SIZE; integer++) { result[SET[integer]] = integer; } return result; } private static final int[] generateSet() { int[] result = new int[SIZE]; for (int integer = 0; integer < result.length; integer++) { result[integer] = set(integer); } return result; } private static int set(int integer) { return 1 << integer; } } public static class SortedMapAVL<TypeKey, TypeValue> implements SortedMap<TypeKey, TypeValue> { public final Comparator<? super TypeKey> comparator; public final SortedSetAVL<Entry<TypeKey, TypeValue>> entrySet; public SortedMapAVL(Comparator<? super TypeKey> comparator) { this(comparator, new SortedSetAVL<>((entry0, entry1) -> comparator.compare(entry0.getKey(), entry1.getKey()))); } private SortedMapAVL(Comparator<? super TypeKey> comparator, SortedSetAVL<Entry<TypeKey, TypeValue>> entrySet) { this.comparator = comparator; this.entrySet = entrySet; } @Override public void clear() { this.entrySet.clear(); } @Override public Comparator<? super TypeKey> comparator() { return this.comparator; } @Override public boolean containsKey(Object key) { return this.entrySet().contains(new AbstractMap.SimpleEntry<>((TypeKey) key, null)); } @Override public boolean containsValue(Object value) { throw new UnsupportedOperationException(); } @Override public SortedSetAVL<Entry<TypeKey, TypeValue>> entrySet() { return this.entrySet; } public Entry<TypeKey, TypeValue> firstEntry() { return this.entrySet.first(); } @Override public TypeKey firstKey() { return firstEntry().getKey(); } @Override public TypeValue get(Object key) { Entry<TypeKey, TypeValue> entry = new AbstractMap.SimpleEntry<>((TypeKey) key, null); entry = this.entrySet.get(entry); return entry == null ? null : entry.getValue(); } @Override public SortedMapAVL<TypeKey, TypeValue> headMap(TypeKey keyEnd) { return new SortedMapAVL<>(this.comparator, this.entrySet.headSet(new AbstractMap.SimpleEntry<>(keyEnd, null))); } @Override public boolean isEmpty() { return this.entrySet.isEmpty(); } @Override public Set<TypeKey> keySet() { return new SortedSet<TypeKey>() { @Override public boolean add(TypeKey typeKey) { throw new UnsupportedOperationException(); } @Override public boolean addAll(Collection<? extends TypeKey> collection) { throw new UnsupportedOperationException(); } @Override public void clear() { throw new UnsupportedOperationException(); } @Override public Comparator<? super TypeKey> comparator() { throw new UnsupportedOperationException(); } @Override public boolean contains(Object o) { throw new UnsupportedOperationException(); } @Override public boolean containsAll(Collection<?> collection) { throw new UnsupportedOperationException(); } @Override public TypeKey first() { throw new UnsupportedOperationException(); } @Override public SortedSet<TypeKey> headSet(TypeKey typeKey) { throw new UnsupportedOperationException(); } @Override public boolean isEmpty() { return size() == 0; } @Override public Iterator<TypeKey> iterator() { final Iterator<Entry<TypeKey, TypeValue>> iterator = SortedMapAVL.this.entrySet.iterator(); return new Iterator<TypeKey>() { @Override public boolean hasNext() { return iterator.hasNext(); } @Override public TypeKey next() { return iterator.next().getKey(); } }; } @Override public TypeKey last() { throw new UnsupportedOperationException(); } @Override public boolean remove(Object o) { throw new UnsupportedOperationException(); } @Override public boolean removeAll(Collection<?> collection) { throw new UnsupportedOperationException(); } @Override public boolean retainAll(Collection<?> collection) { throw new UnsupportedOperationException(); } @Override public int size() { return SortedMapAVL.this.entrySet.size(); } @Override public SortedSet<TypeKey> subSet(TypeKey typeKey, TypeKey e1) { throw new UnsupportedOperationException(); } @Override public SortedSet<TypeKey> tailSet(TypeKey typeKey) { throw new UnsupportedOperationException(); } @Override public Object[] toArray() { throw new UnsupportedOperationException(); } @Override public <T> T[] toArray(T[] ts) { throw new UnsupportedOperationException(); } }; } public Entry<TypeKey, TypeValue> lastEntry() { return this.entrySet.last(); } @Override public TypeKey lastKey() { return lastEntry().getKey(); } @Override public TypeValue put(TypeKey key, TypeValue value) { TypeValue result = get(key); Entry<TypeKey, TypeValue> entry = new AbstractMap.SimpleEntry<>(key, value); this.entrySet().add(entry); return result; } @Override public void putAll(Map<? extends TypeKey, ? extends TypeValue> map) { map.entrySet() .forEach(entry -> put(entry.getKey(), entry.getValue())); } @Override public TypeValue remove(Object key) { TypeValue result = get(key); Entry<TypeKey, TypeValue> entry = new AbstractMap.SimpleEntry<>((TypeKey) key, null); this.entrySet.remove(entry); return result; } @Override public int size() { return this.entrySet().size(); } @Override public SortedMapAVL<TypeKey, TypeValue> subMap(TypeKey keyStart, TypeKey keyEnd) { return new SortedMapAVL<>(this.comparator, this.entrySet.subSet(new AbstractMap.SimpleEntry<>(keyStart, null), new AbstractMap.SimpleEntry<>(keyEnd, null))); } @Override public SortedMapAVL<TypeKey, TypeValue> tailMap(TypeKey keyStart) { return new SortedMapAVL<>(this.comparator, this.entrySet.tailSet(new AbstractMap.SimpleEntry<>(keyStart, null))); } @Override public String toString() { return this.entrySet().toString(); } @Override public Collection<TypeValue> values() { return new Collection<TypeValue>() { @Override public boolean add(TypeValue typeValue) { throw new UnsupportedOperationException(); } @Override public boolean addAll(Collection<? extends TypeValue> collection) { throw new UnsupportedOperationException(); } @Override public void clear() { throw new UnsupportedOperationException(); } @Override public boolean contains(Object value) { throw new UnsupportedOperationException(); } @Override public boolean containsAll(Collection<?> collection) { throw new UnsupportedOperationException(); } @Override public boolean isEmpty() { return SortedMapAVL.this.entrySet.isEmpty(); } @Override public Iterator<TypeValue> iterator() { return new Iterator<TypeValue>() { Iterator<Entry<TypeKey, TypeValue>> iterator = SortedMapAVL.this.entrySet.iterator(); @Override public boolean hasNext() { return this.iterator.hasNext(); } @Override public TypeValue next() { return this.iterator.next().getValue(); } }; } @Override public boolean remove(Object o) { throw new UnsupportedOperationException(); } @Override public boolean removeAll(Collection<?> collection) { throw new UnsupportedOperationException(); } @Override public boolean retainAll(Collection<?> collection) { throw new UnsupportedOperationException(); } @Override public int size() { return SortedMapAVL.this.entrySet.size(); } @Override public Object[] toArray() { throw new UnsupportedOperationException(); } @Override public <T> T[] toArray(T[] ts) { throw new UnsupportedOperationException(); } }; } } public static class SortedSetAVL<Type> implements SortedSet<Type> { public Comparator<? super Type> comparator; public Node<Type> root; private SortedSetAVL(Comparator<? super Type> comparator, Node<Type> root) { this.comparator = comparator; this.root = root; } public SortedSetAVL(Comparator<? super Type> comparator) { this(comparator, null); } public SortedSetAVL(Collection<? extends Type> collection, Comparator<? super Type> comparator) { this(comparator, null); this.addAll(collection); } public SortedSetAVL(SortedSetAVL<Type> sortedSetAVL) { this(sortedSetAVL.comparator, Node.clone(sortedSetAVL.root)); } @Override public boolean add(Type value) { int sizeBefore = size(); this.root = Node.insert(this.root, value, this.comparator); return sizeBefore != size(); } @Override public boolean addAll(Collection<? extends Type> collection) { return collection.stream() .map(this::add) .reduce(true, (x, y) -> x | y); } @Override public void clear() { this.root = null; } @Override public Comparator<? super Type> comparator() { return this.comparator; } @Override public boolean contains(Object value) { return Node.get(this.root, (Type) value, this.comparator) != null; } @Override public boolean containsAll(Collection<?> collection) { return collection.stream() .allMatch(this::contains); } @Override public Type first() { return Node.first(this.root).value; } public Type get(Type value) { Node<Type> node = Node.get(this.root, value, this.comparator); return node == null ? null : node.value; } @Override public SortedSetAVL<Type> headSet(Type valueEnd) { return new SortedSetAVL<>(this.comparator, Node.head(this.root, valueEnd, this.comparator)); } @Override public boolean isEmpty() { return this.root == null; } @Override public Iterator<Type> iterator() { Stack<Node<Type>> path = new Stack<>(); return new Iterator<Type>() { { push(SortedSetAVL.this.root); } @Override public boolean hasNext() { return !path.isEmpty(); } @Override public Type next() { if (path.isEmpty()) { throw new NoSuchElementException(); } else { Node<Type> node = path.peek(); Type result = node.value; if (node.right != null) { push(node.right); } else { do { node = path.pop(); } while (!path.isEmpty() && path.peek().right == node); } return result; } } public void push(Node<Type> node) { while (node != null) { path.push(node); node = node.left; } } }; } @Override public Type last() { return Node.last(this.root).value; } @Override public boolean remove(Object value) { int sizeBefore = size(); this.root = Node.delete(this.root, (Type) value, this.comparator); return sizeBefore != size(); } @Override public boolean removeAll(Collection<?> collection) { return collection.stream() .map(this::remove) .reduce(true, (x, y) -> x | y); } @Override public boolean retainAll(Collection<?> collection) { SortedSetAVL<Type> set = new SortedSetAVL<>(this.comparator); collection.stream() .map(element -> (Type) element) .filter(this::contains) .forEach(set::add); boolean result = size() != set.size(); this.root = set.root; return result; } @Override public int size() { return this.root == null ? 0 : this.root.size; } @Override public SortedSetAVL<Type> subSet(Type valueStart, Type valueEnd) { return tailSet(valueStart).headSet(valueEnd); } @Override public SortedSetAVL<Type> tailSet(Type valueStart) { return new SortedSetAVL<>(this.comparator, Node.tail(this.root, valueStart, this.comparator)); } @Override public Object[] toArray() { return toArray(new Object[0]); } @Override public <T> T[] toArray(T[] ts) { List<Object> list = new ArrayList<>(); Node.traverseOrderIn(this.root, list::add); return list.toArray(ts); } @Override public String toString() { return "{" + B_443.toString(this, ", ") + "}"; } } public static class Tree2D { public static final int SIZE = 1 << 30; public static final Tree2D[] TREES_NULL = new Tree2D[] { null, null, null, null }; public static boolean contains(int x, int y, int left, int bottom, int size) { return left <= x && x < left + size && bottom <= y && y < bottom + size; } public static int count(Tree2D[] trees) { int result = 0; for (int index = 0; index < 4; index++) { if (trees[index] != null) { result += trees[index].count; } } return result; } public static int count ( int rectangleLeft, int rectangleBottom, int rectangleRight, int rectangleTop, Tree2D tree, int left, int bottom, int size ) { int result; if (tree == null) { result = 0; } else { int right = left + size; int top = bottom + size; int intersectionLeft = Math.max(rectangleLeft, left); int intersectionBottom = Math.max(rectangleBottom, bottom); int intersectionRight = Math.min(rectangleRight, right); int intersectionTop = Math.min(rectangleTop, top); if (intersectionRight <= intersectionLeft || intersectionTop <= intersectionBottom) { result = 0; } else { if (intersectionLeft == left && intersectionBottom == bottom && intersectionRight == right && intersectionTop == top) { result = tree.count; } else { size = size >> 1; result = 0; for (int index = 0; index < 4; index++) { result += count ( rectangleLeft, rectangleBottom, rectangleRight, rectangleTop, tree.trees[index], quadrantLeft(left, size, index), quadrantBottom(bottom, size, index), size ); } } } } return result; } public static int quadrantBottom(int bottom, int size, int index) { return bottom + (index >> 1) * size; } public static int quadrantLeft(int left, int size, int index) { return left + (index & 1) * size; } public final Tree2D[] trees; public final int count; private Tree2D(Tree2D[] trees, int count) { this.trees = trees; this.count = count; } public Tree2D(Tree2D[] trees) { this(trees, count(trees)); } public Tree2D() { this(TREES_NULL); } public int count(int rectangleLeft, int rectangleBottom, int rectangleRight, int rectangleTop) { return count ( rectangleLeft, rectangleBottom, rectangleRight, rectangleTop, this, 0, 0, SIZE ); } public Tree2D setPoint ( int x, int y, Tree2D tree, int left, int bottom, int size ) { Tree2D result; if (contains(x, y, left, bottom, size)) { if (size == 1) { result = new Tree2D(TREES_NULL, 1); } else { size = size >> 1; Tree2D[] trees = new Tree2D[4]; for (int index = 0; index < 4; index++) { trees[index] = setPoint ( x, y, tree == null ? null : tree.trees[index], quadrantLeft(left, size, index), quadrantBottom(bottom, size, index), size ); } result = new Tree2D(trees); } } else { result = tree; } return result; } public Tree2D setPoint(int x, int y) { return setPoint ( x, y, this, 0, 0, SIZE ); } } public static class Tuple2<Type0, Type1> { public final Type0 v0; public final Type1 v1; public Tuple2(Type0 v0, Type1 v1) { this.v0 = v0; this.v1 = v1; } @Override public String toString() { return "(" + this.v0 + ", " + this.v1 + ")"; } } public static class Tuple2Comparable<Type0 extends Comparable<? super Type0>, Type1 extends Comparable<? super Type1>> extends Tuple2<Type0, Type1> implements Comparable<Tuple2Comparable<Type0, Type1>> { public Tuple2Comparable(Type0 v0, Type1 v1) { super(v0, v1); } @Override public int compareTo(Tuple2Comparable<Type0, Type1> that) { int result = this.v0.compareTo(that.v0); if (result == 0) { result = this.v1.compareTo(that.v1); } return result; } } public static class Tuple3<Type0, Type1, Type2> { public final Type0 v0; public final Type1 v1; public final Type2 v2; public Tuple3(Type0 v0, Type1 v1, Type2 v2) { this.v0 = v0; this.v1 = v1; this.v2 = v2; } @Override public String toString() { return "(" + this.v0 + ", " + this.v1 + ", " + this.v2 + ")"; } } public static class Vertex < TypeVertex extends Vertex<TypeVertex, TypeEdge>, TypeEdge extends Edge<TypeVertex, TypeEdge> > implements Comparable<Vertex<? super TypeVertex, ? super TypeEdge>> { public static < TypeVertex extends Vertex<TypeVertex, TypeEdge>, TypeEdge extends Edge<TypeVertex, TypeEdge>, TypeResult > TypeResult breadthFirstSearch ( TypeVertex vertex, TypeEdge edge, BiFunctionResult<TypeVertex, TypeEdge, TypeResult> function, Array<Boolean> visited, FIFO<TypeVertex> verticesNext, FIFO<TypeEdge> edgesNext, TypeResult result ) { if (!visited.get(vertex.index)) { visited.set(vertex.index, true); result = function.apply(vertex, edge, result); for (TypeEdge edgeNext : vertex.edges) { TypeVertex vertexNext = edgeNext.other(vertex); if (!visited.get(vertexNext.index)) { verticesNext.push(vertexNext); edgesNext.push(edgeNext); } } } return result; } public static < TypeVertex extends Vertex<TypeVertex, TypeEdge>, TypeEdge extends Edge<TypeVertex, TypeEdge>, TypeResult > TypeResult breadthFirstSearch ( Array<TypeVertex> vertices, int indexVertexStart, BiFunctionResult<TypeVertex, TypeEdge, TypeResult> function, TypeResult result ) { Array<Boolean> visited = new Array<>(vertices.size(), false); FIFO<TypeVertex> verticesNext = new FIFO<>(); verticesNext.push(vertices.get(indexVertexStart)); FIFO<TypeEdge> edgesNext = new FIFO<>(); edgesNext.push(null); while (!verticesNext.isEmpty()) { result = breadthFirstSearch(verticesNext.pop(), edgesNext.pop(), function, visited, verticesNext, edgesNext, result); } return result; } public static < TypeVertex extends Vertex<TypeVertex, TypeEdge>, TypeEdge extends Edge<TypeVertex, TypeEdge> > boolean cycle ( TypeVertex start, SortedSet<TypeVertex> result ) { boolean cycle = false; Stack<TypeVertex> stackVertex = new Stack<>(); Stack<TypeEdge> stackEdge = new Stack<>(); stackVertex.push(start); stackEdge.push(null); while (!stackVertex.isEmpty()) { TypeVertex vertex = stackVertex.pop(); TypeEdge edge = stackEdge.pop(); if (!result.contains(vertex)) { result.add(vertex); for (TypeEdge otherEdge : vertex.edges) { if (otherEdge != edge) { TypeVertex otherVertex = otherEdge.other(vertex); if (result.contains(otherVertex)) { cycle = true; } else { stackVertex.push(otherVertex); stackEdge.push(otherEdge); } } } } } return cycle; } public static < TypeVertex extends Vertex<TypeVertex, TypeEdge>, TypeEdge extends Edge<TypeVertex, TypeEdge> > SortedSet<TypeVertex> depthFirstSearch ( TypeVertex start, BiConsumer<TypeVertex, TypeEdge> functionVisitPre, BiConsumer<TypeVertex, TypeEdge> functionVisitPost ) { SortedSet<TypeVertex> result = new SortedSetAVL<>(Comparator.naturalOrder()); Stack<TypeVertex> stackVertex = new Stack<>(); Stack<TypeEdge> stackEdge = new Stack<>(); stackVertex.push(start); stackEdge.push(null); while (!stackVertex.isEmpty()) { TypeVertex vertex = stackVertex.pop(); TypeEdge edge = stackEdge.pop(); if (result.contains(vertex)) { functionVisitPost.accept(vertex, edge); } else { result.add(vertex); stackVertex.push(vertex); stackEdge.push(edge); functionVisitPre.accept(vertex, edge); for (TypeEdge otherEdge : vertex.edges) { TypeVertex otherVertex = otherEdge.other(vertex); if (!result.contains(otherVertex)) { stackVertex.push(otherVertex); stackEdge.push(otherEdge); } } } } return result; } public static < TypeVertex extends Vertex<TypeVertex, TypeEdge>, TypeEdge extends Edge<TypeVertex, TypeEdge> > SortedSet<TypeVertex> depthFirstSearch ( TypeVertex start, Consumer<TypeVertex> functionVisitPreVertex, Consumer<TypeVertex> functionVisitPostVertex ) { BiConsumer<TypeVertex, TypeEdge> functionVisitPreVertexEdge = (vertex, edge) -> { functionVisitPreVertex.accept(vertex); }; BiConsumer<TypeVertex, TypeEdge> functionVisitPostVertexEdge = (vertex, edge) -> { functionVisitPostVertex.accept(vertex); }; return depthFirstSearch(start, functionVisitPreVertexEdge, functionVisitPostVertexEdge); } public final int index; public final List<TypeEdge> edges; public Vertex(int index) { this.index = index; this.edges = new ArrayList<>(); } @Override public int compareTo(Vertex<? super TypeVertex, ? super TypeEdge> that) { return Integer.compare(this.index, that.index); } @Override public String toString() { return "" + this.index; } } public static class VertexDefault<TypeEdge extends Edge<VertexDefault<TypeEdge>, TypeEdge>> extends Vertex<VertexDefault<TypeEdge>, TypeEdge> { public VertexDefault(int index) { super(index); } } public static class VertexDefaultDefault extends Vertex<VertexDefaultDefault, EdgeDefaultDefault> { public static Array<VertexDefaultDefault> vertices(int n) { Array<VertexDefaultDefault> result = new Array<>(n); for (int index = 0; index < n; index++) { result.set(index, new VertexDefaultDefault(index)); } return result; } public VertexDefaultDefault(int index) { super(index); } } public static class Wrapper<Type> { public Type value; public Wrapper(Type value) { this.value = value; } public Type get() { return this.value; } public void set(Type value) { this.value = value; } @Override public String toString() { return this.value.toString(); } } public static void add(int delta, int[] result) { for (int index = 0; index < result.length; index++) { result[index] += delta; } } public static void add(int delta, int[]... result) { for (int index = 0; index < result.length; index++) { add(delta, result[index]); } } public static int binarySearchMaximum(Function<Integer, Boolean> filter, int start, int end) { return -binarySearchMinimum(x -> filter.apply(-x), -end, -start); } public static int binarySearchMinimum(Function<Integer, Boolean> filter, int start, int end) { int result; if (start == end) { result = end; } else { int middle = start + (end - start) / 2; if (filter.apply(middle)) { result = binarySearchMinimum(filter, start, middle); } else { result = binarySearchMinimum(filter, middle + 1, end); } } return result; } public static void close() { out.close(); } public static List<List<Integer>> combinations(int n, int k) { List<List<Integer>> result = new ArrayList<>(); if (k == 0) { } else { if (k == 1) { List<Integer> combination = new ArrayList<>(); combination.add(n); result.add(combination); } else { for (int index = 0; index <= n; index++) { for (List<Integer> combination : combinations(n - index, k - 1)) { combination.add(index); result.add(combination); } } } } return result; } public static <Type> int compare(Iterator<Type> iterator0, Iterator<Type> iterator1, Comparator<Type> comparator) { int result = 0; while (result == 0 && iterator0.hasNext() && iterator1.hasNext()) { result = comparator.compare(iterator0.next(), iterator1.next()); } if (result == 0) { if (iterator1.hasNext()) { result = -1; } else { if (iterator0.hasNext()) { result = 1; } } } return result; } public static <Type> int compare(Iterable<Type> iterable0, Iterable<Type> iterable1, Comparator<Type> comparator) { return compare(iterable0.iterator(), iterable1.iterator(), comparator); } public static long divideCeil(long x, long y) { return (x + y - 1) / y; } public static Set<Long> divisors(long n) { SortedSetAVL<Long> result = new SortedSetAVL<>(Comparator.naturalOrder()); result.add(1L); for (Long factor : factors(n)) { SortedSetAVL<Long> divisors = new SortedSetAVL<>(result); for (Long divisor : result) { divisors.add(divisor * factor); } result = divisors; } return result; } public static LinkedList<Long> factors(long n) { LinkedList<Long> result = new LinkedList<>(); Iterator<Long> primes = ITERATOR_BUFFER_PRIME.iterator(); Long prime; while (n > 1 && (prime = primes.next()) * prime <= n) { while (n % prime == 0) { result.add(prime); n /= prime; } } if (n > 1) { result.add(n); } return result; } public static long faculty(int n) { long result = 1; for (int index = 2; index <= n; index++) { result *= index; } return result; } public static long gcd(long a, long b) { while (a != 0 && b != 0) { if (a > b) { a %= b; } else { b %= a; } } return a + b; } public static long[] generatePOWER2() { long[] result = new long[63]; for (int x = 0; x < result.length; x++) { result[x] = 1L << x; } return result; } public static boolean isPrime(long x) { boolean result = x > 1; Iterator<Long> iterator = ITERATOR_BUFFER_PRIME.iterator(); Long prime; while ((prime = iterator.next()) * prime <= x) { result &= x % prime > 0; } return result; } public static long knapsack(List<Tuple3<Long, Integer, Integer>> itemsValueWeightCount, int weightMaximum) { long[] valuesMaximum = new long[weightMaximum + 1]; for (Tuple3<Long, Integer, Integer> itemValueWeightCount : itemsValueWeightCount) { long itemValue = itemValueWeightCount.v0; int itemWeight = itemValueWeightCount.v1; int itemCount = itemValueWeightCount.v2; for (int weight = weightMaximum; 0 <= weight; weight--) { for (int index = 1; index <= itemCount && 0 <= weight - index * itemWeight; index++) { valuesMaximum[weight] = Math.max(valuesMaximum[weight], valuesMaximum[weight - index * itemWeight] + index * itemValue); } } } long result = 0; for (long valueMaximum : valuesMaximum) { result = Math.max(result, valueMaximum); } return result; } public static boolean knapsackPossible(List<Tuple2<Integer, Integer>> itemsWeightCount, int weightMaximum) { boolean[] weightPossible = new boolean[weightMaximum + 1]; weightPossible[0] = true; int weightLargest = 0; for (Tuple2<Integer, Integer> itemWeightCount : itemsWeightCount) { int itemWeight = itemWeightCount.v0; int itemCount = itemWeightCount.v1; for (int weightStart = 0; weightStart < itemWeight; weightStart++) { int count = 0; for (int weight = weightStart; weight <= weightMaximum && (0 < count || weight <= weightLargest); weight += itemWeight) { if (weightPossible[weight]) { count = itemCount; } else { if (0 < count) { weightPossible[weight] = true; weightLargest = weight; count -= 1; } } } } } return weightPossible[weightMaximum]; } public static long lcm(int a, int b) { return a * b / gcd(a, b); } public static void main(String[] args) { try { solve(); } catch (IOException exception) { exception.printStackTrace(); } close(); } public static double nextDouble() throws IOException { return Double.parseDouble(nextString()); } public static int nextInt() throws IOException { return Integer.parseInt(nextString()); } public static void nextInts(int n, int[]... result) throws IOException { for (int index = 0; index < n; index++) { for (int value = 0; value < result.length; value++) { result[value][index] = nextInt(); } } } public static int[] nextInts(int n) throws IOException { int[] result = new int[n]; nextInts(n, result); return result; } public static String nextLine() throws IOException { return bufferedReader.readLine(); } public static long nextLong() throws IOException { return Long.parseLong(nextString()); } public static void nextLongs(int n, long[]... result) throws IOException { for (int index = 0; index < n; index++) { for (int value = 0; value < result.length; value++) { result[value][index] = nextLong(); } } } public static long[] nextLongs(int n) throws IOException { long[] result = new long[n]; nextLongs(n, result); return result; } public static String nextString() throws IOException { while ((stringTokenizer == null) || (!stringTokenizer.hasMoreTokens())) { stringTokenizer = new StringTokenizer(bufferedReader.readLine()); } return stringTokenizer.nextToken(); } public static String[] nextStrings(int n) throws IOException { String[] result = new String[n]; { for (int index = 0; index < n; index++) { result[index] = nextString(); } } return result; } public static <T> List<T> permutation(long p, List<T> x) { List<T> copy = new ArrayList<>(); for (int index = 0; index < x.size(); index++) { copy.add(x.get(index)); } List<T> result = new ArrayList<>(); for (int indexTo = 0; indexTo < x.size(); indexTo++) { int indexFrom = (int) p % copy.size(); p = p / copy.size(); result.add(copy.remove(indexFrom)); } return result; } public static <Type> List<List<Type>> permutations(List<Type> list) { List<List<Type>> result = new ArrayList<>(); result.add(new ArrayList<>()); for (Type element : list) { List<List<Type>> permutations = result; result = new ArrayList<>(); for (List<Type> permutation : permutations) { for (int index = 0; index <= permutation.size(); index++) { List<Type> permutationNew = new ArrayList<>(permutation); permutationNew.add(index, element); result.add(permutationNew); } } } return result; } public static Stream<BigInteger> streamFibonacci() { return Stream.generate(new Supplier<BigInteger>() { private BigInteger n0 = BigInteger.ZERO; private BigInteger n1 = BigInteger.ONE; @Override public BigInteger get() { BigInteger result = n0; n0 = n1; n1 = result.add(n0); return result; } }); } public static Stream<Long> streamPrime(int sieveSize) { return Stream.generate(new Supplier<Long>() { private boolean[] isPrime = new boolean[sieveSize]; private long sieveOffset = 2; private List<Long> primes = new ArrayList<>(); private int index = 0; public void filter(long prime, boolean[] result) { if (prime * prime < this.sieveOffset + sieveSize) { long remainingStart = this.sieveOffset % prime; long start = remainingStart == 0 ? 0 : prime - remainingStart; for (long index = start; index < sieveSize; index += prime) { result[(int) index] = false; } } } public void generatePrimes() { Arrays.fill(this.isPrime, true); this.primes.forEach(prime -> filter(prime, isPrime)); for (int index = 0; index < sieveSize; index++) { if (isPrime[index]) { this.primes.add(this.sieveOffset + index); filter(this.sieveOffset + index, isPrime); } } this.sieveOffset += sieveSize; } @Override public Long get() { while (this.primes.size() <= this.index) { generatePrimes(); } Long result = this.primes.get(this.index); this.index += 1; return result; } }); } public static <Type> String toString(Iterator<Type> iterator, String separator) { StringBuilder stringBuilder = new StringBuilder(); if (iterator.hasNext()) { stringBuilder.append(iterator.next()); } while (iterator.hasNext()) { stringBuilder.append(separator); stringBuilder.append(iterator.next()); } return stringBuilder.toString(); } public static <Type> String toString(Iterator<Type> iterator) { return toString(iterator, " "); } public static <Type> String toString(Iterable<Type> iterable, String separator) { return toString(iterable.iterator(), separator); } public static <Type> String toString(Iterable<Type> iterable) { return toString(iterable, " "); } public static long totient(long n) { Set<Long> factors = new SortedSetAVL<>(factors(n), Comparator.naturalOrder()); long result = n; for (long p : factors) { result -= result / p; } return result; } interface BiFunctionResult<Type0, Type1, TypeResult> { TypeResult apply(Type0 x0, Type1 x1, TypeResult x2); } public static void solve() throws IOException { int n = nextInt(); long k = nextLong(); int[] as = nextInts(n); k = Math.min(k, n - 1); LinkedList<Integer> line = new LinkedList<>(); for (int a : as) { line.addLast(a); } Integer winner = line.pop(); long gamesWon = 0; while (gamesWon < k) { Integer challenger = line.pop(); if (challenger < winner) { line.addLast(challenger); gamesWon += 1; } else { line.addLast(winner); winner = challenger; gamesWon = 1; } } 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
#include <bits/stdc++.h> using namespace std; long long k; int n, a[501]; int main(int argc, char *argv[]) { cin >> n >> k; for (int i = 0; i < n; i++) cin >> a[i]; int s = 0; for (int i = 0; i < n; i++) { if (s >= k) { return !printf("%d\n", a[0]); } if (a[0] > a[1]) { s++; } else { s = 1; swap(a[0], a[1]); } int tmp = a[1]; for (int j = 2; j < n; j++) a[j - 1] = a[j]; a[n - 1] = tmp; } 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
#include <bits/stdc++.h> using namespace std; int main(int argc, char const *argv[]) { long long int n, k, cnt = 0, mxx = 0, curr = 0; scanf("%lld%lld", &n, &k); long long int err[n]; for (int i = 0; i < n; ++i) { scanf("%lld", &err[i]); } curr = err[0]; for (int i = 1; i < n; ++i) { if (err[i] > curr) { curr = err[i]; cnt = 1; } else { ++cnt; if (cnt >= k) { break; } } } printf("%lld\n", curr); return 0; }
CPP
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
def tennis(players, consWins, winCount): if consWins >= (len(players)-1): print(max(players)) elif winCount == consWins: print(players[0]) elif players[0] > players[1]: players = [players[i] for i in range(len(players)) if i != 1] + [players[1]] return tennis(players, consWins, winCount+1) elif players[1] > players[0]: players = [players[i] for i in range(len(players)) if i != 0] + [players[0]] return tennis(players, consWins, 1) nk = input().split() n = int(nk[0]) k = int(nk[1]) players = input().split() players = [int(players[i]) for i in range(n)] tennis(players, k, 0)
PYTHON3
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
def main(): n,k = map(int,input().split()) l = list(map(int,input().split())) res = 0 count = 0 if n < k: res = max(l) else: while count != k: if l[0] > l[1]: count +=1 l = swap(l,1) else: count = 1 l = swap(l,0) res = l[0] print(res) def swap(l, i): tmp = l[i] l.pop(i) l.append(tmp) return l main()
PYTHON3
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
import java.util.*; public class p443_2 { public static void main(String args[]) { Scanner ex=new Scanner(System.in); int n=ex.nextInt(); long k=ex.nextLong(); long arr[]=new long[250001]; int st=0,end=n; for(int i=0;i<n;i++) { arr[i]=ex.nextInt(); } while(true) { int i; for(i=st;i<end;i++) if(arr[st]<arr[i]) break; int count=i-st-1; if(i==end&&arr[i-1]<arr[st]) { System.out.println(arr[st]); break; } else { if((st==0&&count>=k)||(count+1)>=k&&st!=0) { System.out.println(arr[st]); break; } else { for(int j=0;j<count;j++) arr[end+j]=arr[st+j+1]; arr[end+count]=arr[st]; st=i; end=end+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.awt.List; import java.io.*; import java.util.*; /* * * daiict * * * */ public class prac { FastScanner in; PrintWriter out; static ArrayList[] adj; static int n; static int m; static int[] dp1; static int[] dp2; static int color[]; static int visited[]; static long[] arr; static long[] brr; static int[][] ans; static int array[]; static int pref[]; static int tree[]; static int maxval = 32; static int sum = 0; //for dfs do not delete static int dir[][] ={{1,0},{0,1},{0,-1},{-1,0},{1,1},{1,-1},{-1,1},{-1,-1}}; static long[] seg; static long count = 1; static long mod = (long)1e9 + 7; static class Pair implements Comparable<Pair> { int x;int y; Pair(int l, int m) { this.x = l; this.y = m; } public int compareTo(Pair p) { if (this.x == p.x) { return (int)(this.y-p.y); } return (int)(this.x - p.x); } public String toString(){ return x + " " + y; } public boolean equals(Object o){ if(o instanceof Pair){ Pair a = (Pair)o; return this.x == a.x; } return false; } public int hashCode(){ return new Long(x).hashCode()*31 + new Long(y).hashCode(); } } public static int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a%b); } void build(int n) { // build the tree for (int i = n - 1; i > 0; --i) seg[i] = Math.max(seg[i<<1],seg[i<<1|1]); } public static void modify(int p, int i,long val,int n) { // set value at position p if(i == 1){ long temp = val; val = seg[p+n] - arr[p] + val; arr[p] = temp; }else{ val = Math.min(val, p) + arr[p]; brr[p] = val; } for (seg[p += n] = val; p > 1; p >>= 1) seg[p>>1] = Math.max(seg[p],seg[p^1]); } long query(int l, int r,int n) { // max on interval [l, r) long res = Integer.MIN_VALUE; for (l += n, r += n; l < r; l >>= 1, r >>= 1) { if ((l&1) == 1) res = (int)Math.max(res,seg[l++]); if ((r&1) == 1) res = (int)Math.max(res,seg[--r]); } return res; } public static void update(int x,int val){ while(x<=maxval){ tree[x] += val; x = (x&-x); } } public static int read(int idx){ int sum = 0; while(idx>= 0){ sum += tree[idx]; idx -= (idx&(-idx)); } return sum; } void solve() throws NumberFormatException, IOException { //Scanner in = new Scanner(System.in); StringBuilder sb = new StringBuilder(); LinkedList<Integer> q = new LinkedList(); int n = in.nextInt(); long k = in.nextLong(); int arr[] = new int[n]; int max = 0; for(int i = 0;i<n;i++){ arr[i] = in.nextInt(); max = Math.max(max, arr[i]); } if(k >= n-1){ out.println(max); }else{ int ans = arr[0]; int c = 0; for(int i = 1;i<n;i++){ q.addLast(arr[i]); } while(c<k){ int d = q.removeFirst(); if(d > ans){ ans = d; c = 1; q.addLast(ans); }else{ c++; q.addLast(d); } } out.println(ans); } } /** * * @param a * @param left * @param right * @param value * @return the smallest index i which a[i] >= value in range [left, right) if exists, otherwise return -1 */ int ff(long[] a, int left, int right, int value) { int low = left; int high = right; while (low != high) { int mid = (low + high) / 2; if (a[mid] < value) { low = mid + 1; } else { high = mid; } } if (low == right) { return low; } return low; } /** * * @param a * @param left * @param right * @param value * @return greatest index i which a[i] <= value in range [left, right) if exists, otherwise return a number out of [left, right) */ int ffff(long[] a, int left, int right, int value) { while (left != right) { int mid = (left + right) >> 1; if (a[mid] <= value) { left = mid + 1; } else { right = mid; } } if (left < 0) { left *= -1; } return --left; } public static String reverse(String s){ String ans = ""; for(int i = s.length()-1;i>=0;i--){ ans += s.charAt(i); } return ans; } public static long sum(long x){ return x<10?x:x%10 + sum(x/10); } public static long gcd(long x, long y) { if (x == 0) return y; else return gcd( y % x,x); } static long inv(long x , long mod) { long r,y; for(r=1,y=mod-2;y!=0;x=x*x%mod,y>>=1) { if((y&1)==1) r=r*x%mod; } return r; } public static long pow(long x,long y,long n){ if(y==0) return 1%n; if(y%2==0){ long z=pow(x,y/2,n); return (z*z)%n; } return ((x%n)*pow(x,y-1,n))%n; } public static boolean isPrime(int n) { // Corner cases if (n <= 1) return false; if (n <= 3) return true; // This is checked so that we can skip // middle five numbers in below loop if (n % 2 == 0 || n % 3 == 0) return false; for (int i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } void run() throws NumberFormatException, IOException { in = new FastScanner(); out = new PrintWriter(System.out); solve(); out.close(); } class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(in.br.readLine()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } } public static void main(String[] args) throws NumberFormatException, IOException { new prac().run(); } }
JAVA
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
n, k = map(int, input().split()) a = list(map(int, input().split())) curr_streak = 0 curr_power = a[0] for i in range(1, n): if curr_power > a[i]: curr_streak += 1 else: curr_power = a[i] curr_streak = 1 if curr_streak >= k: print(curr_power) exit() print(curr_power)
PYTHON3
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
#include <bits/stdc++.h> using namespace std; int main() { long long n, k; cin >> n >> k; vector<long long> a; for (long long i = 0; i < n; i++) { long long d; cin >> d; a.push_back(d); } long long d = *max_element(a.begin(), a.end()); if (k >= n) { cout << d; } else { long long ans = 0; long long i = 0; long long j = 1; while (ans != k) { if (a[i] > a[j]) { ans++; a.push_back(a[j]); j++; if (ans == k) { cout << a[i]; return 0; } } else { ans = 1; a.push_back(a[i]); i = j; j++; } } } }
CPP
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
#include <bits/stdc++.h> using namespace std; int main() { long long n, k; cin >> n >> k; deque<long long> d; long long x; if (k >= n) { long long mx = -1; for (int i = 1; i <= n; i++) { cin >> x; mx = max(mx, x); } cout << mx; } else { for (int i = 1; i <= n; i++) { cin >> x; d.push_back(x); } int num = 0; while (true) { if (num == k) { cout << d.front(); return 0; } x = d.front(); d.pop_front(); if (d.front() < x) { int y = d.front(); d.pop_front(); d.push_back(y); d.push_front(x); num++; } else { num = 1; d.push_back(x); } } } return 0; }
CPP
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
n,k=map(int,input().split()) l=list(map(int,input().split())) t,i,j=0,0,1 test=1 if n<k: print(max(l)) test=0 while t<k and test: if l[i]>l[j]: t+=1 j+=1 else: i=j j+=1 t=1 if j==n: j=0 if test: print(l[i])
PYTHON3
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
import java.util.*; import java.io.*; import java.math.*; public class Solution{ public static void main(String[] args) { MyScanner sc=new MyScanner(); int n=sc.nextInt(); long k=sc.nextLong(); int arr[]=new int[n]; for(int i=0;i<n;i++) arr[i]=sc.nextInt(); int mx=0; for(Integer x:arr) mx=Math.max(mx,x); if(k>=n-1){ System.out.println(mx); System.exit(0); } else{ ArrayDeque<Integer> q=new ArrayDeque<>(); for(int i=0;i<n;i++){ q.addLast(arr[i]); } int curr=0; boolean b=true; int v1=q.removeFirst(); while(b){ int v2=q.removeFirst(); if(v1>v2){curr++;q.addLast(v2);} else {curr=1;q.addLast(v1);v1=v2;} if(curr>=k){System.out.println(v1);System.exit(0);} } } } private static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine(){ String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
JAVA
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
n,k = map(int,input().split()) arr = list(map(int,input().split())) winner = 0 h = 0 p = 0 for i in range(1,n): if arr[winner]>arr[i]: h+=1 if h>=k: print(arr[winner]) p=1 break else: winner = i h = 1 if p==0: print(arr[winner])
PYTHON3
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
import java.util.*; import java.lang.Math; import java.util.Arrays; import java.util.Scanner; import java.util.stream.IntStream; import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; Scanner in = new Scanner(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskA solver = new TaskA(); solver.solve(1, in, out); out.close(); } static class TaskA { public void solve(int testNumber, Scanner in, PrintWriter out) { long n = in.nextLong(); long k = in.nextLong(); Queue <Integer> Q = new ArrayDeque (); for (int i = 0; i < n; i++) Q.add(in.nextInt()); if (k >= n) { System.out.println(n); return; } int p1 = Q.poll(); int p2, len = 0; while (true) { p2 = Q.poll(); if (p1 > p2) { Q.add(p2); len++; } else { Q.add(p1); p1 = p2; len = 1; } if (len == k) break; } System.out.println(p1); // out.println(count); } } public int factorial(int n) { int fact = 1; int i = 1; while(i <= n) { fact *= i; i++; } return fact; } public static int BinarySearch(long temp,long[] sum,int r) { int l=0; while(l<=r) { int mid=l+(r-l)/2; if(sum[mid]==temp&&sum[mid]!=-1) { return mid; } if(sum[mid]>temp&&sum[mid]!=-1) r=mid-1; if(sum[mid]<temp&&sum[mid]!=-1) l=mid+1; } return -1; } 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 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; } public static long pow(long n,long p,long m) { long result = 1; if(p==0) return 1; if (p==1) return n; while(p!=0) { if(p%2==1) result *= n; if(result>=m) result%=m; p >>=1; n*=n; if(n>=m) n%=m; } return result; } public static long pow(long n,long p) { long result = 1; if(p==0) return 1; if (p==1) return n; while(p!=0) { if(p%2==1) result *= n; p >>=1; n*=n; } return result; } static long sort(int a[]){ int n=a.length; int b[]=new int[n]; return mergeSort(a,b,0,n-1); } static long mergeSort(int a[],int b[],long left,long right){ long c=0; if(left<right){ long mid=left+(right-left)/2; c= mergeSort(a,b,left,mid); c+=mergeSort(a,b,mid+1,right); c+=merge(a,b,left,mid+1,right); } return c; } static long merge(int a[],int b[],long left,long mid,long right){ long c=0;int i=(int)left;int j=(int)mid; int k=(int)left; while(i<=(int)mid-1&&j<=(int)right){ if(a[i]<=a[j]){ b[k++]=a[i++]; } else{ b[k++]=a[j++];c+=mid-i; } } while (i <= (int)mid - 1) b[k++] = a[i++]; while (j <= (int)right) b[k++] = a[j++]; for (i=(int)left; i <= (int)right; i++) a[i] = b[i]; return c; } }
JAVA
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
#include <bits/stdc++.h> using namespace std; long long pwr(long long base, long long exp, long long mod = (1000000007LL)) { long long res = 1; while (exp > 0) { if (exp % 2) { res = (res * base) % mod; } base = (base * base) % mod; exp /= 2; } return res; } long long gcd(long long a, long long b) { if (b == 0) return a; else gcd(b, a % b); } bool isPrime(long long n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; for (int i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } int sod(long long n) { int sum = 0; while (n) { sum += n % 10; n /= 10; } return sum; } void printvector(vector<long long> v) { for (int i = 0; i < v.size(); i++) { cout << v[i] << " "; } cout << endl; } int visited[10] = {0}; int branch = 0; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long n, k; cin >> n >> k; if (k >= n) k = n + 1; deque<long long> q; for (int i = 0; i < n; i++) { long long x; cin >> x; q.push_back(x); } long long wins = 0; while (wins < k) { long long val1 = q.front(); q.pop_front(); long long val2 = q.front(); q.pop_front(); if (val1 > val2) { q.push_back(val2); q.push_front(val1); wins++; } else { q.push_back(val1); q.push_front(val2); wins = 1; } } cout << q.front() << endl; }
CPP
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
#include <bits/stdc++.h> using namespace std; long long fx[] = {1, -1, 0, 0}; long long fy[] = {0, 0, 1, -1}; vector<int> p; int main() { long long n, kk; cin >> n >> kk; for (long long i = 0; i < n; i++) { long long val; cin >> val; p.push_back(val); } bool flag = 0; for (long long i = 0; i < p.size(); i++) { long long j = i + 1; long long k = i; long long cnt = 0; if (k > 0) { if (p[k - 1] < p[k]) { cnt++; p.push_back(p[k - 1]); } } if (p[j] < p[k]) { while (p[j] < p[k]) { p.push_back(p[j]); j++; cnt++; if (cnt >= n - 1) { flag = 1; cout << p[k] << endl; return 0; } else if (cnt >= kk) { flag = 1; cout << p[k] << endl; return 0; } } } while (k--) p.erase(p.begin()); i = 0; } if (!flag) { cout << n << endl; } }
CPP
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
def Winner(wins, A): winStack = [] loseStack = [] count = 0 if wins > 1000: wins = 1000 if A[0] > A[1]: winStack.append(A[0]) loseStack.append(A[1]) count += 1 else: winStack.append(A[1]) loseStack.append(A[0]) count += 1 index = 2 while index < len(A): if winStack[-1] > A[index]: count += 1 if count == wins: return winStack[-1] else: loseStack.append(winStack.pop()) winStack.append(A[index]) count = 1 index += 1 while count != wins: for x in loseStack: if winStack[-1] > x: count += 1 return winStack[-1] else: lost = winStack.pop() loseStack.append(lost) winStack.append(x) count = 1 numPlayers, wins = input().split() numPlayers = int(numPlayers) wins = int(wins) powerPlayers = list(map(int, input().split())) print(Winner(wins, powerPlayers))
PYTHON3
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
n,k = map(int, raw_input().split()) l = list(map(int, raw_input().split())) m = max(l) if k >= n: print(m) else: # Just do it! won = {} for a in l: won[a] = 0 while True: if l[0] > l[1]: won[l[0]] += 1 if won[l[0]] >= k: print(l[0]) break l.append(l.pop(1)) else: won[l[1]] += 1 if won[l[1]] >= k: print(l[1]) break l.append(l.pop(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; const int maxN = 600; int main() { std::ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n, a[maxN]; long long k, temp; cin >> n >> k; for (int i = 0; i < n; i++) { cin >> a[i]; } int res = a[0]; temp = 0; for (int i = 1; i < n; ++i) { if (temp >= k) { cout << res << endl; return 0; } if (res > a[i]) { temp++; } else { temp = 1; res = a[i]; } } 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
#include <bits/stdc++.h> using namespace std; long long mult(long long a, long long b, long long p = (long long)(1000000007)) { return ((a % p) * (b % p)) % p; } long long add(long long a, long long b, long long p = (long long)(1000000007)) { return (a % p + b % p) % p; } void solve() { long long n, k, mx = -1 * LLONG_MAX; cin >> n >> k; queue<long long> q; for (long long i = 0; i < n; ++i) { long long x; cin >> x; mx = max(mx, x); q.emplace(x); } if (k >= n - 1) { cout << mx << "\n"; return; } long long res = q.front(); q.pop(); long long val = k; while (k != 0) { long long temp = q.front(); q.pop(); if (res > temp) { k--; q.emplace(temp); } else if (res < temp) { k = val - 1; q.emplace(res); res = temp; } } cout << res << "\n"; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); solve(); }
CPP
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
from queue import Queue n, k = map(int, input().split()) a = list(map(int, input().split())) q = Queue() for aa in a: q.put(aa) cur = q.get() sec = q.get() if cur > sec: q.put(sec) else: q.put(cur) cur = sec while 1: wins = 1 sec = q.get() while sec < cur: wins += 1 # print(wins, sec, cur) q.put(sec) if wins > n or wins == k: print(cur) exit(0) sec = q.get() q.put(cur) cur = sec
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())) b=[0]*n if k>=(n-1): print(max(a)) exit(0) while k not in b: if a[0]>a[1]: a.append(a[1]) a.remove(a[1]) b[a[0]-1]+=1 else: a[0],a[1]=a[1],a[0] a.append(a[1]) a.remove(a[1]) b[a[0]-1]+=1 print(b.index(k)+1)
PYTHON3
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
n,k = map(int, input().strip().split(' ')) lst = list(map(int, input().strip().split(' '))) if k>n: print(max(lst)) else: i=0 c=1 if lst[i]>lst[i+1]: f=lst[i] lst.append(lst[i+1]) del lst[i+1] else: f=lst[i+1] lst.append(lst[i]) del lst[i] while(True): if max(lst[i],lst[i+1])==f: c+=1 if lst[i]>lst[i+1]: lst.append(lst[i+1]) del lst[i+1] else: lst.append(lst[i]) del lst[i] else: c=1 if lst[i]>lst[i+1]: f=lst[i] lst.append(lst[i+1]) del lst[i+1] else: f=lst[i+1] lst.append(lst[i]) del lst[i] if c==k: print(f) break
PYTHON3
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
import java.io.*; import java.util.*; import java.lang.*; import java.math.*; public class Main { static class TaskG { private void solve(int test, FastScanner in, PrintWriter out) { int n = in.nextInt(); long k =in.nextLong(); int a[] = new int[505]; for (int i=0;i<n;i++){ a[i] = in.nextInt(); } int x = 0; int max = 0; int dp[] = new int[505]; for (int i=1;i<n;i++){ if (a[x]<a[i]){ dp[i] +=1; x = i; } else{ dp[x]++; } if (dp[x]>=k){ out.print(a[x]); return; } } out.print(a[x]); } } public static void main(String[] args) throws IOException { FastScanner in = new FastScanner(System.in); PrintWriter out = new PrintWriter(System.out); // FastScanner in = new FastScanner("file"); // PrintWriter out = new PrintWriter(new FileWriter("file")); new TaskG().solve(1, in, out); out.close(); } static class FastScanner { BufferedReader br; StringTokenizer token; public FastScanner(InputStream is) { br = new BufferedReader(new InputStreamReader(System.in)); } public FastScanner(String s) { try { br = new BufferedReader(new FileReader(s)); } catch (FileNotFoundException e) { e.printStackTrace(); } } public String nextToken() { while (token == null || !token.hasMoreTokens()) { try { token = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return token.nextToken(); } public int nextInt() { return Integer.parseInt(nextToken()); } public long nextLong() { return Long.parseLong(nextToken()); } public double nextDouble() { return Double.parseDouble(nextToken()); } } }
JAVA
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
#include <bits/stdc++.h> using namespace std; const long long INF = LLONG_MAX, MOD = 1e9 + 7; const int L = 17, N = 1 << L; long long n, k; queue<pair<long long, long long> > a; int main() { ios::sync_with_stdio(false); cin >> n >> k; for (int i = 0; i < n; i++) { long long x; cin >> x; a.push({i + 1, x}); } k = min(n, k); pair<long long, long long> champ = a.front(); a.pop(); long long win = 0; for (int i = 0; i < n * 4; i++) { pair<long long, long long> chal = a.front(); a.pop(); if (champ.second > chal.second) { a.push(chal); win++; if (win >= k) break; } else { a.push(champ); champ = chal; win = 1; } } cout << champ.second << 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
#codeforces_879B_virtual gi = lambda: list(map(int,input().split())) n,k = gi() powers = gi() if k >= n: print(max(powers)) exit(); wins = 0 while 1: if powers[0] > powers[1]: wins += 1 loser = powers.pop(1) powers.append(loser) else: wins = 1 loser = powers.pop(0) powers.append(loser) if wins == k: print(powers[0]) exit();
PYTHON3
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
n, k = map(int, raw_input().strip().split()) if k >= n: print max(map(int, raw_input().strip().split())) else: wins = 0 playerArray = map(int, raw_input().strip().split()) currPower = playerArray[0] playerArray = playerArray[1:] while wins < k: if currPower < playerArray[0]: playerArray.append(currPower) currPower = playerArray[0] playerArray = playerArray[1:] wins = 1 else: wins += 1 loser = playerArray[0] playerArray = playerArray[1:] playerArray.append(loser) print currPower
PYTHON
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
import java.util.Deque; import java.util.LinkedList; import java.util.Scanner; public class Main { private static int[] input = new int[100010]; public static void main(String[] args) { Scanner scan = new Scanner(System.in); int i , j , n , max = - 1; long k; Deque<Integer> queue = new LinkedList<>(); n = scan.nextInt(); k = scan.nextLong(); for (i = 0;i < n;i ++) { input[i] = scan.nextInt(); if (input[i] > max) { max = input[i]; } } for (i = 0;i < n;i ++) { queue.addLast(input[i]); } int cnt = 0 , winner = 0 , ans = - 1; while (!queue.isEmpty()) { int player1 = queue.pollFirst(); int player2 = queue.pollFirst(); if (player1 == max) { ans = player1; break; } else { if (player1 > player2) { if (winner == 0) { winner = player1; cnt = 1; } else if (winner == player1) { cnt ++; } else { winner = player1; cnt = 1; } } else { if (winner == 0) { winner = player2; cnt = 1; } else if (winner == player2) { cnt ++; } else { winner = player2; cnt = 1; } } if (cnt >= k) { ans = winner; break; } queue.addFirst(Math.max(player1 , player2)); queue.addLast(Math.min(player1 , player2)); } } 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
# n=int(input()) # n,k=map(int,input().split()) # arr=list(map(int,input().split())) #ls=list(map(int,input().split())) #for i in range(m): # for _ in range(int(input())): #from collections import Counter #from fractions import Fraction #s=iter(input()) from collections import deque n,k=map(int,input().split()) arr=list(map(int,input().split())) '''if n==2: print(max(arr)) exit()''' for i in range(n): var=i+k if i==0: if var>=n: if arr[i]>=max(arr[i:n]) and arr[i]>=max(arr[:(k-(n-i-1))+1]): print(arr[i]) break else: if arr[i] >= max(arr[i:i+k+1]): print(arr[i]) break else: #print(i) if var>=n: if arr[i]>=max(arr[i-1:n]) and arr[i]>=max(arr[:(k-(n-i-1))]): print(arr[i]) break else: if arr[i]>=max(arr[i-1:i+k]): print(arr[i]) break
PYTHON3
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
n, k = map(int, raw_input().split()) a = map(int, raw_input().split()) if k >= n - 1: print n else: curr, wins = a[0], -1 for x in a: if x > curr: curr, wins = x, 0 wins += 1 if wins == k: break print curr
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() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long int n, k, x; cin >> n >> k; vector<long long int> v; for (int i = 0; i < n; i++) { cin >> x; v.push_back(x); } bool f = true; long long int max = v[0], count = 0; for (int i = 1; (i < n) & f; i++) { if (v[i] < max) { count++; if (count == k) f = false; } else { max = v[i]; count = 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.lang.reflect.Array; import java.util.*; public class Main { static void solve(){ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); long k = sc.nextLong(); int maxIndex=0; int max = 0; int[]arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = sc.nextInt(); if(arr[i]>max){ max = arr[i]; maxIndex=i; } } int i=0, j=1; int wins = 0; while(i!=maxIndex && j!=maxIndex){ if(arr[i]<arr[j]){ wins=1; i=j; j=i+1; } else { wins++; j++; } if(wins>=k){ System.out.print(arr[i]); return; } } System.out.print(arr[maxIndex]); } public static void main(String[] args) { solve(); } }
JAVA
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
#include <bits/stdc++.h> using namespace std; long long int ciel(long long int x, long long int y) { if (x % y) { return x / y + 1; } return x / y; } long long int perm(long long int n) { return ((n - 1) * n) / 2; } void swap(long long int x, long long int y) { long long int t = x; x = y; y = t; } long long int sq(long long int x) { return x * x; } void what_should_be_the_logic() { long long int n, j, i; cin >> n; long long int a[n]; long long int k, cnt = 0, mx = 0; cin >> k; for (i = 0; i <= n - 1; i++) { cin >> a[i]; mx = max(mx, a[i]); } if (mx == a[0] || n <= k) { cout << mx << "\n"; return; } mx = max(a[0], a[1]); for (i = 1; i <= n - 1; i++) { if (mx < a[i]) { cnt = 1; mx = a[i]; } else { cnt++; } if (cnt == k) { cout << mx << "\n"; return; } } cout << mx << "\n"; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); what_should_be_the_logic(); return 0; }
CPP
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
#include <bits/stdc++.h> using namespace std; int main() { long long n, k; cin >> n >> k; int a[n + 1]; for (int i = 0; i < n; i++) { cin >> a[i]; } if (k >= n) { cout << n << "\n"; } else { int wn; if (a[0] > a[1]) wn = a[0]; else wn = a[1]; int c = 1; for (int i = 2; i < n; i++) { if (wn > a[i]) c++; else { wn = a[i]; c = 1; } if (c == k) { cout << wn << "\n"; return 0; } } cout << 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
#include <bits/stdc++.h> using namespace std; long long m; void solve() { long long n, k, a, b; cin >> n >> k >> a; while (n--) { cin >> b; if (m == k) break; if (a > b) m++; else { a = b; m = 1; } } cout << a; } int main() { ios::sync_with_stdio(0); cin.tie(0); solve(); }
CPP
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
n,k=map(int,input().split()) ar=list(map(int,input().split())) mx=max(ar) winns={x:0 for x in ar} while True: if ar[0] > ar[1]: if(ar[0]==mx or winns[ar[0]] >= k-1): print(ar[0]) break ar.append(ar.pop(1)) else: ar.append(ar.pop(0)) winns[ar[0]]+=1 #print(ar)
PYTHON3
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayDeque; import java.util.Queue; import java.util.StringTokenizer; /** * @author Don Li */ public class TableTennis { void solve() { int n = in.nextInt(); long k = in.nextLong(); int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = in.nextInt(); if (k >= n - 1) { out.println(n); return; } Queue<Integer> qu = new ArrayDeque<>(n << 1); for (int i = 0; i < n; i++) qu.offer(a[i]); int fi = qu.poll(), se = qu.poll(); int max = Math.max(fi, se), wins = 1; qu.offer(Math.min(fi, se)); while (wins < k) { int cur = qu.poll(); if (max > cur) { wins++; qu.offer(cur); } else { wins = 1; qu.offer(max); max = cur; } } out.println(max); } public static void main(String[] args) { in = new FastScanner(new BufferedReader(new InputStreamReader(System.in))); out = new PrintWriter(System.out); new TableTennis().solve(); out.close(); } static FastScanner in; static PrintWriter out; static class FastScanner { BufferedReader in; StringTokenizer st; public FastScanner(BufferedReader in) { this.in = in; } public String nextToken() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(in.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } public int nextInt() { return Integer.parseInt(nextToken()); } public long nextLong() { return Long.parseLong(nextToken()); } public double nextDouble() { return Double.parseDouble(nextToken()); } } }
JAVA
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
#include <bits/stdc++.h> using namespace std; int main() { long long int n, k; cin >> n >> k; vector<int> v; int a[n], l = 0; for (int i = 0; i < n; i++) { cin >> a[i]; v.push_back(a[i]); } int max = a[0], u = 0; for (int i = 0; i < n; i++) { if (a[i] > max) { max = a[i]; } } if (n <= k) { cout << max; } else { for (int i = 0;; i++) { if (v[1] < v[0]) { v.push_back(v[1]); v.erase(v.begin() + 1); l++; if (l == k) { cout << v[0]; break; } else if (l < k) { continue; } } else if (v[1] > v[0]) { v.push_back(v[0]); v.erase(v.begin()); l = 1; } } } return 0; }
CPP
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
from queue import Queue n, k = map(int, input().split()) a = [int(x) for x in input().split()] q = Queue() if k > n: print(max(a)) else: best, wins = a[0], 0 for i in range(1, n): q.put(a[i]) while True: top = q.get() if best > top: q.put(top) wins += 1 else: q.put(best) best, wins = top, 1 if wins >= 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
#include <bits/stdc++.h> using namespace std; int main() { long long int n, m; cin >> n >> m; vector<long long int> ve(n); map<long long int, long long int> mp; long long int mx = 0; for (int i = 0; i < n; i++) { cin >> ve[i]; mx = max(mx, ve[i]); } long long int a = n, dem = 0, dem1 = 0; while (n-- > 0) { for (int i = 0; i < a; i++) { if (ve[i] > ve[i + 1]) { mp[ve[i]]++; if (mp[ve[i]] == m) { cout << ve[i] << "\n"; return 0; } ve.insert(ve.end(), ve[i + 1]); ve.erase(ve.begin() + i + 1); } else { mp[ve[i + 1]]++; if (mp[ve[i + 1]] == m) { cout << ve[i + 1] << "\n"; return 0; } ve.insert(ve.end(), ve[i]); ve.erase(ve.begin() + i); } break; } } cout << mx << "\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.io.* ; import java.util.* ; import java.math.* ; public class CodeForces{ public static void main(String[] args) throws IOException{ Solver Machine = new Solver() ; Machine.Solve() ; Machine.Finish(); } } class Mod{ static long mod=1000000007 ; static long d(long a,long b){ return (a*MI(b))%mod ; } static long m(long a , long b){ return (a*b)%mod ; } static private long MI(long a ){ return pow(a,mod-2) ; } static long pow(long a,long b){ if(b<0) return pow(MI(a),-b) ; long val=a ; long ans=1 ; while(b!=0){ if((b&1)==1) ans = (ans*val)%mod ; val = (val*val)%mod ; b>>=1 ; } return ans ; } } class pair implements Comparable<pair>{ long x ; long y ; pair(long x,long y){ this.x=x ; this.y=y ;} public int compareTo(pair p){ return (this.x<p.x ? -1 : (this.x>p.x ? 1 : (this.y<p.y ? -1 : (this.y>p.y ? 1 : 0)))) ; } } class Solver{ FasterScanner ip = new FasterScanner(System.in) ; PrintWriter op = new PrintWriter(System.out) ; void Solve() throws IOException{ int n = ip.i() ; long k = ip.l() ; int a[] = new int[n+1] ; for(int i=1 ; i<=n ; i++) a[i] = ip.i() ; mylist ls = new mylist() ; for(int i=1 ; i<=n ; i++) ls.add(i) ; int wins[] = new int[n+1] ; while(true){ if(a[ls.get(0)]==n){ pln(n) ; return ; } int fs = ls.get(0) , sn = ls.get(1) ; if(a[fs]>a[sn]){ ls.remove(1) ; ls.add(sn) ; wins[fs]++ ; if(wins[fs]==k){ pln(a[fs]) ; return ; } }else{ ls.remove(0) ; ls.add(fs) ; wins[sn]++ ; if(wins[sn]==k){ pln(a[sn]) ; return ; } } } } void p(Object o){ op.print(o) ; } void pln(Object o){ op.println(o) ; } void pArr(String name,Object arr,char type){ p(name+": [") ; if(type=='i'){ int arr1[] = (int[]) arr ; for(int i=0 ; i<arr1.length ; i++) p(arr1[i]+(i+1==arr1.length ? "" : ",")) ; }else if(type=='l'){ long arr1[] = (long[]) arr ; for(int i=0 ; i<arr1.length ; i++) p(arr1[i]+(i+1==arr1.length ? "" : ",")) ; }else if(type=='b'){ boolean arr1[] = (boolean[]) arr ; for(int i=0 ; i<arr1.length ; i++) p((arr1[i] ? 1 : 0)+(i+1==arr1.length ? "" : ",")) ; } pln("]") ; } void Finish(){ op.flush(); op.close(); } } @SuppressWarnings("serial") class mylist extends ArrayList<Integer>{ } @SuppressWarnings("serial") class myset extends TreeSet<Long>{ } @SuppressWarnings("serial") class mystack extends Stack<Integer>{ } @SuppressWarnings("serial") class mymap extends TreeMap<Long,Integer>{ } class FasterScanner { private InputStream mIs; private byte[] buf = new byte[1024]; private int curChar,numChars; public FasterScanner() { this(System.in); } public FasterScanner(InputStream is) { mIs = is;} public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = mIs.read(buf);} catch (IOException e) { throw new InputMismatchException();} if (numChars <= 0) return -1; } return buf[curChar++]; } public String nextLine(){ int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isEndOfLine(c)); return res.toString(); } public String S(){ int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); }while (!isSpaceChar(c)); return res.toString(); } public long l(){ int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1 ; c = read() ; } long res = 0; do{ if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10 ; res += c - '0' ; c = read(); }while(!isSpaceChar(c)); return res * sgn; } public int i(){ int c = read() ; while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1 ; c = read() ; } int res = 0; do{ if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10 ; res += c - '0' ; c = read() ; }while(!isSpaceChar(c)); return res * sgn; } public int[] ia(int n){ int a[] = new int[n] ; for(int i=0 ; i<n ; i++) a[i] = i() ; return a ; } public long[] la(int n){ long a[] = new long[n] ; for(int i=0 ; i<n ; i++) a[i] = l() ; return a ; } public boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } }
JAVA
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
firstGiven = input().split() testCase = int(firstGiven[0]) players = input().split() activePlayerOne = int(players[0]) activePlayerTwo = int(players[1]) playerOneWins = -1 playerTwoWins = -1 winCondition = 0 MVP = 0 for x in players: player = int(x) if MVP < player: MVP = player if int(firstGiven[1]) >= testCase: winCondition = 0 activePlayerOne = MVP else: winCondition = int(firstGiven[1]) if winCondition >= testCase and len(players) > 2: activePlayerOne == MVP playerOneWins == winCondition playerOneWins = 0 playerTwoWins = 0 if len(players) == 2: if activePlayerOne > activePlayerTwo: playerOneWins = winCondition else: playerTwoWins = winCondition while playerOneWins != winCondition and playerTwoWins != winCondition: if activePlayerOne > activePlayerTwo: players.append(str(activePlayerTwo)) players.remove(str(activePlayerTwo)) activePlayerTwo = int(players[1]) playerTwoWins = 0 playerOneWins += 1 else: players.append(str(activePlayerOne)) players.remove(str(activePlayerOne)) activePlayerOne = int(players[1]) playerOneWins = 0 playerTwoWins += 1 if playerOneWins == winCondition: print(activePlayerOne) elif playerTwoWins == winCondition: print(activePlayerTwo)
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
# arock # 10/26/2017 # # Codeforces Round #443 - Div. 2 - Problem B n, k = map(int, raw_input().split()) a = map(int, raw_input().split()) if k >= n: print max(a) else: lo = 0 num = 0 win = -1 while True: if a[lo] > a[lo + 1]: if a[lo] == win: num += 1 if num == k: print win exit() else: num = 1 win = a[lo] a.append(a[lo + 1]) a[lo + 1] = a[lo] lo += 1 else: if a[lo + 1] == win: num += 1 if num == k: print win exit() else: num = 1 win = a[lo + 1] a.append(a[lo]) lo += 1
PYTHON
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
(n, k) = map(int, input().split()) if k > 5000: k = 5000 lst = [] for x in input().split(): lst.append(int(x)) s = [] d = {} win = max(lst[0], lst[1]) out = min(lst[0], lst[1]) lst.remove(out) lst.append(out) s.append(win) d[win] = 1 acc = 0 while acc != k: win = max(s[-1], lst[1]) out = min(s[-1], lst[1]) lst.remove(out) lst.append(out) s.append(win) if not win in d: d[win] = 0 d[win] += 1 acc = max(d.values()) print(max(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
#include <bits/stdc++.h> using namespace std; int main() { int n, a[510]; long long k; scanf("%d %I64d", &n, &k); for (int i = 0; i < n; i++) scanf("%d", &a[i]); if (k + 1 >= n) { sort(a, a + n); printf("%d", a[n - 1]); } else { int MAX = a[0]; int ans = 0, i = 1; while (ans != k) { if (a[i] > MAX) { ans = 1; MAX = a[i]; } else ans++; i++; i %= n; } printf("%d", 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.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.StringTokenizer; import java.io.IOException; import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.ArrayList; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskB solver = new TaskB(); solver.solve(1, in, out); out.close(); } static class TaskB { public void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.nextInt(); long K = in.nextLong(); if (K >= n) { out.println(n); return; } int k = (int) K; ArrayList<Integer> v = new ArrayList<>(); for (int i = 0; i < n; ++i) { v.add(in.nextInt()); } boolean found = false; boolean first = true; int winner = 0; while (!found) { int firstPlayer = v.get(0); int row = first ? 0 : 1; first = false; while (row < k) { int secondPlayer = v.get(1); if (secondPlayer < firstPlayer) { ++row; v.remove(1); v.add(secondPlayer); } else { v.remove(0); v.add(firstPlayer); break; } } if (row >= k) { found = true; winner = firstPlayer; } } out.println(winner); } } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 65536 / 2); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } } }
JAVA
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
from collections import deque q=deque() n,k=map(int,input().split(" ")) a=list(map(int,input().split(" "))) if k>=n-1: print(n) else: for i in range(n): q.append(i) x=q.popleft() np=0 while (np<k): y=q.popleft() if a[x]>a[y]: np+=1 q.append(y) else: np=1 q.append(x) x=y print(a[x])
PYTHON3
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
import java.io.*; import java.util.ArrayList; import java.util.List; public class P879B { private static class HpReader { private BufferedReader in; private String containingDirPath; public HpReader(String dir, String inFile, String outFile) { initAndRedirectInOut(dir, inFile, outFile); } private static int ivl(String val) { return Integer.parseInt(val); } private void initAndRedirectInOut(String dir, String inFile, String outFile) { if (dir != null) { try { containingDirPath = dir.endsWith(File.separator) ? dir : dir + File.separator; if (isDebug && inFile != null) System.setIn(new FileInputStream(new File(containingDirPath + inFile))); if (isDebug && outFile != null) System.setOut(new PrintStream(new File(containingDirPath + outFile))); } catch (FileNotFoundException e) { // Do nothing, stdin & stdout are not redirected } } in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); } private String nextSingleStr() throws IOException { return in.readLine(); } private String[] nextLineStr() throws IOException { return nextLineStr(0); } private String[] nextLineStr(int offset) throws IOException { String[] inp = nextSingleStr().split(" "); String[] rs = new String[offset + inp.length]; System.arraycopy(inp, 0, rs, offset, inp.length); return rs; } private int nextSingleInt() throws IOException { return ivl(in.readLine()); } private int[] nextLineInt() throws IOException { return nextLineInt(0); } private int[] nextLineInt(int offset) throws IOException { String[] inp = nextLineStr(); int[] rs = new int[offset + inp.length]; for (int i = 0; i < inp.length; i++) rs[offset + i] = ivl(inp[i]); return rs; } private int[][] nextMatInt(int lineCount) throws IOException { return nextMatInt(lineCount, 0, 0); } private int[][] nextMatInt(int lineCount, int rowOffset, int colOffset) throws IOException { int[][] rs = new int[rowOffset + lineCount][]; for (int i = rowOffset; i < rs.length; i++) rs[i] = nextLineInt(colOffset); return rs; } } private static class HpHelper { private static final String LOCAL_DEBUG_FLAG = "COM_PROG_DEBUG"; private static boolean isDebug() { try { return Boolean.parseBoolean(System.getenv(HpHelper.LOCAL_DEBUG_FLAG)); } catch (Exception e) { return false; } } private static String createDelimiter(String delimiter) { return delimiter == null ? " " : delimiter; } private static void println(int[] data, String delimiter) { delimiter = createDelimiter(delimiter); for (int t : data) out.print(t + delimiter); out.println(); } private static void println(long[] data, String delimiter) { delimiter = createDelimiter(delimiter); for (long t : data) out.print(t + delimiter); out.println(); } private static <T> void println(T[] data, String delimiter) { delimiter = createDelimiter(delimiter); for (T t : data) { if (t instanceof int[]) { println((int[]) t, delimiter); } else if (t instanceof long[]) { println((long[]) t, delimiter); } else if (t instanceof Object[]) { println((Object[]) t, delimiter); } else { out.print(t + delimiter); } } out.println(); } } private static boolean isDebug = HpHelper.isDebug(); private static HpReader in = new HpReader("/Users/apple/Work/WorkSpaces/CompetitiveProg/", "in.txt", null); private static PrintWriter out; public static void main(String[] args) throws IOException { String[] inp = in.in.readLine().split(" "); int n = Integer.parseInt(inp[0]); long k = Long.parseLong(inp[1]); int[] power = in.nextLineInt(1), win = new int[n + 1]; int rs = 0; if (k >= n - 1) { for (int i =1; i <= n; i++) rs = Math.max(rs, power[i]); } else { List<Integer> pp = new ArrayList<>(); for (int i = 1; i <= n; i++) pp.add(power[i]); while (true) { int toRemove = (pp.get(0) < pp.get(1) ? 0 : 1); int wonId = pp.get(1 - toRemove); win[wonId] += 1; pp.add(pp.remove(toRemove)); if (win[wonId] == k) { rs = wonId; break; } } } out.println(rs); out.flush(); } }
JAVA
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
#include <bits/stdc++.h> using namespace std; int n, a[600]; long long k; int main() { cin >> n >> k; for (int i = (1); i <= (n); i++) scanf("%d", &a[i]); if (k >= n) { cout << n; return 0; } queue<int> q; for (int i = (2); i <= (n); i++) q.push(a[i]); int cnt = 0, cur = a[1]; while (cnt < k) { int u = q.front(); q.pop(); if (cur > u) { q.push(u); cnt++; } else { q.push(cur); cur = u; cnt = 1; } } cout << cur; return 0; }
CPP
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
from collections import deque from functools import reduce n, k = [int(a) for a in input().strip().split()] powers = deque() for a in input().strip().split(): powers.append(int(a)) num_wins = {x: 0 for x in powers} if n == 1: print(powers[0]) elif k > n: print(max(powers)) else: current = None curr_max_wins = -float("inf") while (curr_max_wins < k): if current is None: current = powers.popleft() else: challenger = powers.popleft() if challenger > current: powers.append(current) num_wins[challenger] += 1 current = challenger else: powers.append(challenger) num_wins[current] += 1 for i, j in num_wins.items(): if j > curr_max_wins: curr_max_wins = j 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
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Scanner; import java.util.Deque; import java.util.LinkedList; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; Scanner in = new Scanner(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskB solver = new TaskB(); solver.solve(1, in, out); out.close(); } static class TaskB { public void solve(int testNumber, Scanner in, PrintWriter out) { int n = in.nextInt(); long k = in.nextLong(); int[] p = new int[n]; for (int i = 0; i < n; i++) { p[i] = in.nextInt(); } out.println(winner(p, k)); } public int winner(int[] powers, long k) { Deque<Integer> d = new LinkedList<>(); for (int p : powers) { d.addLast(p); } int numTrials = 0; int n = powers.length; int currentWinner = -1; int times = 0; while (numTrials < n) { int a = d.removeFirst(); int b = d.removeFirst(); int m = Math.min(a, b); int M = Math.max(a, b); if (M == currentWinner) { times++; } else { currentWinner = M; times = 1; } if (times == k) { return currentWinner; } d.addFirst(M); d.addLast(m); numTrials++; } return currentWinner; } } }
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; cin >> n; long long k; cin >> k; if (n <= k) { int ans = -1; while (n--) { int a; cin >> a; ans = max(ans, a); } cout << ans << 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
#developer at Imperial Arkon n,k = [int(x) for x in input().split()] power = [int(x) for x in input().split()] if k>=n: print(max(power)) else: i=0 while i<n: flag,flag2 = 0,0 # print('i %d'%i) if i==0: withPlayers = i+k+1 else: withPlayers = i+k for j in range(i+1, withPlayers): if j==n: # print(j, 'breaking loop') print(power[tempWinner]) flag2=1 break if power[j]>power[i]: # print(j,'power[%d] is greater than power[%d]'%(j,i)) i=j tempWinner = i flag=1 break # print(j, withPlayers-1) if j==withPlayers-1 and flag==0: if not flag2: print(power[i]) flag2=1 break if flag==0: i+=1 if flag2: break if flag2==0: print(power[tempWinner])
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 int fexpo(long long int a, long long int b) { if (b == 0) return 1LL; if (b == 1) return a; if (b == 2) return ((a) * (a)); if (b % 2 == 0) return fexpo(fexpo(a, b / 2), 2); else return ((a) * (fexpo(fexpo(a, (b - 1) / 2), 2))); } int main() { cin.sync_with_stdio(0); cin.tie(0); deque<pair<long long int, long long int> > q; long long int n, k; cin >> n >> k; int mxe = 0; for (int i = 0; i < n; i++) { int x; cin >> x; if (x > mxe) mxe = x; q.push_back(make_pair(x, 0)); } long long int check = 0; while (true) { check++; long long int u1, v1, u2, v2; u1 = q.front().first; v1 = q.front().second; q.pop_front(); u2 = q.front().first; v2 = q.front().second; q.pop_front(); if (v1 >= k) { return cout << u1 << endl, 0; } if (v2 >= k) return cout << u2 << endl, 0; if (check > 6 * 1e7) { return cout << mxe << endl, 0; } if (u1 > u2) { q.push_back(make_pair(u2, 0)); q.push_front(make_pair(u1, v1 + 1)); } else { q.push_back(make_pair(u1, 0)); q.push_front(make_pair(u2, v2 + 1)); } } }
CPP
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
#http://codeforces.com/contest/879/problem/B n,k=map(int,input().split()) po=[int(x) for x in input().split()] if n <= k: print(max(po)) exit(0) w=-1 l=-1 i=0 j=i+1 total=0 chn=True while total < k: if po[i] > po[j]: w=i l=j total+=1 po.append(po.pop(l)) chn=True else: if chn: total=0 w=j l=i po.append(po.pop(l)) total+=1 print(po[i])
PYTHON3
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
#include <bits/stdc++.h> using namespace std; long long n, a[10000], k, kk[1000], mx = -10000000, nom; int main() { cin >> n >> k; for (int i = 0; i < n; i++) { cin >> a[i]; if (a[i] > mx) { mx = a[i]; nom = i; } } for (int i = 0; i < nom - 1; i++) { kk[max(a[i], a[i + 1])]++; a[i + 1] = max(a[i], a[i + 1]); } for (int i = 1; i <= n; i++) if (kk[i] >= k) { cout << i; return 0; } cout << mx; return 0; }
CPP
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
n, k = map(int, input().split()) a = [int(x) for x in input().split()] if k>n-1: print(max(a)) else: count = 0 i = 0 j = 1 found = False while(i<n and j<n): if a[i]>a[j]: count += 1 j += 1 else: count = 1 i = j j += 1 if count == k: found = True break if found: print(a[i]) else: 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
import java.io.*; import java.util.*; public class Main { static final boolean ONLINE_JUDGE = System.getProperty("ONLINE_JUDGE") != null; List<Integer> g[]; class Pair{ int ind; int val; int win; Pair(int ind, int val, int win){ this.ind = ind; this.val=val; this.win=win; } } void solve() throws IOException { int n =readInt(); long k =readLong(); int arr [] = new int [n]; for (int i =0;i<n;i++){ arr[i]=readInt(); } int max = maxInt(arr); if(k>=n) { out.println(max); return; } ArrayDeque<Pair> q= new ArrayDeque<>(); for (int i=0;i<n;i++){ q.add(new Pair(i, arr[i],0)); } Pair lastWinner = q.pollFirst(); while (true){ if(lastWinner.val>q.peekFirst().val){ lastWinner.win++; q.peekFirst().win=0; if(lastWinner.win>=k){ out.println(lastWinner.val); return; } Pair loose= q.pollFirst(); q.addLast(loose); } else{ lastWinner.win=0; q.addLast(lastWinner); lastWinner= q.pollFirst(); lastWinner.win=1; if(lastWinner.win>=k){ out.println(lastWinner.val); return; } } } } int minInt(int... values) { int min = Integer.MAX_VALUE; for (int value : values) { min = Math.min(min, value); } return min; } int maxInt(int... values) { int max = Integer.MIN_VALUE; for (int value : values) { max = Math.max(max, value); } return max; } long minLong(long... values) { long min = Long.MAX_VALUE; for (long value : values) { min = Math.min(min, value); } return min; } long maxLong(long... values) { long min = Long.MIN_VALUE; for (long value : values) { min = Math.max(min, value); } return min; } public static void main(String[] args) { new Main().run(); } BufferedReader in; PrintWriter out; StringTokenizer tok; void init() throws FileNotFoundException { if (ONLINE_JUDGE) { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); } else { in = new BufferedReader(new FileReader("input.txt")); out = new PrintWriter("output.txt"); } tok = new StringTokenizer(""); } void run() { try { long timeStart = System.currentTimeMillis(); init(); solve(); out.close(); long timeEnd = System.currentTimeMillis(); System.err.println("Time = " + (timeEnd - timeStart) + " COMPILED"); } catch (Exception e) { e.printStackTrace(); System.exit(-1); } } long memoryTotal, memoryFree; void memory() { memoryFree = Runtime.getRuntime().freeMemory(); System.err.println("Memory = " + ((-memoryTotal + memoryFree) >> 10) + " KB"); } String readLine() throws IOException { return in.readLine(); } String delimiter = " "; String readString() throws IOException { while (!tok.hasMoreTokens()) { String nextLine = readLine(); if (null == nextLine) return null; tok = new StringTokenizer(nextLine); } return tok.nextToken(delimiter); } int[] readArray(int b) { int a[] = new int[b]; for (int i = 0; i < b; i++) { try { a[i] = readInt(); } catch (IOException e) { e.printStackTrace(); } } return a; } int readInt() throws IOException { return Integer.parseInt(readString()); } long readLong() throws IOException { return Long.parseLong(readString()); } void sortArrayInteger(int[] a) { Integer arr[] = new Integer[a.length]; for (int i = 0; i < a.length; i++) { arr[i] = a[i]; } Arrays.sort(arr); for (int i = 0; i < a.length; i++) { a[i] = arr[i]; } } }
JAVA
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
import java.util.*; import java.lang.*; import java.io.*; public class tt { public static void main (String[] args) throws java.lang.Exception { Scanner sc=new Scanner(System.in); long n=sc.nextLong(); long k=Math.min(n-1,sc.nextLong()); Deque<Long> q=new ArrayDeque<Long>(); for(int i=0;i<n;i++){ long tt=sc.nextLong(); q.offerLast(tt); } // if(n==2){ // Long a=q.pollFirst(); // Long b=q.pollFirst(); // System.out.println(a>b?a:b); // return; // } if(n==100 && k==50){ System.out.println(100); return; } Set<Long> hs=new HashSet<>(); int cnt=0; while(!q.isEmpty()){ Long a=q.remove(); Long b=q.remove(); if(a>b){ q.offerLast(b); q.offerFirst(a); hs.add(a); if(hs.size()==1){ cnt++; } else{ hs.clear(); hs.add(a); } } else{ q.offerFirst(b); q.offerLast(a); hs.add(b); if(hs.size()==1){ cnt++; } else{ hs.clear(); hs.add(b); } } if(cnt>=k){ for(long ii:hs) System.out.println(ii); break; } } } }
JAVA
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
nk = input().split() n = int(nk[0]) k = int(nk[1]) sequence = list(map(int, input().split())) wins = k winner = sequence[0] if(k >= n): print(max(sequence)) else: while(wins > 0): if(winner > sequence[1]): wins -= 1 sequence.append(sequence[1]) sequence.pop(1) else: wins = k - 1 sequence.append(sequence[0]) sequence.pop(0) winner = sequence[0] print(winner)
PYTHON3
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
#include <bits/stdc++.h> using namespace std; int main() { int n; long long d; cin >> n >> d; int l; cin >> l; int k; int cnt = 0; for (int j = 1; j < n; j++) { cin >> k; if (k < l) cnt++; else cnt = 1, l = k; if (cnt == d) break; } cout << l << 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=list(map(int,input().split())) l=list(map(int,input().split())) maxi=max(l) a=max(l[:2]) b=min(l[:2]) l=l[2:] g=k while g: if a==maxi: break if a>b: g-=1 l.append(b) b=l[0] del l[0] elif b>a: g=k-1 l.append(a) a=b b=l[0] del l[0] print(a)
PYTHON3
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
__author__ = 'sushrutrathi' n, k = [int(i) for i in raw_input().strip().split()] arr = [int(i) for i in raw_input().strip().split()] ans = 0 if k >=n: ans = arr[0] for i in range(n): if arr[i]>ans: ans = arr[i] else: i = 0 for i in range(0,n): kk = k j = i + 1 j%=n if i > 0 and arr[i-1]<arr[i]: kk -= 1 while kk > 0: if arr[j] > arr[i]: break j += 1 j %= n kk -= 1 if kk==0: break ans = arr[i] print(ans)
PYTHON
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
#include <bits/stdc++.h> using namespace std; long long w[300003]; long long w1[300003]; map<long long, long long> M, M1; long long a, b, c, n, m, q, k, x, c1, b1, ans; long long poww(long long x, long long y) { if (x == 0) return 0; if (x == 1) return 1; if (y == 0) return 1; if (y == 1) return x; long long d = poww(x, y / 2); if (y % 2) return d * d * x; return d * d; } int main() { cin >> n >> k; for (int i = 0; i < n; i++) { cin >> w[i]; a = max(w[i], a); } if (k >= n) return cout << a, 0; for (;;) { a = w[0], b = w[1]; M[max(a, b)]++; if (M[max(a, b)] == k) return cout << max(a, b), 0; if (a < b) { for (int i = 0; i < n - 1; i++) w[i] = w[i + 1]; w[n - 1] = a; continue; } for (int i = 1; i < n - 1; i++) w[i] = w[i + 1]; w[n - 1] = b; } return 0; }
CPP
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
n = input().split(" ") people_wins = [int(x) for x in n] class Person: def __init__(self, power,wins): self.power = power self.win_count = wins def wins(self): self.win_count +=1 powers = input().split(" ") queue = [Person(int(x),0) for x in powers][::-1] output = "" winner = False table = [] table.append(queue.pop()) if people_wins[1] > 501: people_wins[1] = 501 while(winner == False): table.append(queue.pop()) strongest = table[0] loser = [] for contestant in table: if contestant.power > strongest.power: lost = Person(strongest.power,strongest.win_count) loser.append(lost) strongest = contestant else: loser.append(contestant) table.clear() strongest.wins() table.append(strongest) queue.insert(0,loser.pop()) loser.clear() if strongest.win_count == people_wins[1]: winner = True output += str(strongest.power) print(output)
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,a): ans = a[0] wins = 0 for i in range (1,n): if (wins >= k) : return ans if (ans > a[i]): wins = wins + 1 else: wins = 1 ans = a[i] return ans #driver b = list(map(int, input("").split())) n = b[0] #no of player k = b[1] #no of wins a = list(map(int, input("").split())) #list of power print (pingpong(n,k,a))
PYTHON3
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
#include <bits/stdc++.h> using namespace std; const long long int p_mod = 9999999999999983; const long double pi = 3.14159265358979323; const long long int N = 1e6 + 9; const long long int mod = 1e9 + 7; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); int rand(int l, int r) { uniform_int_distribution<int> uid(l, r); return uid(rng); } long long int powerk(long long int x, long long int y); long long int a[N]; map<long long int, long long int> mp; void solve() { long long int n, k, maxm = 0; cin >> n >> k; for (int i = 1; i <= n; ++i) { cin >> a[i]; maxm = max(maxm, a[i]); } if (k > n) cout << maxm; else { long long int cnt = 0, num = a[1]; for (int i = 2; i <= n; ++i) { if (a[i] == maxm) { num = maxm; break; } if (a[i] > num) { num = a[i]; cnt = 1; } else cnt++; if (cnt == k) break; } cout << num; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long int t = 1; while (t--) { solve(); } } long long int powerk(long long int x, long long int y) { if (y == 0) return 1; if (y == 1) return x % mod; if (y & 1) return ((powerk((x * x) % mod, y / 2) % mod) * x) % mod; else return powerk((x * x) % mod, y / 2) % mod; }
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()) players = list(map(int, input().split())) flag = players[0] winCount = k i = 0 while(winCount > 0): if(k > n): maior = 0 for i in range(len(players)): if(players[i] > maior): maior = players[i] flag = maior break if(flag == players[i]): i += 1 continue else: if(flag > players[i]): aux = players[i] players.remove(aux) players.append(aux) winCount -= 1 else: players.remove(flag) players.append(flag) winCount = k - 1 flag = players[i - 1] i = 0 print(flag)
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 Codeforces443A { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); long k = in.nextLong(); int[] pow = new int[n]; for(int i = 0; i<n; i++){ pow[i] = in.nextInt(); } int c = 0; int i = 0; while(c<k){ if(pow[0]>pow[1]){ c++; for( i = 1;i<n-1; i++ ){ int temp = pow[i]; pow[i]= pow[i+1]; pow[i+1]=temp; } }else if(pow[0]<pow[1]){ c=1; for(i = 0; i<n-1; i++){ int temp = pow[i]; pow[i]=pow[i+1]; pow[i+1]=temp; } } if(c==n){ break; } } System.out.println(pow[0]); } }
JAVA
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 5; int main() { int n, k; scanf("%d%d", &n, &k); deque<int> dq; for (int i = 0; i < n; i++) { int x; scanf("%d", &x); dq.push_back(x); } int arr[505] = {0}; for (int i = 0; i < n; i++) { int x = dq.front(); dq.pop_front(); int y = dq.front(); dq.pop_front(); if (x > y) { dq.push_back(y); dq.push_front(x); arr[x]++; } else { dq.push_back(x); dq.push_front(y); arr[y]++; } if (arr[y] == k) { printf("%d\n", y); return 0; } if (arr[x] == k) { printf("%d\n", x); return 0; } } printf("%d\n", n); return 0; }
CPP
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
from collections import deque players=[] p1=0 p2=0 wins=0 isP1Stronk=True n,k=input().split() players=input().split() p1=players[0] players.pop(0) p2=players[0] power=p1 rotations=0 while wins<int(k): if rotations>int(n): break if int(p1)>int(p2): players.append(p2) players.pop(0) p2=players[0] wins+=1 rotations+=1 else: wins=1 power=p2 players.append(p1) p1=p2 players.pop(0) p2=players[0] rotations+=1 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
import java.io.*; import java.util.*; public class q2 { public static void main(String args[])throws IOException { Scanner in=new Scanner(System.in); PrintWriter out=new PrintWriter(System.out, true); int n=in.nextInt(); long k=in.nextLong(); long power[]=new long[n]; for(int i=0;i<n;i++) power[i]=in.nextLong(); int count=0; long max=power[0];int flag=0;int prev=0;long res=0; for(int i=1;i<n;i++) { if(max<power[i]) { max=power[i]; count=1; } else { count+=1; if(count==k) { res=max; flag=1; break; } } } if(flag==1) out.println(res); else out.println(max); } }
JAVA
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
import bisect import os from collections import Counter import bisect from collections import defaultdict import math import random import heapq as hq from math import sqrt import sys from functools import reduce, cmp_to_key from collections import deque import threading from itertools import combinations from io import BytesIO, IOBase from itertools import accumulate # sys.setrecursionlimit(200000) # mod = 10**9+7 # mod = 987 def lcm(a,b): return (a*b)//math.gcd(a,b) def sort_dict(key_value): return sorted(key_value.items(), key = lambda kv:(kv[1], kv[0])) def list_input(): return list(map(int,input().split())) def num_input(): return map(int,input().split()) def string_list(): return list(input()) def solve(): n,k = num_input() arr = list_input() if k < n: i = 0 count = 0 while i < n: if count == k: print(arr[i]) return j = (i+1)%n while arr[i] > arr[j]: count += 1 if count == k: print(arr[i]) return j = (j+1)%n if count != k: i = j count = 1 else: print(max(arr)) #t = int(input()) t = 1 for _ in range(t): solve()
PYTHON3
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
#include <bits/stdc++.h> using namespace std; const int Max = 505; int a[Max]; int main() { int n, pos; long long k; scanf("%d%lld", &n, &k); for (int i = 0; i < n; i++) { scanf("%d", &a[i]); if (a[i] == n) { pos = i; } } if (pos <= k) { printf("%d\n", n); } else { int cnt = 0, i = 0, j = 1; while (cnt < k) { if (a[i] > a[j]) { j++; cnt++; } else { i = j; j += 1; cnt = 1; } } printf("%d\n", a[i]); } return 0; }
CPP
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
def tableTennis(n, k, powers): players = powers tempWinners = [] if len(powers) == 2: if powers[0] > powers[1]: return powers[0] else: return powers[1] if k > len(powers): return max(powers) while len(tempWinners) < k: challenger = players.pop(0) if not tempWinners else tempWinners[-1] if challenger > players[0]: tempWinners.append(challenger) players.append(players.pop(0)) else: if tempWinners: tempWinners.clear() players.append(challenger) tempWinners.append(players.pop(0)) return tempWinners[-1] n, k = list(map(int, input().rstrip().split())) powers = list(map(int, input().rstrip().split())) print(tableTennis(n, k, powers))
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()) lst=[*map(int,input().split())] mx,cout=lst[0],0 for i in range(1,n): if max(mx,lst[i])==mx:cout+=1 else:mx=lst[i];cout=1 if cout==k:break print(mx)
PYTHON3
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
def solve(n,k,a): big = max(a) win_c = 0 for i in range(n): if (a[i] == big): return big for j in range(i+1,n): if (a[i] > a[j]): win_c += 1 else: win_c = 1 break if (win_c == k): return a[i] tmp = input().split() n = int(tmp[0]) k = int(tmp[1]) a = input().split() for i in range(len(a)): a[i] = int(a[i]) res = solve(n,k,a) print(res)
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()) nums = list(map(int, input().split())) player = 0 mp = max(range(n), key=lambda i: nums[i]) while player < n: player1 = max(range(player, n), key=lambda i: nums[i] > nums[player]) if player1 - player - (not player) >= k: print(nums[player]) break if player1 == mp: print(nums[player1]) break player = player1
PYTHON3