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
n, k = map(int, input().split()) A = list(map(int, input().split())) Q = max(A) cnt = 0 power = A[0] if A[0] == Q: print(Q) exit(0) for i in range(1, n): if cnt == k: print(power) exit(0) if A[i] == Q: print(Q) exit(0) elif A[i] < power: cnt += 1 else: cnt = 1 power = A[i]
PYTHON3
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
# @Date: 26-Oct-2017 # @Last modified time: 26-Oct-2017 # after index of max whatever be the power max will win n,k=map(int,input().split()) a=list(map(int,input().split())) maxi=-1 index=-1 # get the maxi index and value for i in range(n): if a[i]>maxi: maxi=a[i] index=i import sys # before i do the shifting. After that print(maxi) if k>=index: print(maxi) else: count=0 while a[0]!=maxi: if a[0]>a[1]: count+=1 if count==k: print(a[0]) sys.exit() a=[a[0]]+a[2:]+[a[1]] else: count=1 a=[a[1]]+a[2:]+[a[0]] if a[0]==maxi: print(maxi)
PYTHON3
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
#include <bits/stdc++.h> using namespace std; template <typename P, typename Q> ostream& operator<<(ostream& os, pair<P, Q> p) { os << "(" << p.first << "," << p.second << ")"; return os; } int main(int argc, char* argv[]) { ios_base::sync_with_stdio(0); cin.tie(0); int n; long long int k; while (cin >> n >> k) { deque<int> q; for (int i = 0; i < n; ++i) { int a; cin >> a; q.push_back(a); } int x = n; while (true) { int a = q.front(); q.pop_front(); int b = q.front(); q.pop_front(); int cnt = 1; int c = max(a, b); q.push_back(min(a, b)); while (cnt < min<long long int>(n, k)) { if (c < q.front()) { break; } ++cnt; q.push_back(q.front()); q.pop_front(); } if (min<long long int>(n, k) == cnt) { x = c; break; } q.push_front(c); } cout << x << 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.*; import java.io.*; import java.lang.Math.*; public class MainA { static int mod = (int) 1e9 + 7; public static void main(String[] args) throws java.lang.Exception { InputReader in = new InputReader(System.in); PrintWriter out=new PrintWriter(System.out); int n=in.nextInt(); long k=in.nextLong(); int a[]=new int[n]; int max=-1; for(int i=0;i<n;i++) { a[i]=in.nextInt(); max=Math.max(max,a[i]); } if(n<=k) { out.println(max); } else { for(int i=0;i<n;) { int count=0; while(count<k) { if(i+count+1==n) { out.println(a[i]); out.close(); return; } if(a[i+count+1]<a[i])count++; else break; if((i==0 && count==k )||(i!=0 && count+1==k)) { out.println(a[i]); out.close(); return; } } i=i+count+1; } } out.close(); } public static class Trie { Trie []children = new Trie[26]; int value; int leve; Trie() { value=1; for (int i = 0; i < 26; i++) children[i] = null; } public static void insert(Trie root,String key) { int length = key.length(); int index; Trie pCrawl = root; int k=1; for (int level = length-1; level >=0; level--) { index = key.charAt(level) - 'a'; if (pCrawl.children[index] == null) pCrawl.children[index] = new Trie(); else { pCrawl.children[index].value++; } pCrawl.children[index].leve=k++; pCrawl = pCrawl.children[index]; } } public static void dfs(Trie root,int len[]) { for(int i=0;i<26;i++) { if(root.children[i]!=null) { int level=root.children[i].leve; int value=root.children[i].value; len[level-1]=Math.max(len[level-1],value); dfs(root.children[i],len); } } } } public static long add(long a,long b) { return (a+b)%mod; } public static long mul(long a,long b) { return (a*b)%mod; } public static long sub(long a,long b) { return (a-b+mod)%mod; } static class Pairs implements Comparable<Pairs> { int x; int y; Pairs(int a, int b) { x = a; y = b; } @Override public boolean equals(Object o) { Pairs other=(Pairs)o; return x==other.x && y==other.y; } @Override public int compareTo(Pairs o) { // TODO Auto-generated method stub return Integer.compare(x,o.x); } @Override public int hashCode() { return Objects.hash(x,y); } } public static void debug(Object... o) { System.out.println(Arrays.deepToString(o)); } public static boolean isPal(String s) { for (int i = 0, j = s.length() - 1; i <= j; i++, j--) { if (s.charAt(i) != s.charAt(j)) return false; } return true; } public static String rev(String s) { StringBuilder sb = new StringBuilder(s); sb.reverse(); return sb.toString(); } public static long gcd(long x, long y) { if (y == 0) return x; if (x % y == 0) return y; else return gcd(y, x % y); } public static int gcd(int x, int y) { if(y==0) return x; if (x % y == 0) return y; else return gcd(y, x % y); } public static long gcdExtended(long a, long b, long[] x) { if (a == 0) { x[0] = 0; x[1] = 1; return b; } long[] y = new long[2]; long gcd = gcdExtended(b % a, a, y); x[0] = y[1] - (b / a) * y[0]; x[1] = y[0]; return gcd; } public static 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[][] mul(long a[][], long b[][]) { long c[][] = new long[2][2]; c[0][0] = a[0][0] * b[0][0] + a[0][1] * b[1][0]; c[0][1] = a[0][0] * b[0][1] + a[0][1] * b[1][1]; c[1][0] = a[1][0] * b[0][0] + a[1][1] * b[1][0]; c[1][1] = a[1][0] * b[0][1] + a[1][1] * b[1][1]; return c; } public static long[][] pow(long n[][], long p, long m) { long result[][] = new long[2][2]; result[0][0] = 1; result[0][1] = 0; result[1][0] = 0; result[1][1] = 1; if (p == 0) return result; if (p == 1) return n; while (p != 0) { if (p % 2 == 1) result = mul(result, n); //System.out.println(result[0][0]); for (int i = 0; i < 2; i++) { for (int j = 0; j < 2; j++) { if (result[i][j] >= m) result[i][j] %= m; } } p >>= 1; n = mul(n, n); // System.out.println(1+" "+n[0][0]+" "+n[0][1]+" "+n[1][0]+" "+n[1][1]); for (int i = 0; i < 2; i++) { for (int j = 0; j < 2; j++) { if (n[i][j] >= m) n[i][j] %= m; } } } return result; } public static long pow(long n,long p, long m) { long result=1; if(p==0)return 1; if(p==1)return n%m; while(p!=0) { if(p%2==1)result=(result*n)%m; p >>=1; n=(n*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 class InputReader { private final InputStream stream; private final byte[] buf = new byte[8192]; private int curChar, snumChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int snext() { if (snumChars == -1) throw new InputMismatchException(); if (curChar >= snumChars) { curChar = 0; try { snumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (snumChars <= 0) return -1; } return buf[curChar++]; } public int nextInt() { int c = snext(); while (isSpaceChar(c)) { c = snext(); } int sgn = 1; if (c == '-') { sgn = -1; c = snext(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = snext(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = snext(); while (isSpaceChar(c)) { c = snext(); } int sgn = 1; if (c == '-') { sgn = -1; c = snext(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = snext(); } while (!isSpaceChar(c)); return res * sgn; } public int[] nextIntArray(int n) { int a[] = new int[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } return a; } public long[] nextLongArray(int n) { long a[] = new long[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } return a; } public String readString() { int c = snext(); while (isSpaceChar(c)) { c = snext(); } StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = snext(); } while (!isSpaceChar(c)); return res.toString(); } public String nextLine() { int c = snext(); while (isSpaceChar(c)) c = snext(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = snext(); } while (!isEndOfLine(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }
JAVA
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
#include <bits/stdc++.h> using namespace std; int main() { long long n, k; int i, a, b, c, best = 0, g; cin >> n >> k; cin >> a; g = 0; for (i = 1; i < n; i++) { cin >> b; if (a > b) g++; else { a = b; g = 1; } if (g >= k) { cout << a; return 0; } } cout << a; 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.lang.reflect.Array; import java.math.BigInteger; import java.util.*; import java.io.IOException; import java.io.InputStream; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.stream.Collector; import java.util.stream.Collectors; import java.util.stream.IntStream; public class Main { public static void main(String[] args) { File file = new File("in.txt"); File fileOut = new File("out.txt"); InputStream inputStream = null; OutputStream outputStream = null; // try {inputStream= new FileInputStream(file);} catch (FileNotFoundException ex){return;}; // try {outputStream= new FileOutputStream(fileOut);} catch (FileNotFoundException ex){return;}; inputStream = System.in; outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); Task solver = new Task(); solver.solve(1, in, out); out.close(); } } class Task { List<Integer> pow; public void solve(int testNumber, InputReader in, PrintWriter out) { Integer n = in.nextInt(); Long k = in.nextLong(); pow = new ArrayList<>(n); Integer maxPow = Integer.MIN_VALUE; for(int i=0; i<n; i++){ Integer p = in.nextInt(); maxPow = Math.max(maxPow, p); pow.add(p); } if(k>=n-1){ out.println(maxPow); return; } for(int i=0; i<n; i++){ Integer candidate = pow.get(i); if(checkCandidate(candidate, k, i+1)){ out.println(candidate); return; } } out.println(maxPow); } public Boolean checkCandidate(Integer p, Long k, Integer index){ if(index>1 && pow.get(index-2) < p){ k--; } for(int i=index; i<Math.min(index+k, pow.size()); i++){ if(pow.get(i)>=p){ return Boolean.FALSE; } } return Boolean.TRUE; } } class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public String nextLine(){ try { return reader.readLine(); } catch (IOException e){ throw new RuntimeException(e); } } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } } class Pair<F, S> { public final F first; public final S second; public Pair(F first, S second) { this.first = first; this.second = second; } @Override public boolean equals(Object o) { if (!(o instanceof Pair)) { return false; } Pair<?, ?> p = (Pair<?, ?>) o; return Objects.equals(p.first, first) && Objects.equals(p.second, second); } @Override public int hashCode() { return (first == null ? 0 : first.hashCode()) ^ (second == null ? 0 : second.hashCode()); } @Override public String toString() { return "(" + first + ", " + second + ')'; } } class IntPair extends Pair<Integer, Integer>{ public IntPair(Integer first, Integer second){ super(first, second); } }
JAVA
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.io.BufferedWriter; import java.util.InputMismatchException; import java.io.IOException; import java.io.Writer; import java.io.OutputStreamWriter; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author Rustam Musin ([email protected]) */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); OutputWriter out = new OutputWriter(outputStream); TaskB solver = new TaskB(); solver.solve(1, in, out); out.close(); } static class TaskB { public void solve(int testNumber, InputReader in, OutputWriter out) { int n = in.readInt(); long k = in.readLong(); IntIntPair[] a = new IntIntPair[n]; for (int i = 0; i < n; i++) { a[i] = IntIntPair.makePair(i, in.readInt()); } int[] wins = new int[n]; while (!ready(a)) { boolean firstWins = a[0].second > a[1].second; int winnerAt = firstWins ? 0 : 1; if (++wins[a[winnerAt].first] == k) { out.print(a[winnerAt].second); return; } move(a, winnerAt ^ 1); } out.print(a[0].second); } void move(IntIntPair[] a, int from) { IntIntPair x = a[from]; for (int i = from + 1; i < a.length; i++) { a[i - 1] = a[i]; } a[a.length - 1] = x; } boolean ready(IntIntPair[] a) { for (int i = 1; i < a.length; i++) { if (a[i].second > a[0].second) { return false; } } return true; } } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private InputReader.SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) { throw new InputMismatchException(); } if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) { return -1; } } return buf[curChar++]; } public int readInt() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long readLong() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public boolean isSpaceChar(int c) { if (filter != null) { return filter.isSpaceChar(c); } return isWhitespace(c); } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } static class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void close() { writer.close(); } public void print(int i) { writer.print(i); } } static class IntIntPair implements Comparable<IntIntPair> { public final int first; public final int second; public static IntIntPair makePair(int first, int second) { return new IntIntPair(first, second); } public IntIntPair(int first, int second) { this.first = first; this.second = second; } public boolean equals(Object o) { if (this == o) { return true; } if (o == null || getClass() != o.getClass()) { return false; } IntIntPair pair = (IntIntPair) o; return first == pair.first && second == pair.second; } public int hashCode() { int result = first; result = 31 * result + second; return result; } public String toString() { return "(" + first + "," + second + ")"; } @SuppressWarnings({"unchecked"}) public int compareTo(IntIntPair o) { int value = Integer.compare(first, o.first); if (value != 0) { return value; } return Integer.compare(second, o.second); } } }
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 MX = 100000; int ini[505]; int cnt[505]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; long long k; int mx = 0; cin >> n >> k; for (int i = (1); i <= (n); i++) { cin >> ini[i]; mx = max(mx, ini[i]); } int p = ini[1]; for (int i = (2); i <= (n); i++) { if (ini[i] == mx) { cout << mx << "\n"; return 0; } if (p > ini[i]) { cnt[p]++; } else { p = ini[i]; cnt[p]++; } if (cnt[p] == k) { cout << p << "\n"; return 0; } } 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.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); long k = sc.nextLong(); int[] a = new int[n]; int[] score = new int[n]; for (int i = 0; i < n; i++) { a[i] = sc.nextInt(); score[i] = 0; } int max = a[0]; int cnt = 0; for (int i = 1; i < n; i++) { if (a[i] > max) { max = a[i]; cnt = 1; } else { cnt++; } if (cnt == k) { break; } } System.out.print(max); } }
JAVA
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
#include <bits/stdc++.h> using namespace std; int main() { int n, k; int a[600], st; cin >> n >> k; for (int i = 0; i < n; i++) { scanf("%d", &a[i]); } int ans = a[0]; st = 0; for (int i = 1; i < n; i++) { if (st >= k) { cout << ans << endl; return 0; } if (ans > a[i]) { st++; } else { st = 1; ans = a[i]; } } cout << ans << endl; return 0; }
CPP
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
n,k=map(int,raw_input().split()) l=map(int,raw_input().split()) flag=1 d={} for i in range(1,n+1): d[i]=0 t=0 for i in range(1,len(l)): if l[t]>l[i]: d[l[t]]+=1 else: d[l[i]]+=1 t=i; if d[l[t]]==k: ans=l[t] flag=0 break if flag==0: print ans else: print max(l)
PYTHON
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
import java.io.*; import java.util.*; import java.util.Map.Entry; import java.lang.Math; import java.math.BigInteger; public class Problem { class Pair { int x, y; Pair(int x, int y) { this.x = x; this.y = y; } } class Doc implements Comparable<Doc>{ int start; int rep; Doc(int start, int rep){ this.start = start; this.rep = rep; } @Override public int compareTo(Doc other) { if(this.start>other.start){ return 1; } if(this.start<other.start){ return -1; } if(this.rep<other.rep){ return 1; } return -1; } } void solve() throws IOException { int n = rI(); long k = rL(); int[] a = rA(n); if(k>=n){ out.println(n);return; } int cur = a[0]; int counter = 0; for(int i=1;i<n;++i){ if(a[i]<cur){ counter++; }else{ counter = 1; cur = a[i]; } if(counter==k){ out.println(cur);return; } if(cur==n){ out.println(n);return; } } } int checkBit(int mask, int bit) { return (mask >> bit) & 1; } public static void main(String[] args) throws IOException { new Problem().run(); } boolean isLower(char a) { return ((int) a) >= 97 ? true : false; } int INF = Integer.MAX_VALUE - 12345; int[][] STEPS = { { 0, 1 }, { 1, 0 }, { -1, 0 }, { 0, -1 } }; BufferedReader in; PrintWriter out; StringTokenizer tok; Random rnd = new Random(); static final boolean ONLINE_JUDGE = System.getProperty("ONLINE_JUDGE") != null; Problem() 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(""); } long checkBit(long mask, int bit) { return (mask >> bit) & 1; } // ====================================================== // ====================================================== void run() throws IOException { solve(); out.close(); } char[] reverseCharArray(char[] arr) { char[] ans = new char[arr.length]; for (int i = 0; i < arr.length; ++i) { ans[i] = arr[arr.length - i - 1]; } return ans; } int sqrt(double m) { int l = 0; int r = 1000000000 + 9; int i = 1000; while (r - l > 1) { int mid = (r + l) / 2; if (mid * mid > m) { r = mid; } else { l = mid; } } return l; } int countPow(int m, int n) { int ans = 0; while (m % n == 0 && m > 0) { ans++; m /= n; } return ans; } long binPow(long a, long b) { if (b == 0) { return 1; } if (b % 2 == 1) { return a * binPow(a, b - 1); } else { long c = binPow(a, b / 2); return c * c; } } int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } long pow(long x, long k) { long ans = 1; for (int i = 0; i < k; ++i) { ans *= x; } return ans; } // //////////////////////////////////////////////////////////////////// String delimiter = " "; String readLine() throws IOException { return in.readLine(); } String rS() throws IOException { while (!tok.hasMoreTokens()) { String nextLine = readLine(); if (null == nextLine) return null; tok = new StringTokenizer(nextLine); } return tok.nextToken(delimiter); } int rI() throws IOException { return Integer.parseInt(rS()); } long rL() throws IOException { return Long.parseLong(rS()); } double rD() throws IOException { return Double.parseDouble(rS()); } int[] rA(int b) { int a[] = new int[b]; for (int i = 0; i < b; i++) { try { a[i] = rI(); } catch (IOException e) { e.printStackTrace(); } } return a; } int[] readSortedIntArray(int size) throws IOException { Integer[] array = new Integer[size]; for (int index = 0; index < size; ++index) { array[index] = rI(); } Arrays.sort(array); int[] sortedArray = new int[size]; for (int index = 0; index < size; ++index) { sortedArray[index] = array[index]; } return sortedArray; } int[] sortedIntArray(int size, int[] array) throws IOException { for (int index = 0; index < size; ++index) { array[index] = rI(); } Arrays.sort(array); int[] sortedArray = new int[size]; for (int index = 0; index < size; ++index) { sortedArray[index] = array[index]; } return sortedArray; } }
JAVA
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
def play(a,gameNum,k): while gameNum<k: if a[0]>a[1]: a=[a[0]]+a[2:]+[a[1]] gameNum+=1 else: a=[a[1]]+a[2:]+[a[0]] gameNum=1 return a[0] n,k=map(int,raw_input().split()) a=map(int,raw_input().split()) if k>n: print max(a) else: print play(a,0,k)
PYTHON
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
#include <bits/stdc++.h> using namespace std; int main() { int n, power[1000], s = 0, i, p, maxpower = 0; long long int k; cin >> n >> k; for (i = 0; i < n; i++) { cin >> power[i]; if (power[i] > maxpower) maxpower = power[i]; } p = power[0]; if (p == maxpower) cout << p << endl; else for (i = 1; i < n; i++) { if (p > power[i]) s++; if (s == k) { cout << p << endl; break; } if (p < power[i]) { p = power[i]; if (p == maxpower) { cout << p << endl; break; } s = 1; } } return 0; }
CPP
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
n,k = map(int,raw_input().split(" ")) arr = map(int,raw_input().split(" ")) wins = [0]*505 p = 0 flag = False for i in range(1,n): if arr[p] > arr[i]: wins[arr[p]] += 1 else: wins[arr[i]] += 1 p = i if wins[arr[p]] == k: flag = True break if flag: print arr[p] else: print max(arr)
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
#NYAN NYAN #β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘ #β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–„β–„β–„β–„β–„β–„β–„β–„β–„β–„β–„β–„β–„β–„β–„β–„β–„β–„β–„β–„β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘ #β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–„β–€β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–„β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–€β–„β–‘β–‘β–‘β–‘β–‘β–‘β–‘ #β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–ˆβ–‘β–‘β–„β–‘β–‘β–‘β–‘β–„β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–ˆβ–‘β–‘β–‘β–‘β–‘β–‘β–‘ #β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–ˆβ–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–„β–ˆβ–„β–„β–‘β–‘β–„β–‘β–‘β–‘β–ˆβ–‘β–„β–„β–„β–‘β–‘β–‘ #β–‘β–„β–„β–„β–„β–„β–‘β–‘β–ˆβ–‘β–‘β–‘β–‘β–‘β–‘β–€β–‘β–‘β–‘β–‘β–€β–ˆβ–‘β–‘β–€β–„β–‘β–‘β–‘β–‘β–‘β–ˆβ–€β–€β–‘β–ˆβ–ˆβ–‘β–‘ #β–‘β–ˆβ–ˆβ–„β–€β–ˆβ–ˆβ–„β–ˆβ–‘β–‘β–‘β–„β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–ˆβ–ˆβ–‘β–‘β–‘β–‘β–€β–€β–€β–€β–€β–‘β–‘β–‘β–‘β–ˆβ–ˆβ–‘β–‘ #β–‘β–‘β–€β–ˆβ–ˆβ–„β–€β–ˆβ–ˆβ–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–€β–‘β–ˆβ–ˆβ–€β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–€β–ˆβ–ˆβ–‘ #β–‘β–‘β–‘β–‘β–€β–ˆβ–ˆβ–ˆβ–ˆβ–‘β–€β–‘β–‘β–‘β–‘β–„β–‘β–‘β–‘β–ˆβ–ˆβ–‘β–‘β–‘β–„β–ˆβ–‘β–‘β–‘β–‘β–„β–‘β–„β–ˆβ–‘β–‘β–ˆβ–ˆβ–‘ #β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–€β–ˆβ–‘β–‘β–‘β–‘β–„β–‘β–‘β–‘β–‘β–‘β–ˆβ–ˆβ–‘β–‘β–‘β–‘β–„β–‘β–‘β–‘β–„β–‘β–‘β–„β–‘β–‘β–‘β–ˆβ–ˆβ–‘ #β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–„β–ˆβ–„β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–€β–„β–‘β–‘β–€β–€β–€β–€β–€β–€β–€β–€β–‘β–‘β–„β–€β–‘β–‘ #β–‘β–‘β–‘β–‘β–‘β–‘β–ˆβ–€β–€β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–€β–€β–€β–€β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–€β–‘β–‘β–‘β–‘ #β–‘β–‘β–‘β–‘β–‘β–‘β–ˆβ–ˆβ–ˆβ–ˆβ–€β–‘β–‘β–ˆβ–ˆβ–ˆβ–€β–‘β–‘β–‘β–‘β–‘β–‘β–€β–ˆβ–ˆβ–ˆβ–‘β–‘β–€β–ˆβ–ˆβ–€β–‘β–‘β–‘β–‘β–‘β–‘ #β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘ from __future__ import print_function import os import sys import fileinput import codecs import operator import functools import math inval = None file = None Debug = True import time def readSequence(elementType=int, inputLine=None, seperator=' ', strip=True): global file if (inputLine == None): inputLine = file.readline() if strip: inputLine = inputLine.strip() return [elementType(x) for x in inputLine.split(seperator)] def C(n, r): r = min(r, (n - r)) if (r == 0): return 1 if (r < 0): return 0 numer = functools.reduce(operator.mul, range(n, (n - r), (-1))) denom = functools.reduce(operator.mul, range(1, (r + 1))) return (numer // denom) def sumdigit(num): num = str(num) r = 0 for x in num: r = (r + int(x)) pass return r def digitListToInt(numList): return int(''.join(map(str, numList))) def listByDigit(digitLen): return [0 for _ in range(digitLen)] def F(n, k): ans = 0 for x in range(1, n): ans = (ans + (x * C((n - x), (k - 1)))) pass return ans pass def keyWithMaxValue(d1): v = list(d1.values()) return list(d1.keys())[v.index(max(v))] def checkProperIrreducible(a, b): if (a == 1): return True if (a == b): return False if (a > b): return checkProperIrreducible(b, a) else: return checkProperIrreducible(a, (b - a)) class MultiArray(object, ): def MultiArray(defaultList, listCount): this.Lists[0] = defaultList for i in range(1, listCount): this.Lists[i] = list() pass def MultiArray(listCount): for i in range(listCount): this.Lists[i] = list() pass def MakeRankList(idx): input = this.Lists[0] output = ([0] * len(input)) for (i, x) in enumerate(sorted(range(len(input)), key=(lambda y: input[y]))): output[x] = i this.Lists[idx] = output pass pass def solve(): solve879b() pass def solve879a(): rs = RobertsSpaceIndustries = readSequence n = rs()[0] cur = 0 for x in range(n): li = rs() s = li[0] d = li[1] i = 0 while (cur >= (s + (d * i))): i += 1 pass cur = (s + (d * i)) write(cur) pass def solve879b(): rs = RobertsSpaceIndustries = readSequence li = rs() (n, k) = (li[0], li[1]) li = rs() arr = li wins = ([0] * (n + 3)) if (k > 600): write(n) return while True: a = arr[0] b = arr[1] if (a > b): arr = (([a] + arr[2:]) + [b]) wins[a] += 1 if (wins[a] == k): write(a) return pass else: arr = (([b] + arr[2:]) + [a]) wins[b] += 1 if (wins[b] == k): write(b) return pass pass pass def ToSpaceSeperatedString(targetList): return ' '.join(map(str, targetList)) def solveTemplate(): global file rl = file.readline rs = RobertsSpaceIndustries = readSequence li = rs() (n, x) = (li[0], li[1]) pass inval = None def write(str1): print(str1) ti = time.time() file = fileinput.input(inval) solve()
PYTHON
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
n, k = map(int, input().split()) a = list(map(int, input().split())) k1 = 1 a1 = max(a[0], a[1]) for i in range(2, n): if a1 > a[i]: k1 +=1 else: a1 = a[i] k1 =1 if k1 == k: break print(a1)
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 com.sun.org.apache.bcel.internal.generic.AALOAD; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.math.BigInteger; import java.util.*; import java.util.stream.IntStream; import javafx.util.Pair; public class Main { static void sort(int a[]) { Random ran = new Random(); for (int i = 0; i < a.length; i++) { int r = ran.nextInt(a.length); int temp = a[r]; a[r] = a[i]; a[i] = temp; } Arrays.sort(a); } public static void main(String[] args) throws IOException { Scanner input = new Scanner(System.in); int n = input.nextInt(); long k = input.nextLong(); ArrayList<Integer> a = new ArrayList<>(); for (int i = 0; i < n; i++) { a.add(input.nextInt()); } int ans[] = new int[n+1]; int j=n; if(k>=n) { System.out.println(n); return; } for (int i = 0; ; i++) { if(a.get(0)<a.get(1)) { ans[a.get(1)]++; if(ans[a.get(1)]>=k) { System.out.println(a.get(1)); return; } int value = a.remove(0); // System.out.println(value); a.add(value); } else if(a.get(0)>a.get(1)) { ans[a.get(0)]++; if(ans[a.get(0)]>=k) { System.out.println(a.get(0)); return; } int value= a.remove(1); // System.out.println(value); a.add(value); } } } }
JAVA
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.lang.reflect.Array; import java.math.BigInteger; import java.net.InetAddress; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.PriorityQueue; import java.util.Scanner; import java.util.Set; import java.util.Stack; import java.util.StringTokenizer; import java.util.TreeSet; import javax.swing.tree.TreeNode; public class Main { static PrintWriter pw=new PrintWriter(System.out); static int test,max; public static void main(String[] args) throws IOException { Reader.init(System.in); int n=Reader.nextInt(),ar[]=new int[n]; long k=Reader.nextLong(); for (int i = 0; i < n; i++) { ar[i]=Reader.nextInt(); if(ar[i]>max) max=ar[i]; } int count=0; test=ar[0]; for (int i = 1; i < n; i++) { if(ar[i]>test) { count=1; test=ar[i]; } else count++; if(count==k) { pw.println(test); pw.close(); return; } if(test==max){ pw.println(max); pw.close(); return; } } pw.close(); } } class Reader { static BufferedReader reader; static StringTokenizer tokenizer; public static int pars(String x) { int num = 0; int i = 0; if (x.charAt(0) == '-') { i = 1; } for (; i < x.length(); i++) { num = num * 10 + (x.charAt(i) - '0'); } if (x.charAt(0) == '-') { return -num; } return num; } static void init(InputStream input) { reader = new BufferedReader( new InputStreamReader(input)); tokenizer = new StringTokenizer(""); } static void init(FileReader input) { reader = new BufferedReader(input); tokenizer = new StringTokenizer(""); } static String next() throws IOException { while (!tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer( reader.readLine()); } return tokenizer.nextToken(); } static int nextInt() throws IOException { return pars(next()); } static long nextLong() throws IOException { return Long.parseLong(next()); } static double nextDouble() throws IOException { return Double.parseDouble(next()); } }
JAVA
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
playersCount, winNumber = [int(x) for x in input().split(' ')] players = [int(x) for x in input().split(' ')] winsCount = 0 max = 0 for i in range(1, playersCount): if players[max] > players[i]: winsCount += 1 else: winsCount = 1 max = i if winsCount >= winNumber: break print(players[max])
PYTHON3
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
numero_de_pessoas, vitorias_nescessarias = map(int,input().split(" ")) fila = list(map(int,input().split(" "))) jogador_1 = fila.pop(0) jogador_2 = fila.pop(0) jogador_ganhando = -1 pontos = 0 while pontos < vitorias_nescessarias: if jogador_1 > jogador_2: if jogador_1 == jogador_ganhando: pontos += 1 else: pontos = 1 jogador_ganhando = jogador_1 if len(fila) > 0: jogador_2 = fila.pop(0) else: break else: if jogador_2 == jogador_ganhando: pontos += 1 else: pontos = 1 jogador_ganhando = jogador_2 if len(fila) > 0: jogador_1 = fila.pop(0) else: break print(jogador_ganhando)
PYTHON3
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
n, k = map(int, input().split()) arr = list(map(int, input().split())) dic = {} curr = arr.pop(0) dic[curr] = 0 while(curr != n): if arr[0] == n: curr = n break if curr > arr[0]: arr.pop(0) dic[curr] += 1 else: curr = arr.pop(0) dic[curr] = 1 if dic[curr] >= k: break print(curr)
PYTHON3
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
n, k = map(int, input().split()) power = list(map(int, input().split())) results = {i: 0 for i in range(n)} maximal = max(power) max_index = power.index(maximal) for i in range(max_index): if power[i] < power[i + 1]: results[i + 1] += 1 ind = i + 1 while power[ind] < power[i]: results[i] += 1 ind += 1 if results[i] >= k: print(power[i]) exit(0) print(maximal)
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 std::cin; using std::cout; using std::endl; using std::max; using std::min; using std::queue; using std::string; int main() { int n; cin >> n; long long k; cin >> k; int data[501] = { 0, }; for (int i = 1; i <= n; i++) { cin >> data[i]; } if (k > n - 1) { cout << n; return 0; } queue<int> q; for (int i = 1; i <= n; i++) { q.push(data[i]); } int win = 0; int left = q.front(); q.pop(); while (true) { int right = q.front(); q.pop(); if (left > right) { win++; q.push(right); } else { q.push(left); left = right; win = 1; } if (win == k) { cout << left; 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 = [int(i) for i in input().split()] l = [int(i) for i in input().split()] d = l[0] t = 0 for i in range(1,n): if l[i] < d: t = t+1 else: t = 1 d = l[i] if t == k: break print(d)
PYTHON3
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
#include <bits/stdc++.h> using namespace std; int main() { int n, x, y = -1, i = 0; long long k, cnt = 0; cin >> n >> k; while (i < n) { cin >> x; if (y < x) { if (i != 0) cnt = 1; y = x; } else cnt++; if (cnt == k) break; i++; } cout << y; return 0; }
CPP
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
(n, k) = map(int, input().split()) a = list(map(int, input().split())) pobs = 0 curs = a[0] for i in range(1, n): if curs>a[i]: pobs+=1 else: curs = a[i] pobs = 1 if pobs==k: break print(curs)
PYTHON3
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
#include <bits/stdc++.h> int n; unsigned long long k; int a[500]; int main() { scanf("%d%I64u", &n, &k); for (int i = 0; i < n; i++) scanf("%d", a + i); unsigned long long ck = k; int cp = 0; for (int i = 1; i < n && ck > 0; i++) { if (a[cp] > a[i]) { ck--; } else { ck = k - 1; cp = i; } } printf("%d\n", a[cp]); return 0; }
CPP
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
from sys import stdout, stdin def play(line, n,k): player1 = line[0] player2 = 0 wins = 0 i = 1 while i < len(line): player2 = line[i] if player1 > player2: wins +=1 else: player1 = player2 wins = 1 if wins == k: i = len(line) i+=1 return player1 line = stdin.readline().replace('\n','').split(' ') n,k = int(line[0]), int(line[1]) people = [int(x) for x in stdin.readline().replace('\n','').split(' ')] print(play(people, n,k))
PYTHON3
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
#include <bits/stdc++.h> using namespace std; int a[505]; long long cnt[505]; int main() { int n; long long k; cin >> n >> k; int _max = 0; for (int i = 1; i <= n; i++) { cin >> a[i]; _max = max(_max, a[i]); } if (a[1] == _max) return 0 * printf("%d", _max); int m = 1; for (int i = 2; i <= n; i++) { if (a[i] == _max) return 0 * printf("%d", _max); if (a[m] > a[i]) cnt[m]++; if (cnt[m] >= k) return 0 * printf("%d", a[m]); if (a[i] > a[m]) cnt[i]++, m = i; if (cnt[m] >= k) return 0 * printf("%d", a[m]); } 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.InputMismatchException; import java.io.IOException; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author ankur */ 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); thursday solver = new thursday(); solver.solve(1, in, out); out.close(); } static class thursday { public void solve(int testNumber, InputReader in, PrintWriter out) { //out.println(-1); //int n=in.nextInt(); int n = in.nextInt(); long k = in.nextLong(); if (k >= n - 1) { out.print(n); return; } int ar[] = in.nextIntArray(n); //boolean is=false; int ct = 0; int pl = -1; for (int i = 0; i < n; i++) { if (ar[0] > ar[1]) { ct++; int tem = ar[1]; pl = ar[0]; // pl=ar[] //int temp[]=new int[n]; for (int j = 1; j < n - 1; j++) { ar[j] = ar[j + 1]; } ar[n - 1] = tem; } else { pl = ar[1]; ct = 1; int tem = ar[0]; ar[0] = ar[1]; for (int j = 1; j < n - 1; j++) { ar[j] = ar[j + 1]; } ar[n - 1] = tem; } if (ct >= k) { out.print(pl); return; } } out.print(pl); } } static class InputReader { private final InputStream stream; private final byte[] buf = new byte[8192]; private int curChar; private int snumChars; public InputReader(InputStream st) { this.stream = st; } public int read() { //*-*------clare------ if (snumChars == -1) throw new InputMismatchException(); if (curChar >= snumChars) { curChar = 0; try { snumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (snumChars <= 0) return -1; } return buf[curChar++]; } public int nextInt() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public int[] nextIntArray(int n) { int a[] = new int[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } return a; } public boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || 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
n, k = map(int, input().rstrip().split(' ')) players = list(map(int, input().rstrip().split(' '))) if n <= k: k = n - 1 playerWinner = 0 countUntilWin = 0 for i in range(1, n): tempComp = max(players[i], players[playerWinner]) if players[playerWinner] == tempComp: countUntilWin += 1 else: countUntilWin = 1 playerWinner = i if countUntilWin == k: break print(players[playerWinner])
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 = [int(x) for x in input().split()] A.reverse() cc = A.pop() cr = 0 while len(A) > 0 and cc != n and cr < k: nc = A.pop() if nc > cc: cc = nc cr = 1 else: cr += 1 print(cc)
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
# table tennis from collections import deque def TableTennis(pw, power): wins = 0 power = deque(power) p1 = power.popleft() p2 = power[0] if len(power) == 1: if p1 > p2: print(p1) else: print(p2) else: while wins != pw[1]: if pw[1] > 500 and wins > 500 : break if p1 > p2: power.append(power.popleft()) p2 = power[0] wins += 1 else: power.append(p1) p1 = power.popleft() p2 = power[0] wins = 1 print(p1) pw = [int(x) for x in input().split()] power = [int(x) for x in input().split()] TableTennis(pw,power)
PYTHON3
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
#include <bits/stdc++.h> using namespace std; const long long mod = 1e9 + 7; long long a[300010], cnt[300010]; long long n, k, maxx; int main() { ios_base::sync_with_stdio(false); cin >> n >> k; for (int i = 1; i <= n; ++i) { cin >> a[i]; maxx = max(maxx, a[i]); } int pos = 1, cnt = 0; while (a[pos] != maxx) { for (int i = pos + 1; i <= n; ++i) { if (a[pos] > a[i]) { cnt++; if (cnt >= k) { cout << a[pos]; return 0; } } else { cnt = 1; pos = i; break; } } } cout << maxx; return 0; }
CPP
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
from collections import deque n, k = map(int, input().split()) num = list(map(int, input().split())) q = deque() kolvo = 0 fl = False wins = [0] * n for i in range(2, len(num)): q.append(num[i]) battle = [num[0], num[1]] while not k in wins and not fl: x = min(battle) pr = max(battle) pr1 = max(battle) if kolvo > 1: if pr1 != pr: wins[pr - 1] = 0 if wins[pr - 1] > len(num): print(pr) fl = True break battle.pop(battle.index(x)) q.append(x) y = q.popleft() battle.append(y) kolvo += 1 wins[pr - 1] += 1 if not fl: print(pr)
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: print(max(a)) else: i = 1 j = 0 wins = 0 while(j <= n): if a[j] > a[i]: wins+=1 if wins >= k: print(a[j]) break elif a[j] < a[i]: j+=1 i=j wins=1 i = (i+1) % n
PYTHON
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
n,k=map(int,input().split()) a=list(map(int,input().split())) maximum=max(a) winner=count=0 while(count<k): cur=a[0] if cur==maximum: winner=cur break next=a[1] if next==maximum: winner=next break if cur>next: winner=cur a=[cur]+a[2:]+[next] count+=1 else: a=a[1:]+[cur] count=1 print(winner)
PYTHON3
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
pp_num, wins_time = list(map(int, input().split())) pp = list(map(int, input().split())) cnt = 0 while cnt != wins_time: if pp[0] == max(pp): break if pp[0] > pp[1]: pp = [pp[0]] + pp[2:] + [pp[1]] cnt += 1 else: pp = pp[1:] + pp[0:1] cnt = 1 print(pp[0])
PYTHON3
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
import java.util.*; public class Hack_Earth { public static void main(String[] arg){ Scanner scan = new Scanner(System.in); int n=scan.nextInt(); long k=scan.nextLong(); int[] player = new int[n]; int max=-1; for(int i=0;i<n;i++){ player[i] = scan.nextInt(); if(player[i]>max) max = player[i]; } if(k>=n) System.out.println(max); else{ int count=0; int power = player[0]; for(int j=1;count<k;j++){ if(j==n) j=0; if(player[j]>power){ power = player[j]; count=0; } count++; } System.out.println(power); } scan.close(); } }
JAVA
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.math.BigInteger; import java.util.*; import java.util.concurrent.ThreadLocalRandom; import java.util.concurrent.atomic.AtomicLong; import java.util.stream.Collectors; import java.util.stream.IntStream; import java.util.stream.Stream; public class Main2 { static long mod = 1000000007L; static FastScanner scanner; public static void main(String[] args) { scanner = new FastScanner(); int n = scanner.nextInt(); long k = scanner.nextLong(); int[] a = scanner.nextIntArray(n); int max = IntStream.of(a).max().getAsInt(); int winner = Math.max(a[0], a[1]); if (n == 2) { System.out.println(winner); return; } int count = 1; for (int i = 2; i < n; i++) { winner = Math.max(winner, a[i]); if (winner == a[i]) { count = 1; } else { count++; } if (count == k) { System.out.println(winner); return; } } System.out.println(winner); } static class Pos implements Comparable<Pos> { int val, ind; public Pos(int val, int ind) { this.val = val; this.ind = ind; } @Override public int compareTo(Pos o) { return Integer.compare(val, o.val); } } static class Pt{ long x, y; public Pt(long x, long y) { this.x = x; this.y = y; } } public static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String nextToken() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return st.nextToken(); } String nextLine() { try { return br.readLine(); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(); } } int nextInt() { return Integer.parseInt(nextToken()); } long nextLong() { return Long.parseLong(nextToken()); } double nextDouble() { return Double.parseDouble(nextToken()); } int[] nextIntArray(int n) { int[] res = new int[n]; for (int i = 0; i < n; i++) res[i] = nextInt(); return res; } long[] nextLongArray(int n) { long[] res = new long[n]; for (int i = 0; i < n; i++) res[i] = nextLong(); return res; } String[] nextStringArray(int n) { String[] res = new String[n]; for (int i = 0; i < n; i++) res[i] = nextToken(); return res; } } static class PrefixSums { long[] sums; public PrefixSums(long[] sums) { this.sums = sums; } public long sum(int fromInclusive, int toExclusive) { if (fromInclusive > toExclusive) throw new IllegalArgumentException("Wrong value"); return sums[toExclusive] - sums[fromInclusive]; } public static PrefixSums of(int[] ar) { long[] sums = new long[ar.length + 1]; for (int i = 1; i <= ar.length; i++) { sums[i] = sums[i - 1] + ar[i - 1]; } return new PrefixSums(sums); } public static PrefixSums of(long[] ar) { long[] sums = new long[ar.length + 1]; for (int i = 1; i <= ar.length; i++) { sums[i] = sums[i - 1] + ar[i - 1]; } return new PrefixSums(sums); } } static class ADUtils { static void sort(int[] ar) { Random rnd = ThreadLocalRandom.current(); for (int i = ar.length - 1; i > 0; i--) { int index = rnd.nextInt(i + 1); // Simple swap int a = ar[index]; ar[index] = ar[i]; ar[i] = a; } Arrays.sort(ar); } static void sort(long[] ar) { Random rnd = ThreadLocalRandom.current(); for (int i = ar.length - 1; i > 0; i--) { int index = rnd.nextInt(i + 1); // Simple swap long a = ar[index]; ar[index] = ar[i]; ar[i] = a; } Arrays.sort(ar); } } static class MathUtils { static long[] FIRST_PRIMES = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89 , 97 , 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997, 1009, 1013, 1019, 1021, 1031, 1033, 1039, 1049, 1051}; static long[] primes(long to) { long[] result = Arrays.copyOf(FIRST_PRIMES, 10000); int size = FIRST_PRIMES.length; a: for (long t = 1061; t <= to; t++) { for (int i = 0; i < size; i++) { if (t % result[i] == 0) { continue a; } } result[size++] = t; } return Arrays.copyOf(result, size); } static long modpow(long b, long e, long m) { long result = 1; while (e > 0) { if ((e & 1) == 1) { /* multiply in this bit's contribution while using modulus to keep * result small */ result = (result * b) % m; } b = (b * b) % m; e >>= 1; } return result; } static long submod(long x, long y, long m) { return (x - y + m) % m; } } }
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.ArrayList; import java.util.List; import java.util.Scanner; /** * Created by s326lab on 29/11/2017. */ public class Solution1 { public static void main(String[] args) { Scanner r = new Scanner(System.in); int n1 = r.nextInt(); long n2 = r.nextLong(); List<Integer> partial = new ArrayList<>(); int[]array = new int[n1]; partial.add(r.nextInt()); for(int k = 1; k < n1; k++){ int temp1 = r.nextInt(); int temp2 = partial.get(k -1); if (temp2 > temp1){ partial.add(temp2); array[temp2 - 1]++; if(array[temp2 - 1] >= n2){ System.out.println(temp2); return; }//if2 }else{ partial.add(temp1); array[temp1-1]++; if(array[temp1-1]>= n2){ System.out.println(temp1); return; } }//else }//for System.out.println(partial.get(partial.size()-1)); }//main }//class
JAVA
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
#include <bits/stdc++.h> using namespace std; int main() { int n; long long k; cin >> n >> k; queue<int> q; for (int i = 0; i < n; i++) { int x; cin >> x; q.push(x); } int player = q.front(); q.pop(); long long score = 0; while (score < k) { if (player == n) break; int player2 = q.front(); q.pop(); if (player > player2) { score++; q.push(player2); } else { score = 1; q.push(player); player = player2; } } cout << player; return 0; }
CPP
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
n,k = [int(i) for i in input().split()] l = [int(i) for i in input().split(" ")] cur = 0 winner = l[0] if len(l) == 2: print(2) else: while cur < min(k,501): if l[0] > l[1]: win = l[0] winner = win lose = l[1] l = [l[0]] + l[2:] l.append(lose) cur += 1 else: win = l[1] lose = l[0] winner = win l = l[1:] l.append(lose) cur = 1 #print(l) print(winner)
PYTHON3
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
import java.util.*; public class Solution { public static void main(String[] args) throws java.lang.Exception { Scanner in = new Scanner(System.in); int n = in.nextInt(); long k = in.nextLong(); Queue<Integer> file = new LinkedList<>(); int max = 0; int a; for (int i = 0; i < n; i++) { a = in.nextInt(); max = Math.max(max, a); file.add(a); } int winner = file.poll(); int counter = 0; while(winner != max && counter<k){ if (winner>file.peek()){ file.add(file.poll()); counter++; } else { file.add(winner); winner = file.poll(); counter = 1; } } System.out.println(winner); } }
JAVA
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
from collections import deque n,k = map(int, input().rstrip().split()) if n<=k: k = n-1 power = deque(map(int, input().rstrip().split())) winner = power.popleft() winCount = 0 while winCount != k: enemy = power[0] compare = max(winner,enemy) if winner == compare: power.append(enemy) power.popleft() winCount += 1 else: power.append(winner) winner = power.popleft() winCount = 1 print (winner)
PYTHON3
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
#include <bits/stdc++.h> using namespace std; class ListNode { public: ListNode() { power = 0; k = 0; next = NULL; } int power; int k; ListNode* next; }; class Queue { public: Queue() { head = NULL; tail = NULL; } ListNode* head; ListNode* tail; void dequeue() { ListNode* t; if (head != NULL) { t = head; head = head->next; delete t; } } void enqueue(ListNode t) { int power = t.power; int k = t.k; if (head == NULL) { head = new ListNode; head->power = power; head->k = k; tail = head; } else { tail->next = new ListNode; tail->next->power = power; tail->next->k = k; tail = tail->next; } } void enqueue(int power, int k) { if (head == NULL) { head = new ListNode; head->power = power; head->k = k; tail = head; } else { tail->next = new ListNode; tail->next->power = power; tail->next->k = k; tail = tail->next; } } void remove(ListNode* ptr) { if (ptr->next == NULL) return; ListNode* t = ptr->next; ptr->next = t->next; delete t; } }; int main() { long int n; long long int k; int a; int b; int as; int bs; int winner; cin >> n >> k; int* re = new int[n]; Queue player; ListNode t; bool finish = false; int tt; for (int i = 0; i < n; i++) { cin >> tt; player.enqueue(tt, 0); } if (n == 2) if (player.head->power > player.head->next->power) winner = player.head->power; else winner = player.head->next->power; else { ListNode* ptr = player.head; int count = 0; while (!finish) { count++; if (count == n) { winner = player.head->power; break; } if (player.head->power < player.head->next->power) { player.head->next->k += 1; if (player.head->next->k == k) { winner = player.head->next->power; finish = true; break; continue; } t.k = player.head->k; t.power = player.head->power; t.next = NULL; player.dequeue(); player.enqueue(t); } else { t.k = player.head->next->k; t.power = player.head->next->power; t.next = NULL; (player.head->k)++; if ((player.head->k) == k) { finish = true; winner = player.head->power; break; } player.remove(player.head); player.enqueue(t); } } } cout << winner; return 0; }
CPP
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
#include <bits/stdc++.h> int n; long long k; int a[250050]; int cnt[510]; int main() { scanf("%d %I64d", &n, &k); for (int i = 0; i < n; ++i) scanf("%d", a + i); if (k > n * n) printf("%d\n", n); else { int s = 0, t = n - 1; for (;;) { if (a[s] > a[s + 1]) { if ((++cnt[a[s]]) >= k) { printf("%d\n", a[s]); break; } a[++t] = a[s + 1]; a[s + 1] = a[s]; ++s; } else { if ((++cnt[a[s + 1]]) >= k) { printf("%d\n", a[s + 1]); break; } a[++t] = a[s]; ++s; } } } return 0; }
CPP
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
import java.util.Scanner; import java.util.Arrays; public class Solution { public static void main(String [] args) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); long k=sc.nextLong(); long arr[]=new long [n]; for(int i=0;i<n;i++) { arr[i]=sc.nextLong(); } long count=0; int i=0; long player=Math.max(arr[0],arr[1]); if(player==arr[1]){i=1;count=1;} int j=i+1; if((i+1)==n)j=0; if(k>=n-1) { Arrays.sort(arr); System.out.println(arr[n-1]); return; } while(count<k) { if(j==i) { j++; if(j==n)j=0; continue; } if(player<arr[j]) { count=1; player=arr[j]; i=j; j=i+1; if(j==n)j=0; continue; } count++; j++; if(j==n)j=0; } System.out.println(player); } }
JAVA
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
'''input 6 2 6 5 3 1 2 4 ''' n,k = map(int,raw_input().split()) a = map(int,raw_input().split()) if k >= n: ans = max(a) print ans else: loser = [] ans = max(a[0],a[1]) z = 1 for i in range(2,n): tmp = ans ans = max(ans,a[i]) a.append(min(ans,a[i])) if tmp == ans: z += 1 if tmp != ans: z = 1 if z == k: break print ans
PYTHON
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
#include <bits/stdc++.h> using namespace std; const double PI = acos(-1.0), EPS = 1e-9; const int MX = 1 * 1e5 + 10; int N, M, k; int arr[MX]; int win[MX]; int main() { long long k; cin >> N >> k; for (int i = 1; i <= N; i++) cin >> arr[i]; int mx = *max_element(arr + 1, arr + 1 + N); int cur_max = arr[1], ans = 1; for (int i = 1; i <= N; i++) { if (arr[i] == mx) { ans = mx; break; } cur_max = max(cur_max, arr[i]); if (i > 1 && ++win[cur_max] == k) { ans = cur_max; break; } } cout << ans << "\n"; return 0; }
CPP
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
#include <bits/stdc++.h> using namespace std; int n, x, mx; long long k; vector<int> arr; int main() { cin >> n >> k; for (int i = 0; i < n; i++) { cin >> x; arr.push_back(x); } k = min(k, (long long)n); int cnt = 0; while (cnt < k) { if (arr[0] > arr[1]) { int tmp = arr[1]; arr.erase(arr.begin() + 1); arr.push_back(tmp); cnt++; } else { int tmp = arr[0]; arr.erase(arr.begin()); arr.push_back(tmp); cnt = 1; } } cout << arr[0]; return 0; }
CPP
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
n,k = map(int,input().split()) powers = list(map(int,input().split())) max_power = max(powers) if n == 2 or k > n -2 or powers[0] == max_power: print(max_power) else: temp_max = 0 number_of_wins = 0 for a in powers: if a > temp_max: if not temp_max: number_of_wins = 0 else: number_of_wins = 1 temp_max = a else: number_of_wins += 1 if number_of_wins == k: print(temp_max) break if(number_of_wins != k): print(max_power)
PYTHON3
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
#include <bits/stdc++.h> using namespace std; void yesno(bool a) { if (a) { cout << "YES" << '\n'; } else { cout << "NO" << '\n'; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; long long n, k; cin >> n >> k; int arr[n]; int maxi = 0; int i; deque<int> q; for (i = 0; i < n; i++) { cin >> arr[i]; q.push_back(arr[i]); maxi = max(maxi, arr[i]); } if (k > n) { cout << maxi; return 0; } int cnt[n + 1]; memset(cnt, 0, sizeof(cnt)); while (1) { int p1 = q.front(); q.pop_front(); int p2 = q.front(); q.pop_front(); if (p1 > p2) { q.push_front(p1); q.push_back(p2); cnt[p1]++; } else { q.push_front(p2); q.push_back(p1); cnt[p2]++; } if (cnt[p1] == k) { cout << p1 << '\n'; return 0; } else if (cnt[p2] == k) { cout << p2 << '\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
n,k=list(map(int,input().split())) number=list(map(int,input().split())) if k>=n: print(max(number)) else : flag=0 i=0 while i < n-k+1: if i==0 and max(number[i:i+k+1])==number[i] : flag=1 print(number[i]) break elif i!=0 and max(number[i-1:i+k])==number[i]: flag=1 print(number[i]) break else: i+=1 if flag==0: print(max(number))
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
arr = raw_input() n, k = map(int, arr.split(" ")) arr = raw_input() arr = map(int, arr.split(" ")) index = 1 count = 0 helper = [] pivot = arr[0] while index < n: if pivot > arr[index]: count += 1 if count == k: break else: pivot = arr[index] count = 1 index += 1 print(pivot)
PYTHON
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
import java.io.*; import java.math.BigInteger; import java.util.*; public class main { private static InputStream stream; private static byte[] buf = new byte[1024]; private static int curChar; private static int numChars; private static SpaceCharFilter filter; private static PrintWriter pw; private static long mod = 1000000000 + 7; private static void soln() { int n = nextInt(); long k = nextLong(); int[] arr = new int[n]; int max = Integer.MIN_VALUE; for(int i=0;i<n;i++) { arr[i] = nextInt(); max = Math.max(arr[i], max); } if(k>=n-1) pw.println(max); else { int max1 = Math.max(arr[0],arr[1]); int k1 = 1; for(int i=2;i<n && k1<=k; i++) { if(max1>arr[i]) { k1++; }else { k1 = 1; max1 = arr[i]; } if(k1==k) break; } pw.println(max1); } } private static void debug(Object... o) { System.out.println(Arrays.deepToString(o)); } private static class Pair implements Comparable<Pair>{ long val; int br; public Pair(long a,int b) { val=a; br=b; } @Override public int compareTo(Pair arg0) { return (int) (this.val-arg0.val); } } private static long pow(long a, long b, long c) { if (b == 0) return 1; long p = pow(a, b / 2, c); p = (p * p) % c; return (b % 2 == 0) ? p : (a * p) % c; } private static long gcd(long n, long l) { if (l == 0) return n; return gcd(l, n % l); } public static void main(String[] args) throws Exception { // new Thread(null, new Runnable() // { // @Override // public void run() // { // /* // * try { InputReader(new // * FileInputStream("C:\\Users\\hardik\\Desktop\\B-large-practice.in")); } catch // *) (FileNotFoundException e) { // TODO Auto-generated catch block // * e.printStackTrace(); } // */ // InputReader(System.in); // pw = new PrintWriter(System.out); // /* // * try { pw=new PrintWriter(new // * FileOutputStream("C:\\Users\\hardik\\Desktop\\out.txt")); } catch // * (FileNotFoundException e) { // TODO Auto-generated catch block // * e.printStackTrace(); } // */ // soln(); // pw.close(); // } // }, "1", 1 << 26).start(); /* * try { InputReader(new * FileInputStream("C:\\Users\\hardik\\Desktop\\B-large-practice.in")); } catch * (FileNotFoundException e) { // TODO Auto-generated catch block * e.printStackTrace(); } */ InputReader(System.in); pw = new PrintWriter(System.out); /* * try { pw=new PrintWriter(new * FileOutputStream("C:\\Users\\hardik\\Desktop\\out.txt")); } catch * (FileNotFoundException e) { // TODO Auto-generated catch block * e.printStackTrace(); } */ soln(); pw.close(); } public static void InputReader(InputStream stream1) { stream = stream1; } private static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private static boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } private static int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } private static int nextInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } private static long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } private static String nextToken() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } private static 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(); } private static int[] nextIntArray(int n) { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = nextInt(); } return arr; } private static long[] nextLongArray(int n) { long[] arr = new long[n]; for (int i = 0; i < n; i++) { arr[i] = nextLong(); } return arr; } private static void pArray(int[] arr) { for (int i = 0; i < arr.length; i++) { System.out.print(arr[i] + " "); } System.out.println(); return; } private static void pArray(long[] arr) { for (int i = 0; i < arr.length; i++) { System.out.print(arr[i] + " "); } System.out.println(); return; } private static boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return isWhitespace(c); } private static char nextChar() { int c = read(); while (isSpaceChar(c)) c = read(); char c1 = (char) c; while (!isSpaceChar(c)) c = read(); return c1; } private interface SpaceCharFilter { public boolean isSpaceChar(int ch); } }
JAVA
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
#include <bits/stdc++.h> using namespace std; deque<long long> a, b; int main() { ios_base::sync_with_stdio(0); cout.tie(0); cin.tie(0); long long n, k, v, total = 0, ans; cin >> n >> k; cin >> v; a.push_front(v); for (int i = 1; i < n; i++) { cin >> v; b.push_back(v); } if (k > n) k = n - 1; while (true) { long long x = a.front(); a.pop_front(); long long y = b.front(); b.pop_front(); if (x > y) { a.push_front(x); total++; b.push_back(y); if (total >= k) { ans = x; break; } } else { a.push_front(y); total = 1; b.push_back(x); } } cout << ans; }
CPP
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
import java.util.Arrays; import java.util.Scanner; import java.util.UUID; public class water { public static Scanner cin=new Scanner(System.in); public static void main(String[] args) { // TODO Auto-generated method stub while (cin.hasNext()){ int n = cin.nextInt(); long k = cin.nextLong(); int[] a = new int[n]; for(int i=0;i<n;i++){ a[i]=cin.nextInt(); } int tmp = -1; int max=0; if(a[0]<a[1]){ max=a[1]; tmp=1; }else{ max=a[0]; tmp=0; } int count=1; for(int i=2;i<n;i++){ if(max>a[i]){ count++; }else if(count>=k){ break; }else{ max=a[i]; tmp=i; count=1; } } System.out.println(a[tmp]); } } }
JAVA
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
#include <bits/stdc++.h> using namespace std; long long n, m, i, k, j, p, q, y; long long x[100001]; long long r() { long long p = 0, f = 1; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') { f = -1; } c = getchar(); } while (c >= '0' && c <= '9') { p = p * 10 + c - '0'; c = getchar(); } return p * f; } int main() { n = r(); k = r(); for (i = 1; i <= n; i++) x[i] = r(); if (n == 2) { cout << 2; return 0; } y = max(x[1], x[2]); int p = 1; for (i = 3; i <= n; i++) { if (x[i] > y) y = x[i], p = 1; else p++; if (p == k) { cout << y; return 0; } } cout << y << 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.*; import java.io.*; import java.math.BigInteger; public class A2 { public static void main(String []args){ Scanner sc=new Scanner(System.in); int n=sc.nextInt(); long k = sc.nextLong(); int[] arr = new int[n]; ArrayList<Integer> al = new ArrayList<Integer>(); for(int i=0; i<n;i++) al.add(sc.nextInt()); long count=0; int ans=0; boolean flag=false; if(k>=n-1){ Collections.sort(al, Collections.reverseOrder()); System.out.println(al.get(0)); } else{ while(count<k ){ if(al.get(0)>al.get(1)) { count++; int temp= al.remove(1); al.add(temp); } else{ count=1; int temp=al.remove(0); al.add(temp); } if(count==k){ ans=al.get(0);flag=true; break;} } if(flag) 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
#include <bits/stdc++.h> using namespace std; const long long N = 4e5 + 5; const long long mod = 1e18; long long power(long long a, long long b) { if (b == 0) return 1LL; long long d = power(a, b / 2); d *= d; d %= mod; if (b % 2) d *= a; d %= mod; return d; } long long inv(long long a, long long MOD) { return power(a, MOD - 2); } long long bs(long long a[], long long n, long long key) { long long s = 1; long long e = n; while (s <= e) { long long mid = (s + e) / 2; if (a[mid] == key) return mid; else if (a[mid] > key) e = mid - 1; else s = mid + 1; } return -1; } long long a[N]; long long n; long long k; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cout.precision(8); cout << fixed; cin >> n >> k; for (long long i = 1; i <= n; i++) cin >> a[i]; if (k > n - 1) { sort(a + 1, a + n + 1); cout << a[n] << endl; return 0; } int ans = 0; int cnt = 0; for (long long i = 1; i <= n; i++) { if (cnt == k || i == n) { ans = a[i]; break; } if (a[i] > a[i + 1]) { swap(a[i], a[i + 1]); cnt++; } else cnt = 1; } 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
num_de_pessoas, num_de_vitorias = map(int, input().split()) habilidades = list(map(int, input().split())) aux = 0 if(num_de_pessoas <= 2): print(max(habilidades)) else: while aux < num_de_vitorias: if habilidades[0] > habilidades[1]: jogador = habilidades[1] habilidades.remove(jogador) habilidades.append(jogador) aux += 1 if habilidades[0] == num_de_pessoas: aux = num_de_vitorias else: jogador = habilidades[0] habilidades.remove(jogador) habilidades.append(jogador) aux = 1 print(habilidades[0])
PYTHON3
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
from sys import * from math import * n,k=map(int,stdin.readline().split()) a=list(map(int,stdin.readline().split())) if k>=n: print(max(a)) else: j=0 while j<k: if a[0]>a[1]: a.append(a.pop(1)) j+=1 else: a.append(a.pop(0)) j=1 print(a[0])
PYTHON3
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
#include <bits/stdc++.h> using namespace std; using lint = long long; int a[555]; void solve(istream& cin, ostream& cout) { int n; lint k; cin >> n >> k; for (int i = (0); i < int(n); ++i) { cin >> a[i]; } int mx = *max_element(a, a + n); int c = 0; int p = 0; for (int i = (0); i < int(n); ++i) { if (a[i] == mx) { cout << mx; return; } if (a[p] > a[i]) { c++; } else { c = i == 0 ? 0 : 1; p = i; } if (c == k) { cout << a[p]; return; } } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); solve(cin, cout); 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(long,raw_input().split()) powers = map(int,raw_input().split()) if(k >= n-1): print max(powers) else: strongest = powers[0] cWins = 0 for i in range(1,n): if cWins>=k: break if (powers[i] > strongest): strongest = powers[i] cWins=1 else: cWins+=1 print strongest
PYTHON
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
n, k = map(int, input().split()) A = [int(s) for s in input().split()] MAX = max(A) count = 0 player = 0 for i in range(n): if(A[i] == MAX): print(MAX) break elif(A[i] > player): if(i > 0): count = 1 player = A[i] else: count += 1 if(count == k): print(player) 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; typedef long long ll; const ll INF = 1e9; const ll mod = 1e9 + 7; const double pi = acos(-1); const double eps = 1e-6; int main() { deque<int> v; int n; ll k; cin >> n >> k; for (int i = 0; i < n; i++) { int x; cin >> x; v.push_back(x); } deque<int> v2 = v; vector<int> answ; int x; cin >> x; int cnt = 0; int maxx = *max_element(v.begin(), v.end()); map<int, int> win; do { int a = v2.front(); v2.pop_front(); int b = v2.front(); v2.pop_front(); if (a > b) { v2.push_front(a); v2.push_back(b); } else { v2.push_front(b); v2.push_back(a); } if (v2.front() != maxx) { win[v2.front()]++; answ.push_back(v2.front()); cnt++; } } while (v2.front() != maxx); map<int, int> mp; for (auto i : answ) { mp[i]++; if (mp[i] == k) { cout << i; return 0; } } cout << maxx; }
CPP
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
size, req_Wins = [int(x) for x in input().split()] queue = [int(x) for x in input().split()] players = list(queue) undefeated = queue.pop(0) wins = 0 for x in range(size-1): challenger = queue.pop(0) if undefeated > challenger: wins += 1 else: undefeated = challenger wins = 1 if wins == req_Wins: break if queue: print(undefeated) else: print(max(players))
PYTHON3
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
n, k = map(int, input().rstrip().split(' ')) players = list(map(int, input().rstrip().split(' '))) if(n <= k): k = n - 1 winner = 0 count = 0 for i in range(1, n): temp = max(players[i], players[winner]) if(players[winner] == temp): count = count + 1 else: count = 1 winner = i if(count == k): break print(players[winner])
PYTHON3
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
import java.io.*; import java.util.*; public class Main { public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); long n[] = Arrays.stream(in.readLine().split(" ")).mapToLong(Long::parseLong).toArray(), a[] = Arrays.stream(in.readLine().split(" ")).mapToLong(Long::parseLong).toArray(), winner=0; if(a.length==2){ winner = (a[0]>a[1]) ? a[0] : a[1]; } else if(n[0]<n[1]){ for(int i=0;i<n[0];i++){ if(a[i]>winner) winner=a[i]; } } else { int i=0,p1=0,p2=1,nexttoplay=2; while (i < n[1]) { ++i; if (a[p1] > a[p2]) { winner = a[p1]; p2 = nexttoplay; } else { winner = a[p2]; p1=p2; p2 = nexttoplay; i=1; } ++nexttoplay; if(nexttoplay==p1) ++nexttoplay; if(nexttoplay==n[0]) nexttoplay=0; } } System.out.print(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; void solve() { int n; cin >> n; long long k; cin >> k; int arr[n]; for (int i = 0; i < n; i++) { cin >> arr[i]; } if (k > n - 1) { k = n - 1; } int ans = 0; for (int i = 0; i < n; i++) { int v = arr[i]; bool isAns = true; for (int j = 1; j < k; j++) { int idx = (i + j) % n; if (v < arr[idx]) { isAns = false; } } if (i == 0) { int idx = (i + k) % n; if (v < arr[idx]) { isAns = false; } } else { int idx = (i - 1) % n; if (v < arr[idx]) { isAns = false; } } if (isAns) { ans = v; break; } } cout << ans; } int main() { solve(); return 0; }
CPP
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
n,k = (int(x) for x in list(input().split(" "))) players = list(map(int,input().split())) if k > 500: print(max(players)) else: win=0 while win < k: if int(players[0]) > int(players[1]): players.append(players.pop(1)) win +=1 else: win = 0 players.append(players.pop(0)) win +=1 print(players[0])
PYTHON3
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
import java.util.*; public class Main { public static Scanner scanner = new Scanner(System.in); public static int n; public static long winLimit; public static void main(String[] args) { String[] number = scanner.nextLine().split(" "); scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?"); n = Integer.parseInt(number[0]); winLimit = Long.parseLong(number[1]); String[] p = scanner.nextLine().split(" "); scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?"); Queue<Player> players = new LinkedList<>(); for (int i = 0; i < n; i++) { players.add(new Player(Integer.parseInt(p[i]))); } if (n < winLimit) { Player max = players.remove(); players.add(max); for(int i=0; i<players.size()-1; i++) { Player current = players.remove(); if (current.getPower() > max.getPower()) { max = current; } players.add(current); } System.out.println(max.getPower()); } else { Player winner = players.remove(); while (winner.getWins() != winLimit) { if (winner.getPower() > players.peek().getPower()) { winner.incrementWins(); players.add(players.remove()); } else { winner = players.remove(); winner.incrementWins(); players.add(winner); } } System.out.println(winner.getPower()); } } public static class Player { private int power; private int wins = 0; public Player(int power) { this.power = power; } public int getPower() { return power; } public int getWins() { return wins; } public void incrementWins() { wins++; } } }
JAVA
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
import java.io.*; import java.math.BigInteger; import java.util.*; public class l { //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////// ///////// //////// ///////// //////// HHHH HHHH EEEEEEEEEEEEE MMMM MMMM OOOOOO SSSSSSS EEEEEEEEEEEEE ///////// //////// HHHH HHHH EEEEEEEEEEEEE MMMMMM MMMMMM OOO OOO SSSS SSS EEEEEEEEEEEEE ///////// //////// HHHH HHHH EEEEE MMMM MMM MMM MMMM OOO OOO SSSS SSS EEEEE ///////// //////// HHHH HHHH EEEEE MMMM MMMMMM MMMM OOO OOO SSSS EEEEE ///////// //////// HHHH HHHH EEEEE MMMM MMMM OOO OOO SSSSSSS EEEEE ///////// //////// HHHHHHHHHHHHHHHH EEEEEEEEEEE MMMM MMMM OOO OOO SSSSSS EEEEEEEEEEE ///////// //////// HHHHHHHHHHHHHHHH EEEEEEEEEEE MMMM MMMM OOO OOO SSSSSSS EEEEEEEEEEE ///////// //////// HHHH HHHH EEEEE MMMM MMMM OOO OOO SSSS EEEEE ///////// //////// HHHH HHHH EEEEE MMMM MMMM OOO OOO SSS SSSS EEEEE ///////// //////// HHHH HHHH EEEEEEEEEEEEE MMMM MMMM OOO OOO SSS SSSS EEEEEEEEEEEEE ///////// //////// HHHH HHHH EEEEEEEEEEEEE MMMM MMMM OOOOOO SSSSSSS EEEEEEEEEEEEE ///////// //////// ///////// //////// ///////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// static long mod = (int) (1e9 + 7); static int n; static StringBuilder sol; static class pair implements Comparable<pair> { int a,b; public pair( int x,int y) { a=x;b=y; } public int compareTo(pair pair) { if (a!=pair.a)return a-pair.a; else return b-pair.b; } public String toString(){ return a+" "+b; } } static ArrayList<Integer> primes; static int[] isComposite; static void sieve(int N) // O(N log log N) { isComposite = new int[N+1]; isComposite[0] = isComposite[1] = 1; // 0 indicates a prime number primes = new ArrayList<Integer>(); for (int i = 2; i <= N; ++i) //can loop till i*i <= N if primes array is not needed O(N log log sqrt(N)) if (isComposite[i] == 0) //can loop in 2 and odd integers for slightly better performance { primes.add(i); if(1l * i * i <= N) for (int j = i * i; j <= N; j += i) // j = i * 2 will not affect performance too much, may alter in modified sieve isComposite[j] = 1; } } static boolean is; public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); //FileWriter f = new FileWriter("C:\\Users\\Ibrahim\\out.txt"); PrintWriter pw = new PrintWriter(System.out); int n =sc.nextInt(); long k = sc.nextLong(); if (k>n-2){ pw.println(n); } else { int max=sc.nextInt(); int count=0; for (int i =1;i<n;i++){ int x = sc.nextInt(); if (x>max){ count=1; max=x; } else count++; if (count==k)break; } pw.println(max); } pw.flush(); } static int inv; static class UnionFind { int[] p, rank, setSize; int numSets; public UnionFind(int N) { p = new int[numSets = N]; rank = new int[N]; setSize = new int[N]; for (int i = 0; i < N; i++) { p[i] = i; setSize[i] = 1; } } public int findSet(int i) { return p[i] == i ? i : (p[i] = findSet(p[i])); } public boolean isSameSet(int i, int j) { return findSet(i) == findSet(j); } public void unionSet(int i, int j) { if (isSameSet(i, j)) return; numSets--; int x = findSet(i), y = findSet(j); if(rank[x] > rank[y]) { p[y] = x; setSize[x] += setSize[y]; } else { p[x] = y; setSize[y] += setSize[x]; if(rank[x] == rank[y]) rank[y]++; } } public int numDisjointSets() { return numSets; } public int sizeOfSet(int i) { return setSize[findSet(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.io.*; public class codeforces{ static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader() { reader = new BufferedReader(new InputStreamReader(System.in)); 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()); } public double nextDouble() { return Double.parseDouble(next()); } public String nextLine() { String s = null; try { s = reader.readLine(); } catch (IOException e) { throw new RuntimeException(e); } return s; } public String nextParagraph() { String line = null; String ans = ""; try { while ((line = reader.readLine()) != null) { ans += line; } } catch (IOException e) { throw new RuntimeException(e); } return ans; } } //static int mod = 1000000007; static void printArray(int ar[]){ for(int x:ar) System.out.print(x+" "); } static void printMatrix(int ar[][],int n){ for(int i=0;i<n;i++){ for(int j=i;j<n;j++){ System.out.print(ar[i][j]+" "); } System.out.println(); } } static class Data{ int t; int l; int r; public Data(int t,int l,int r){ this.t=t; this.l=l; this.r=r; } } static int gcd(int a, int b){ if (a == 0) return b; return gcd(b%a, a); } public static void main(String args[]){ InputReader sc=new InputReader(); PrintWriter pw=new PrintWriter(System.out); int n=sc.nextInt(); long k=sc.nextLong(); int ar[]=new int[n]; int i=0; int max = Integer.MIN_VALUE; for(i=0;i<n;i++){ ar[i]=sc.nextInt(); max = Math.max(max,ar[i]); } long count = 1; int player = Math.max(ar[0],ar[1]); for(i=2;i<n;i++){ if(player == max){ break; }else if(count == k){ break; } if(ar[i]>player){ count = 1; player = ar[i]; }else count++; } pw.print(player); pw.close(); } }
JAVA
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
n,k = map(int,input().split()) a = list(map(int,input().split())) b = [0]*n if (a.index(max(a))<k) or (a.index(max(a))==0): print(max(a)) else: while max(b)<k: if a[0]>a[1]: b[0] = b[0]+1 x=a[1] y=b[1] a.remove(a[1]) b.remove(b[1]) a.append(x) b.append(y) else: b[1] = b[1]+1 x=a[0] y=b[0] a.remove(a[0]) b.remove(b[0]) a.append(x) b.append(y) print(a[b.index(max(b))])
PYTHON3
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
import java.util.*; import java.io.*; import java.math.*; public class Main { static Scanner input = new Scanner(new BufferedReader(new InputStreamReader(System.in))); public static void main(String[] args) { int n = input.nextInt(); long k = input.nextLong(); int maxPow = input.nextInt(), count = 0; for (int i = 1; i < n; i++) { int curPow = input.nextInt(); if (curPow > maxPow) { maxPow = curPow; count = 1; } else { count++; } if (count == k) { break; } } System.out.println(maxPow); } }
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 a[1005]; int kol[1005]; int main() { int n; cin >> n; long long k; cin >> k; int i0; for (int i = 1; i <= n; i++) { cin >> a[i]; a[n + i] = a[i]; } for (int i = 1; i <= 2 * n; i++) { for (int j = i + 1; j <= 2 * n; j++) if (a[i] > a[j]) kol[i]++; else break; if ((kol[i] >= k and i == 1) or (kol[i] >= k - 1 and i > 1)) { cout << a[i]; return 0; } if (kol[i] == n - 1) { i0 = i; } } if (i0 > n) i0 -= n; cout << a[i0]; 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 int n, k; cin >> n >> k; long long int pow[n]; for (long long int i = 1; i <= n; i++) cin >> pow[i]; long long int MAX = pow[1], num = 0; for (long long int i = 2; i <= n && num < k; i++) { if (pow[i] < MAX) num++; else { MAX = pow[i]; num = 1; } } cout << MAX << 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.Collections; import java.util.LinkedList; import java.util.Queue; import java.util.Scanner; public class TableTennis { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); long wins = in.nextLong(); Queue<Integer> q = new LinkedList<Integer>(); for(int i=0;i<n;i++) q.add(in.nextInt()); int player1,player2; int winner1=0,winner2=0; if(wins>n){ System.out.println(Collections.max(q)); System.exit(0); } player1=q.poll(); player2=q.poll(); while(winner1<wins&&winner2<wins){ if(player1>=player2){ q.add(player2); winner1++; winner2=0; player2=q.poll(); } else{ q.add(player1); winner2++; winner1=0; player1=q.poll(); } } if(winner1!=0) System.out.println(player1); else System.out.println(player2); } }
JAVA
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
//package codeforces.round443.div2; import java.io.*; import java.util.*; public class TableTennis { static class ProblemSolver { void solveTheProblem(InputReader in, PrintWriter out) { int n = in.nextInt(); long k = Long.parseLong(in.next()); if (k > n - 1) { k = n - 1; } LinkedList<Integer> q = new LinkedList<>(); Map<Integer, Integer> map = new HashMap<>(); for (int i = 0; i < n; i++) { int p = in.nextInt(); q.add(p); map.put(p, 0); } while (true) { int p1 = q.removeFirst(); int p2 = q.removeFirst(); if (p1 > p2) { map.put(p1, map.get(p1) + 1); q.addFirst(p1); q.addLast(p2); if(map.get(p1) >= k) { out.println(p1); break; } } else { map.put(p2, map.get(p2) + 1); q.addFirst(p2); q.addLast(p1); if(map.get(p2) >= k) { out.println(p2); break; } } } } void swap(int[] nums, int i, int j) { int tmp = nums[i]; nums[i] = nums[j]; nums[j] = tmp; } } // Default template for all the codes static class InputReader { BufferedReader reader; StringTokenizer tokenizer; InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } int nextInt() { return Integer.parseInt(next()); } } //Main method for all the codes public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); ProblemSolver problemSolver = new ProblemSolver(); problemSolver.solveTheProblem(in, out); out.close(); } }
JAVA
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
n,k=map(int,input().split()) a=list(map(int,input().split())) w=0 temp=a[0] m=max(a) while w<k: if temp<a[1]: a.remove(temp) a.append(temp) temp=a[0] w=1 else: w+=1 temp2=a[1] a.remove(temp2) a.append(temp2) if a[0]==m: temp=m break print(temp)
PYTHON3
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
n, k = map(int, input().split()) v = list(map(int, input().split())) count = 0 previous = v[0] for i in range(1, n): if previous>v[i]: count+=1 else: previous = v[i] count = 1 if count==k: break print(previous)
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 = input().strip().split() n = int(n) k = int(k) day =0 a = list(map(int, input().strip().split())) score = 0 win = 0 if k >=n: print(n) else: for i in range(2 * n): if i !=0: if a[win] > a[i]: a.append(i) score +=1 else: a.append(win) win = i score =1 if score == k: print (a[win]) break
PYTHON3
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
import java.util.*; public class Solution { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); long k = in.nextLong(); LinkedList<Integer> q = new LinkedList<>(); for(int i = 0; i < n; i++) { q.add(in.nextInt()); } for(int i = 0; i < n; i++) { long kk = i == 0 ? 0 : 1; int player = q.get(0); int last = 1; for(; last < n; ++last) { if(player > q.get(last)) { kk++; if(last == n - 1) { kk = k; break; } } else { break; } } if(kk >= k) { System.out.println(player); break; } else { List<Integer> eliminated = q.subList(0, last); List<Integer> copy = new LinkedList<>(eliminated); eliminated.clear(); Collections.reverse(copy); q.addAll(copy); } } } }
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())) win = 0 local = 0 for i in range(1, n): if a[local] > a[i]: win += 1 else: local = i win = 1 if win == k: break print(a[local])
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())) for i in range(n): if a[1]>a[0]: a[0],a[1]=a[1],a[0] if a[0]==max(a): print(a[0]);break else: try: if all(a[0]>a[j] for j in range(1,k+1)) : print(a[0]);break except:'blah!' x=a[0] del a[0] a.append(x)
PYTHON3
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
n,k=map(int,input().split()) l=list(map(int,input().split())) if k<=len(l): pos = l.index(max(l)) h= l[:pos] m=0 i=0 j=1 while i<pos and j<pos and m<k: if h[i]>h[j]: m+=1 j+=1 else : i=j j+=1 m=1 if m>=k: print(h[i]) else : print(max(l)) else : print(max(l))
PYTHON3
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
import java.util.LinkedList; import java.util.Optional; import java.util.Queue; import java.util.Scanner; public class TennisTable { public static long MaxQueueElement(Queue<Long> queue){ Optional<Long> max = queue.stream() .max(Comparable::compareTo); // if(max.isPresent()){ return max.get(); // } } public static void main(String[] args) { Queue<Long> q = new LinkedList<Long>(); boolean flag = true; long first, second; Scanner sc = new Scanner(System.in); int n = sc.nextInt(); long wins = sc.nextLong(); long [] a = new long[n+1]; for (int i = 0; i < n; i++) { q.add(sc.nextLong()); } long maxElement = MaxQueueElement(q); first = q.remove(); second = q.remove(); while (flag){ if (first > second){ a[(int) first]++; q.add(second); if (a[(int)first] == wins || q.size() == 1 || first==maxElement){ System.out.println(first); break; } }else { a[(int)second]++; q.add(first); if (a[(int)second] == wins || q.size() == 1|| second==maxElement){ System.out.println(second); break; } first = second; } second = q.remove(); } } }
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, input().split()) a = list(map(int, input().split())) mx = max(a) num = a[0] if a[0] == mx: print(a[0]) sys.exit() count = 0 for i in range(1, n): if a[i] < num: count += 1 elif a[i] == mx: print(mx) sys.exit() else: count = 0 num = a[i] count = 1 if count == k: print(num) sys.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,input().split()) l=list(map(int,input().split())) if l[0]==max(l[0:min(k+1,len(l))]): print(l[0]) else : for i in range (1,n): if l[i]==max(l[i:min(i+k,len(l))]): print (l[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
from collections import deque def ping_pong(wins, numPlayers): pongers = deque(numPlayers) iterateCounter = 0 while(iterateCounter < len(pongers)): winsCounter = 0 fightsCounter = 0 pongerFought = [] while(True): fightsCounter += 1 if(pongers[0] > pongers[1]): if(pongers[0] > pongers[-1] and fightsCounter == 1): winsCounter += 2 else: winsCounter += 1 pongerFought.append(pongers[1]) if(winsCounter == wins or (len(pongerFought) + 1 == len(pongers) and max(pongers) == pongers[0])): print(pongers[0]) return else: temp = pongers[1] pongers.remove(temp) pongers.append(temp) else: val = pongers.popleft() pongers.append(val) break iterateCounter += 1 first_line = [int(a) for a in input().split(' ')] second_line = [int(a) for a in input().split(' ')] ping_pong(first_line[1], second_line)
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
#!/usr/bin/python from math import ceil n, k = list(map(int, input().split())) p = list(map(int, input().split())) def sim(): global p a, p = p[0], p[1:] c = 0 while 1: b, p = p[0], p[1:] while a > b: c += 1 if c >= k: return a p.append(b) b, p = p[0], p[1:] c = 1 p.append(a) a = b if k >= n - 1: print(max(p)) else: print(sim())
PYTHON3
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
n, k = map(int,input().split()) A = list(map(int,input().split())) C = [0] * n t = 0 p = 1 if k <= n: while True: if A[t] > A[p]: A.append(A[p]) if C[A[t]-1] == k-1: print(A[t]) break else: C[A[t]-1] += 1 else: A.append(A[t]) t = p if C[A[p]-1] == k-1: print(A[p]) break else: C[A[p]-1] += 1 p += 1 else: A.sort() print(A[-1])
PYTHON3
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
#include <bits/stdc++.h> using namespace std; namespace Tzh { const int maxn = 1010; long long n, k, g[maxn]; void work() { cin >> n >> k; for (int i = 1; i <= n; i++) cin >> g[i]; if (k >= n) { cout << *max_element(g + 1, g + n + 1); return; } long long winner = 1, num = 0; for (int i = 2; i <= n; i++) { if (g[winner] > g[i]) num++; else winner = i, num = 1; if (num == k) { cout << g[winner]; return; } } cout << g[winner]; return; return; } } // namespace Tzh int main() { ios::sync_with_stdio(false); Tzh::work(); 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.math.BigInteger; import java.util.LinkedList; import java.util.Queue; import java.util.Scanner; import java.util.concurrent.LinkedBlockingDeque; public class Main { public static int maxn = 100005; public static Scanner in = new Scanner(System.in); public static void main(String[] args) { Queue<Integer> q = new LinkedList<Integer>(); int n; long k; int a[] = new int[maxn]; n = in.nextInt(); k = in.nextLong(); for(int i=1;i<=n;i++) { int tmp; a[i] = in.nextInt(); q.add(a[i]); } int win = 0; if(k >= n) { win = a[1]; for(int i=2;i<=n;i++) { if(win < a[i]) win = a[i]; } } else if(k < n) { int time = 0; win = q.remove(); while(true) { if(win >= q.element()) { time++; q.add(q.remove()); } else { q.add(win); win = q.element(); q.remove(); time = 1; } if(time == k) { break; } } } System.out.println(win); } }
JAVA
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
// package mainpackage; import java.io.*; import java.util.*; public class Main { public static void main(String... args) throws Exception { out = new PrintWriter(System.out); new Main().solve(); out.close(); } private static PrintWriter out; private BufferedReader reader; private StringTokenizer st; private int n, k; private int[] data; private void solve() throws Exception { reader = new BufferedReader(new InputStreamReader(System.in)); st = new StringTokenizer(reader.readLine()); n = Integer.parseInt(st.nextToken()); long tmpk = Long.parseLong(st.nextToken()); if (tmpk >= n - 1) { out.println(n); reader.close(); return; } k = (int)tmpk; data = new int[n]; st = new StringTokenizer(reader.readLine()); for (int i = 0; i < n; ++i) { data[i] = Integer.parseInt(st.nextToken()); } reader.close(); int t = -1; for (int i = 0; i < n; ++i) { if (data[i] == n) { t = i; break; } } if (k >= t) { out.println(n); return; } int indx = 0; int len = 0; for (int i = 1; i <= t; ++i) { if (data[indx] < data[i]) { indx = i; len = 1; continue; } ++len; if (len == k) { out.println(data[indx]); return; } } out.println(n); } }
JAVA
879_B. Table Tennis
n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner. For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. Input The first line contains two integers: n and k (2 ≀ n ≀ 500, 2 ≀ k ≀ 1012) β€” the number of people and the number of wins. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
2
8
from collections import deque n, k = map(int, input().split()) guys = [int(x) for x in input().split()] wins = dict() queue = deque() max_power = max(guys) for guy in guys: wins[guy] = 0 queue.append(guy) cur = guys[0] queue.popleft() while cur != max_power and wins[cur] != k: enemy = queue.popleft() if enemy > cur: cur, enemy = enemy, cur wins[cur] += 1 queue.append(enemy) print(cur)
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.OutputStream; import java.io.PrintWriter; import java.io.BufferedWriter; import java.io.Writer; import java.io.OutputStreamWriter; import java.util.InputMismatchException; import java.io.IOException; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author ouheguaughwugfyuwgfiuqwgr */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); OutputWriter out = new OutputWriter(outputStream); TaskB solver = new TaskB(); solver.solve(1, in, out); out.close(); } static class TaskB { public void solve(int testNumber, InputReader scan, OutputWriter out) { int n = scan.nextInt(); long k = scan.nextLong(); int arr[] = new int[n + 1]; for (int i = 1; i <= n; i++) { arr[i] = scan.nextInt(); } if (k >= n) { out.println(n); return; } else { int a = arr[1]; int z = 0; int i = 2; int idx = 1; if (a == n) { out.println(n); return; } for (; i <= n; i++) { if (arr[i] < a) { z++; if (z == k) break; } else { z = 1; a = arr[i]; idx = i; } } if (i != n) { out.println(a); return; } out.println(n); } } } static class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void close() { writer.close(); } public void println(int i) { writer.println(i); } } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private InputReader.SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) { throw new InputMismatchException(); } if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) { return -1; } } return buf[curChar++]; } public int nextInt() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public boolean isSpaceChar(int c) { if (filter != null) { return filter.isSpaceChar(c); } return isWhitespace(c); } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }
JAVA