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())
powers = list(map(int, input().split()))[:n]
if k >= n - 1:
print(max(powers))
else:
wins = 0
index = 0
while True:
A = index % n
B = (index + 1) % n
winner_power = -1
if powers[A] < powers[B]:
winner_power = powers[B]
wins = 1
else:
winner_power = powers[A]
powers[A], powers[B] = powers[B], powers[A]
wins += 1
index = B
if wins >= k:
print(winner_power)
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.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
import java.util.Scanner;
public class test2 {
public static void main(String[] args) {
Scanner read=new Scanner(System.in);
int n=read.nextInt();
long pow=read.nextLong();
LinkedList<Integer> arr=new LinkedList<Integer>();
//int max=0;
for(int i=0;i<n;i++){
int x=read.nextInt();
arr.add(x);
}
if(pow>=n || pow==n-1){
Collections.sort(arr);System.out.println(arr.get(n-1));return;
}
int score=0;
int i=0;
//System.out.println("bb");
int k=i+1;
while(true){
//System.out.println("ee");
if(arr.get(i)>arr.get(k)){
//System.out.println("ee");
score++;
if(score==pow){
System.out.println(arr.get(i));return;
}
arr.add(arr.get(k));
arr.remove(k);
}
else{score=1;
arr.add(arr.get(i));
arr.remove(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 | n, k = map(int, input().split())
if k > n:
k = n
arr = list(map(int, input().split()))
c = 0
cur = -1
while c < k:
if arr[0] > arr[1]:
if cur == arr[0]:
c += 1
else:
cur = arr[0]
c = 1
arr = [arr[0]] + arr[2:] + [arr[1]]
else:
if cur == arr[1]:
c += 1
else:
cur = arr[1]
c = 1
arr = arr[1:] + [arr[0]]
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 | n, k = map(int,input().split())
a = list(map(int,input().split()))
if(k > n):
print(n)
else:
cur = a[0]
hod = -1
ans = n
for x in a:
if( x > cur):
if(hod >= k):
ans = min(ans, cur)
cur = x
hod = 1
else:
hod+=1
print (ans)
| PYTHON3 |
879_B. Table Tennis | n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner.
For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner.
Input
The first line contains two integers: n and k (2 β€ n β€ 500, 2 β€ k β€ 1012) β the number of people and the number of wins.
The second line contains n integers a1, a2, ..., an (1 β€ ai β€ n) β powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct.
Output
Output a single integer β power of the winner.
Examples
Input
2 2
1 2
Output
2
Input
4 2
3 1 2 4
Output
3
Input
6 2
6 5 3 1 2 4
Output
6
Input
2 10000000000
2 1
Output
2
Note
Games in the second sample:
3 plays with 1. 3 wins. 1 goes to the end of the line.
3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner. | 2 | 8 | import java.io.*;
import java.util.*;
import java.util.Arrays;
public class CF5 {
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
double k = scan.nextLong();
List<Double> arr = new ArrayList<Double>();
for (int i = 0 ; i < n ; i ++)
{
arr.add(scan.nextDouble());
}
double score = 0;
while (score < k)
{
if(arr.size()==1)
{
System.out.println(arr.get(0).intValue());
break;
}
else if (arr.get(0)>arr.get(1))
{
arr.remove(1);
score++;
if (score==k)
{
System.out.println(arr.get(0).intValue());
}
}
else
{
arr.remove(0);
score = 1;
if (score==k)
{
System.out.println(arr.get(1).intValue());
}
}
}
}
}
| JAVA |
879_B. Table Tennis | n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner.
For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner.
Input
The first line contains two integers: n and k (2 β€ n β€ 500, 2 β€ k β€ 1012) β the number of people and the number of wins.
The second line contains n integers a1, a2, ..., an (1 β€ ai β€ n) β powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct.
Output
Output a single integer β power of the winner.
Examples
Input
2 2
1 2
Output
2
Input
4 2
3 1 2 4
Output
3
Input
6 2
6 5 3 1 2 4
Output
6
Input
2 10000000000
2 1
Output
2
Note
Games in the second sample:
3 plays with 1. 3 wins. 1 goes to the end of the line.
3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner. | 2 | 8 | import java.io.*;
import java.util.*;
public class Main
{
public static void main(String agrs[])
{
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
long k = scan.nextLong();
int max = Integer.MIN_VALUE;
int arr[] = new int[n];
Queue<Integer> queue = new LinkedList<Integer>();
for(int i=0;i<n;i++)
{
arr[i] = scan.nextInt();
max = Math.max(max, arr[i]);
queue.add(arr[i]);
}
if(k>=n)
System.out.println(max);
else
{
int count=0;
while(true)
{
int element = queue.poll();
while(true)
{
int x = queue.peek();
if(x<element)
{
count++;
queue.add(queue.poll());
}
if(element<x)
{
queue.add(element);
count=1;
break;
}
if(count==k)
{
System.out.println(element);
return ;
}
}
}
}
}
} | JAVA |
879_B. Table Tennis | n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner.
For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner.
Input
The first line contains two integers: n and k (2 β€ n β€ 500, 2 β€ k β€ 1012) β the number of people and the number of wins.
The second line contains n integers a1, a2, ..., an (1 β€ ai β€ n) β powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct.
Output
Output a single integer β power of the winner.
Examples
Input
2 2
1 2
Output
2
Input
4 2
3 1 2 4
Output
3
Input
6 2
6 5 3 1 2 4
Output
6
Input
2 10000000000
2 1
Output
2
Note
Games in the second sample:
3 plays with 1. 3 wins. 1 goes to the end of the line.
3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner. | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int main() {
int n, i, j, vitorias[550];
long long int powers[550], maior = -1, k;
deque<int> jogo;
cin >> n >> k;
for (i = 1; i <= n; i++)
cin >> powers[i], jogo.push_back(i), vitorias[i] = 0,
maior = max(maior, powers[i]);
if (k < n - 1) {
while (true) {
int p1, p2;
p1 = jogo.front();
jogo.pop_front();
p2 = jogo.front();
jogo.pop_front();
if (powers[p1] > powers[p2]) {
vitorias[p1]++;
if (vitorias[p1] >= k) {
printf("%lld\n", powers[p1]);
return 0;
}
jogo.push_front(p1);
jogo.push_back(p2);
vitorias[p2] = 0;
} else {
vitorias[p2]++;
if (vitorias[p2] >= k) {
printf("%lld\n", powers[p2]);
return 0;
}
jogo.push_front(p2);
jogo.push_back(p1);
vitorias[p1] = 0;
}
}
} else {
printf("%lld\n", maior);
}
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.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;
public class Main {
PrintWriter pw;
Scanner sc;
Main(boolean stdio) throws FileNotFoundException {
if (stdio) {
pw = new PrintWriter(System.out);
sc = new Scanner(System.in);
} else {
pw = new PrintWriter("input.txt");
sc = new Scanner("output.txt");
}
}
void dispose() {
pw.flush();
pw.close();
sc.close();
}
void run() {
long n = sc.nextLong();
long k = sc.nextLong();
if (k >= n - 1) {
pw.println(n);
return;
}
long winner = sc.nextLong();
long wins = 0;
for (int i = 1; i < n && wins < k; i++) {
long second = sc.nextLong();
if (second > winner) {
winner = second;
wins = 1;
} else {
wins++;
}
}
pw.println(winner);
return;
}
public static void main(String[] args) throws FileNotFoundException {
Main main = new Main(true);
main.run();
main.dispose();
}
}
| JAVA |
879_B. Table Tennis | n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner.
For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner.
Input
The first line contains two integers: n and k (2 β€ n β€ 500, 2 β€ k β€ 1012) β the number of people and the number of wins.
The second line contains n integers a1, a2, ..., an (1 β€ ai β€ n) β powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct.
Output
Output a single integer β power of the winner.
Examples
Input
2 2
1 2
Output
2
Input
4 2
3 1 2 4
Output
3
Input
6 2
6 5 3 1 2 4
Output
6
Input
2 10000000000
2 1
Output
2
Note
Games in the second sample:
3 plays with 1. 3 wins. 1 goes to the end of the line.
3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner. | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int n, can;
long long a[10000], sol, k;
int main() {
scanf("%d %lld", &n, &k);
for (int i = 1; i <= n; i++) scanf("%lld", &a[i]);
sol = a[1];
for (int i = 2; i <= n; i++) {
if (sol > a[i])
can++;
else {
sol = a[i];
can = 1;
}
if (can == k) {
printf("%lld\n", sol);
return 0;
}
}
printf("%lld\n", sol);
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.math.*;
import java.io.*;
public class Main {
public static void main(String[] args) {
Scanner cin=new Scanner(System.in);
int n=cin.nextInt();
long k=cin.nextLong();
int[] a=new int [550];
for(int i=0;i<n;i++) a[i]=cin.nextInt();
long cnt=0;
for(int t=0;t<n;t++) {
if(a[0]>a[1]) {
int tmp=a[1];
for(int i=1;i<n-1;i++) a[i]=a[i+1];
a[n-1]=tmp;
cnt++;
}
else {
cnt=1;
int tmp=a[0];
for(int i=0;i<n-1;i++) a[i]=a[i+1];
a[n-1]=tmp;
}
if(cnt==k) {
System.out.println(a[0]);
return ;
}
}
System.out.println(n);
return ;
}
static int gcd(int a,int b) {
if(b==0) return a;
return gcd(b,a%b);
}
}
| JAVA |
879_B. Table Tennis | n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner.
For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner.
Input
The first line contains two integers: n and k (2 β€ n β€ 500, 2 β€ k β€ 1012) β the number of people and the number of wins.
The second line contains n integers a1, a2, ..., an (1 β€ ai β€ n) β powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct.
Output
Output a single integer β power of the winner.
Examples
Input
2 2
1 2
Output
2
Input
4 2
3 1 2 4
Output
3
Input
6 2
6 5 3 1 2 4
Output
6
Input
2 10000000000
2 1
Output
2
Note
Games in the second sample:
3 plays with 1. 3 wins. 1 goes to the end of the line.
3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner. | 2 | 8 | n,k = map(int,raw_input().strip().split())
arr = map(int,raw_input().strip().split())
if k>n-1 or n==2:
print max(arr)
else:
atplay = arr[0]
arr = arr[1:]
wins = 0
ind = 0
while wins<k:
if arr[ind%(n-1)]>atplay:
atplay,arr = arr[ind%(n-1)],arr[ind%(n-1)+1:]+arr[:ind%(n-1)]+[atplay]
wins = 1
ind = 0
else:
ind += 1
wins += 1
#print arr,atplay,wins,ind
print atplay | 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;
void solve() {
long long n, k;
cin >> n >> k;
vector<pair<int, int>> a(n);
int mx = 0;
for (int i = 0; i < n; ++i) {
cin >> a[i].first;
mx = max(a[i].first, mx);
a[i].second = i;
}
vector<pair<int, int>> b = a;
vector<int> cnt(n);
int ans = -1;
for (int i = 0; i < n - 1; ++i) {
if (a[0] < a[1]) {
cnt[a[1].second]++;
a.push_back(a[0]);
a.erase(a.begin());
} else {
cnt[a[0].second]++;
a.push_back(a[1]);
a.erase(a.begin() + 1);
}
for (int j = 0; j < n; ++j) {
if (cnt[j] == k) {
ans = b[j].first;
cout << ans << "\n";
return;
}
}
};
cout << (ans == -1 ? mx : ans) << "\n";
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
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 | #include <bits/stdc++.h>
using namespace std;
long long n, k, a[507], num[507];
int solve() {
deque<int> q;
for (int i = 1; i <= n; i++) q.push_back(a[i]);
while (1) {
int a = q.front();
q.pop_front();
int b = q.front();
q.pop_front();
if (b > a) swap(a, b);
num[a]++;
if (num[a] >= k) return a;
q.push_front(a);
q.push_back(b);
}
}
int main() {
cin >> n >> k;
for (int i = 1; i <= n; i++) cin >> a[i];
if (k >= n - 1) {
sort(a + 1, a + 1 + n);
cout << a[n] << endl;
} else {
cout << solve() << 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 sys
n,k=map(int,input().split())
a=list(map(int,input().split()))
cnt=0
m=max(a)
tmp=a[0]
for i in range(1,len(a)):
if tmp>a[i]:cnt+=1
else:
cnt=1
tmp=a[i]
if cnt==k:
print(tmp)
sys.exit()
if tmp==m:
print(m)
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 | #include <bits/stdc++.h>
using namespace std;
const int N = 300005;
const int mod = (long long)1e9 + 7;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long k;
int n;
cin >> n >> k;
int a[n];
for (int i = 0; i < n; i++) cin >> a[i];
int count = 0;
for (int i = 0; i < n; i++) {
for (int j = (i + 1) % n;; j = (j + 1) % n) {
if (count >= k || j == i) {
cout << a[i] << "\n";
return 0;
}
if (a[j] < a[i])
count++;
else
break;
if (count >= k || j == i) {
cout << a[i] << "\n";
return 0;
}
}
count = 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 | p, n = map(int, input().split())
strength = list(map(int, input().split()))
if n >= p:
print(max(strength))
else:
curmax = strength[0]; curn = 0;
for i in range(1,p):
if strength[i] > curmax:
curmax = strength[i]
curn = 1
else:
curn += 1
if curn >= n:
print(curmax)
curn = -1
break
if curn >= 0:
print(curmax) | PYTHON3 |
879_B. Table Tennis | n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner.
For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner.
Input
The first line contains two integers: n and k (2 β€ n β€ 500, 2 β€ k β€ 1012) β the number of people and the number of wins.
The second line contains n integers a1, a2, ..., an (1 β€ ai β€ n) β powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct.
Output
Output a single integer β power of the winner.
Examples
Input
2 2
1 2
Output
2
Input
4 2
3 1 2 4
Output
3
Input
6 2
6 5 3 1 2 4
Output
6
Input
2 10000000000
2 1
Output
2
Note
Games in the second sample:
3 plays with 1. 3 wins. 1 goes to the end of the line.
3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner. | 2 | 8 | import java.util.Scanner;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Queue;
import java.util.LinkedList;
public class Problems {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Problems x = new Problems();
counter c = x.new counter();
int n = sc.nextInt(),temp,p1,p2;
long k = sc.nextLong();
int[] arr = new int[n];
Queue<Integer> q = new LinkedList<Integer>();
for (int i = 0; i < n; i++) {
temp = sc.nextInt();
arr[i] = temp;
q.add(temp);
}
if (k >= n)
System.out.println(max(arr));
else {
p1 = q.poll(); p2 = q.poll();
if (p1 > p2) {
q.add(p2);
c.value = p1;
}
else {
q.add(p1);
c.value = p2;
}
c.current = 1;
while (c.current < k) {
p1 = q.poll();
if (c.value > p1) {
q.add(p1);
c.current++;
}
else {
q.add(c.value);
c.value = p1;
c.current = 1;
}
}
System.out.println(c.value);
}
}
public static int max(int[] array) {
int max = 0, length = array.length;
for (int j = 0; j < length; j++) {
if (array[j] > max)
max = array[j];
}
return max;
}
public class counter{
int current;
int 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.InputStreamReader;
import java.util.*;
import java.util.Map.*;
public class j {
public static void main(String[] args) throws Exception{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] str = br.readLine().split(" ");
int n = Integer.parseInt(str[0]);
long k = Long.parseLong(str[1]);
int[] a = new int[501];
str = br.readLine().split(" ");
if(k>(n-1)) {
int max = 0;
for(int i=0;i<n;i++) {
int tmp = Integer.parseInt(str[i]);
if(tmp>max) max = tmp;
}
System.out.println(max);
} else {
int[] b = new int[n];
for(int i=0;i<n;i++) b[i] = Integer.parseInt(str[i]);
int winner = b[0]>b[1]?b[0]:b[1];
a[winner]++;
if(k==1) {System.out.println(winner);return ;}
int max = winner;
//System.out.println("Winner : "+winner);
for(int i=2;i<n;i++) {
winner = winner>b[i]?winner:b[i];
if(b[i]>max) {
max = b[i];
}
a[winner]++;
//System.out.println("Winn"+winner);
if(a[winner]==k) {
System.out.println(winner);return ;
}
}
System.out.println(max);
}
}
} | JAVA |
879_B. Table Tennis | n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner.
For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner.
Input
The first line contains two integers: n and k (2 β€ n β€ 500, 2 β€ k β€ 1012) β the number of people and the number of wins.
The second line contains n integers a1, a2, ..., an (1 β€ ai β€ n) β powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct.
Output
Output a single integer β power of the winner.
Examples
Input
2 2
1 2
Output
2
Input
4 2
3 1 2 4
Output
3
Input
6 2
6 5 3 1 2 4
Output
6
Input
2 10000000000
2 1
Output
2
Note
Games in the second sample:
3 plays with 1. 3 wins. 1 goes to the end of the line.
3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner. | 2 | 8 | n,k=map(int,input().split())
x=list(map(int,input().split()))
flag=0
c=k
while(flag!=n and c!=0):
if(x[0] > x[1]):
x.append(x[1])
del x[1]
c=c-1
flag=flag+1
elif(x[0] < x[1]):
x.append(x[0])
del x[0]
c=k-1
flag=0
print(x[0]) | PYTHON3 |
879_B. Table Tennis | n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner.
For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner.
Input
The first line contains two integers: n and k (2 β€ n β€ 500, 2 β€ k β€ 1012) β the number of people and the number of wins.
The second line contains n integers a1, a2, ..., an (1 β€ ai β€ n) β powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct.
Output
Output a single integer β power of the winner.
Examples
Input
2 2
1 2
Output
2
Input
4 2
3 1 2 4
Output
3
Input
6 2
6 5 3 1 2 4
Output
6
Input
2 10000000000
2 1
Output
2
Note
Games in the second sample:
3 plays with 1. 3 wins. 1 goes to the end of the line.
3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner. | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
long long MAXN = (long long)1e6 + 3000;
signed main() {
long long n, k;
cin >> n >> k;
vector<long long> a(n);
map<long long, long long> wins;
long long mx = -1;
for (long long i = 0; i < n; ++i)
cin >> a[i], wins[a[i]] = 0, mx = max(mx, a[i]);
deque<long long> queue;
long long cur = a[0];
for (long long i = 1; i < n; ++i) queue.push_back(a[i]);
while (cur != mx && wins[cur] != k) {
long long enemy = queue.front();
queue.pop_front();
if (enemy > cur) {
swap(cur, enemy);
}
wins[cur]++;
queue.push_back(enemy);
}
cout << cur << 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 = list( map( int, input().split() ) )
p = list( map( int, input().split() ) )
if n <= k:
print( max( p ) )
else:
i = 0
ans = 0
while i < n:
j = i+1
fl = False
tot = 0
if i > 0:
tot = 1
if j < n:
while p[i] > p[j] and j+1 < n:
j += 1
tot += 1
fl = True
if tot >= k and fl:
print( p[i] )
exit(0)
i = max( j, i+1 )
print( max(p) ) | PYTHON3 |
879_B. Table Tennis | n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner.
For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner.
Input
The first line contains two integers: n and k (2 β€ n β€ 500, 2 β€ k β€ 1012) β the number of people and the number of wins.
The second line contains n integers a1, a2, ..., an (1 β€ ai β€ n) β powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct.
Output
Output a single integer β power of the winner.
Examples
Input
2 2
1 2
Output
2
Input
4 2
3 1 2 4
Output
3
Input
6 2
6 5 3 1 2 4
Output
6
Input
2 10000000000
2 1
Output
2
Note
Games in the second sample:
3 plays with 1. 3 wins. 1 goes to the end of the line.
3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner. | 2 | 8 | //Codeforces Round #443 (Div. 2)
import java.io.*;
import java.util.*;
public class TaskB {
public static void main (String[] args) throws IOException {
FastScanner fs = new FastScanner(System.in);
PrintWriter pw = new PrintWriter(new BufferedOutputStream(System.out));
int n = fs.nextInt();
long kk = fs.nextLong();
int k = 0;
if (kk > n) {
k = n;
} else {
k = (int)kk;
}
int[] a = new int[n + n + n];
for (int i = 0; i < n; i++) {
a[i] = fs.nextInt();
}
int x = a[0];
int b = 1;
int e = n;
int y = 0;
while (e < n+n+n) {
if (x > a[b]) {
y++;
a[e] = a[b];
e++;
b++;
} else {
a[e] = x;
e++;
x = a[b];
b++;
y = 1;
}
if (y == k) break;
}
pw.println(x);
pw.close();
}
static class FastScanner {
BufferedReader reader;
StringTokenizer tokenizer;
FastScanner(InputStream i) {
reader = new BufferedReader(new InputStreamReader(i));
tokenizer = new StringTokenizer("");
}
String next() throws IOException {
while(!tokenizer.hasMoreTokens()) tokenizer = new StringTokenizer(reader.readLine());
return tokenizer.nextToken();
}
int nextInt() throws IOException {
return Integer.parseInt(next());
}
long nextLong() throws IOException {
return Long.parseLong(next());
}
}
} | JAVA |
879_B. Table Tennis | n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner.
For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner.
Input
The first line contains two integers: n and k (2 β€ n β€ 500, 2 β€ k β€ 1012) β the number of people and the number of wins.
The second line contains n integers a1, a2, ..., an (1 β€ ai β€ n) β powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct.
Output
Output a single integer β power of the winner.
Examples
Input
2 2
1 2
Output
2
Input
4 2
3 1 2 4
Output
3
Input
6 2
6 5 3 1 2 4
Output
6
Input
2 10000000000
2 1
Output
2
Note
Games in the second sample:
3 plays with 1. 3 wins. 1 goes to the end of the line.
3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner. | 2 | 8 | n, k = map(int, input().split())
a = list(map(int, input().split()))
wins = 0
bigger = a[0]
found = False
for i in range(1, n):
if bigger == n:
print(n)
found = True
break
if bigger > a[i]:
wins += 1
else:
bigger = a[i]
wins = 1
if wins == k:
print(bigger)
found = True
break
if not found:
print(bigger)
| 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 N = 1008;
long long d[N];
int main() {
long long n, k;
cin >> n >> k;
long long Max = 0;
for (long long i = 1; i <= n; i++) {
cin >> d[i];
Max = max(Max, d[i]);
}
long long win = 1;
long long id;
if (d[2] > d[1])
id = 2;
else
id = 1;
for (int i = 3; i <= n; i++) {
if (d[id] < d[i]) {
win = 1;
id = i;
} else
win++;
if (win == k) {
cout << d[id];
return 0;
}
}
cout << Max;
}
| CPP |
879_B. Table Tennis | n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner.
For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner.
Input
The first line contains two integers: n and k (2 β€ n β€ 500, 2 β€ k β€ 1012) β the number of people and the number of wins.
The second line contains n integers a1, a2, ..., an (1 β€ ai β€ n) β powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct.
Output
Output a single integer β power of the winner.
Examples
Input
2 2
1 2
Output
2
Input
4 2
3 1 2 4
Output
3
Input
6 2
6 5 3 1 2 4
Output
6
Input
2 10000000000
2 1
Output
2
Note
Games in the second sample:
3 plays with 1. 3 wins. 1 goes to the end of the line.
3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner. | 2 | 8 | import java.io.*;
import java.util.*;
public class B implements Runnable{
public static void main (String[] args) {new Thread(null, new B(), "_cf", 1 << 28).start();}
public void run() {
FastScanner fs = new FastScanner();
PrintWriter out = new PrintWriter(System.out);
int n = fs.nextInt();
long k = fs.nextLong();
int[] a = fs.nextIntArray(n);
if(k >= n) {
sort(a);
out.println(a[n-1]);
} else {
int win = 0;
int person = 0, fight = 1;
while(win < k) {
if(a[fight] > a[person]) {
person = fight;
win = 1;
} else {
win++;
if(win == k) {
person = a[person];
break;
}
}
fight = (fight + 1) % n;
}
out.println(person);
}
out.close();
}
void sort (int[] a) {
int n = a.length;
for(int i = 0; i < 1000; i++) {
Random r = new Random();
int x = r.nextInt(n), y = r.nextInt(n);
int temp = a[x];
a[x] = a[y];
a[y] = temp;
}
Arrays.sort(a);
}
class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner() {
try {
br = new BufferedReader(new InputStreamReader(System.in));
// br = new BufferedReader(new FileReader("testdata.out"));
st = new StringTokenizer("");
} catch (Exception e){e.printStackTrace();}
}
public String next() {
if (st.hasMoreTokens()) return st.nextToken();
try {st = new StringTokenizer(br.readLine());}
catch (Exception e) {e.printStackTrace();}
return st.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 line = "";
try {line = br.readLine();}
catch (Exception e) {e.printStackTrace();}
return line;
}
public Integer[] nextIntegerArray(int n) {
Integer[] a = new Integer[n];
for(int i = 0; i < n; i++) a[i] = nextInt();
return a;
}
public int[] nextIntArray(int n) {
int[] a = new int[n];
for(int i = 0; i < n; i++) a[i] = nextInt();
return a;
}
public char[] nextCharArray() {return nextLine().toCharArray();}
}
} | JAVA |
879_B. Table Tennis | n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner.
For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner.
Input
The first line contains two integers: n and k (2 β€ n β€ 500, 2 β€ k β€ 1012) β the number of people and the number of wins.
The second line contains n integers a1, a2, ..., an (1 β€ ai β€ n) β powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct.
Output
Output a single integer β power of the winner.
Examples
Input
2 2
1 2
Output
2
Input
4 2
3 1 2 4
Output
3
Input
6 2
6 5 3 1 2 4
Output
6
Input
2 10000000000
2 1
Output
2
Note
Games in the second sample:
3 plays with 1. 3 wins. 1 goes to the end of the line.
3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner. | 2 | 8 | first = input()
firstsplit = first.split()
n = int(firstsplit[0])
k = int(firstsplit[1])
second = input()
players = second.split()
players = [int(i) for i in players]
wins = 0
if players[0] > players[1]:
champion = players.pop(0)
wins = wins+1
y = players.pop(0)
players.append(y)
else:
champion = players.pop(1)
wins = wins+1
y = players.pop(0)
players.append(y)
while wins < k:
if wins > n:
break
x = players.pop(0)
if champion > x:
players.append(x)
wins = wins + 1
else:
players.append(champion)
champion = x
wins = 1
print(champion) | PYTHON3 |
879_B. Table Tennis | n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner.
For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner.
Input
The first line contains two integers: n and k (2 β€ n β€ 500, 2 β€ k β€ 1012) β the number of people and the number of wins.
The second line contains n integers a1, a2, ..., an (1 β€ ai β€ n) β powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct.
Output
Output a single integer β power of the winner.
Examples
Input
2 2
1 2
Output
2
Input
4 2
3 1 2 4
Output
3
Input
6 2
6 5 3 1 2 4
Output
6
Input
2 10000000000
2 1
Output
2
Note
Games in the second sample:
3 plays with 1. 3 wins. 1 goes to the end of the line.
3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner. | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
const int maxn = 600;
long long n, k;
int num;
queue<int> que;
int main() {
ios::sync_with_stdio(false);
while (scanf("%I64d%I64d", &n, &k) != EOF) {
int Max = 0;
while (!que.empty()) {
que.pop();
}
for (int i = 1; i <= n; ++i) {
scanf("%d", &num);
que.push(num);
Max = max(Max, num);
}
if (k > n) {
printf("%d\n", Max);
} else {
int win = 0;
num = que.front();
que.pop();
while (true) {
if (num > que.front()) {
int tmp = que.front();
que.pop();
que.push(tmp);
++win;
} else {
que.push(num);
num = que.front();
que.pop();
win = 1;
}
if (win >= k) {
break;
}
}
printf("%d\n", num);
}
}
return 0;
}
| CPP |
879_B. Table Tennis | n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner.
For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner.
Input
The first line contains two integers: n and k (2 β€ n β€ 500, 2 β€ k β€ 1012) β the number of people and the number of wins.
The second line contains n integers a1, a2, ..., an (1 β€ ai β€ n) β powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct.
Output
Output a single integer β power of the winner.
Examples
Input
2 2
1 2
Output
2
Input
4 2
3 1 2 4
Output
3
Input
6 2
6 5 3 1 2 4
Output
6
Input
2 10000000000
2 1
Output
2
Note
Games in the second sample:
3 plays with 1. 3 wins. 1 goes to the end of the line.
3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner. | 2 | 8 | n, k = map(int, raw_input().split())
a = map(int, raw_input().split())
if k >= n:
print max(a)
else:
p = a[0]
c = 0
for i in a[1:]:
if i < p:
c += 1
else:
p = i
c = 1
if c == k:
break
print p | PYTHON |
879_B. Table Tennis | n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner.
For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner.
Input
The first line contains two integers: n and k (2 β€ n β€ 500, 2 β€ k β€ 1012) β the number of people and the number of wins.
The second line contains n integers a1, a2, ..., an (1 β€ ai β€ n) β powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct.
Output
Output a single integer β power of the winner.
Examples
Input
2 2
1 2
Output
2
Input
4 2
3 1 2 4
Output
3
Input
6 2
6 5 3 1 2 4
Output
6
Input
2 10000000000
2 1
Output
2
Note
Games in the second sample:
3 plays with 1. 3 wins. 1 goes to the end of the line.
3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner. | 2 | 8 | n,k=map(int,input().split())
powers = list(map(int,input().split()))
maxPower=powers[0]
wins = 0
for i in range(1,n):
if maxPower > powers[i]:
wins += 1
else:
maxPower = powers[i]
wins = 1
if wins == k:
break
print(maxPower)
| PYTHON3 |
879_B. Table Tennis | n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner.
For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner.
Input
The first line contains two integers: n and k (2 β€ n β€ 500, 2 β€ k β€ 1012) β the number of people and the number of wins.
The second line contains n integers a1, a2, ..., an (1 β€ ai β€ n) β powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct.
Output
Output a single integer β power of the winner.
Examples
Input
2 2
1 2
Output
2
Input
4 2
3 1 2 4
Output
3
Input
6 2
6 5 3 1 2 4
Output
6
Input
2 10000000000
2 1
Output
2
Note
Games in the second sample:
3 plays with 1. 3 wins. 1 goes to the end of the line.
3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner. | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n;
long long k;
cin >> n >> k;
vector<int> a(n + 1);
int maxx;
cin >> maxx;
int temp;
bool flag = 1;
int ans;
for (int i = 1; i <= n - 1; i++) {
cin >> temp;
maxx = max(temp, maxx);
a[maxx]++;
if (a[maxx] == k && flag == 1) {
ans = maxx;
flag = 0;
}
}
if (flag != 0) ans = maxx;
cout << ans;
return 0;
}
| CPP |
879_B. Table Tennis | n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner.
For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner.
Input
The first line contains two integers: n and k (2 β€ n β€ 500, 2 β€ k β€ 1012) β the number of people and the number of wins.
The second line contains n integers a1, a2, ..., an (1 β€ ai β€ n) β powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct.
Output
Output a single integer β power of the winner.
Examples
Input
2 2
1 2
Output
2
Input
4 2
3 1 2 4
Output
3
Input
6 2
6 5 3 1 2 4
Output
6
Input
2 10000000000
2 1
Output
2
Note
Games in the second sample:
3 plays with 1. 3 wins. 1 goes to the end of the line.
3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner. | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int main() {
int n, i, j, vitorias[550];
long long int powers[550], maior = -1, k;
deque<int> jogo;
scanf("%d %lld", &n, &k);
for (i = 1; i <= n; i++)
scanf("%lld", &powers[i]), jogo.push_back(i), vitorias[i] = 0,
maior = max(maior, powers[i]);
if (k < n - 1) {
while (true) {
int p1, p2;
p1 = jogo.front();
jogo.pop_front();
p2 = jogo.front();
jogo.pop_front();
if (powers[p1] > powers[p2]) {
vitorias[p1]++;
if (vitorias[p1] >= k) {
printf("%lld\n", powers[p1]);
return 0;
}
jogo.push_front(p1);
jogo.push_back(p2);
vitorias[p2] = 0;
} else {
vitorias[p2]++;
if (vitorias[p2] >= k) {
printf("%lld\n", powers[p2]);
return 0;
}
jogo.push_front(p2);
jogo.push_back(p1);
vitorias[p1] = 0;
}
}
} else {
printf("%lld\n", maior);
}
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.*;
/**
* 26-Oct-2017
* 8:33:52 PM
* @author HulkBuster
*
*/
public class CF879 {
public static int mod = (int) (1e9 + 7);
public static InputReader in;
public static PrintWriter out;
public static void solve() throws FileNotFoundException {
in = new InputReader(System.in);
out = new PrintWriter(System.out);
int n = in.ni();
long k = in.nl();
int ar[] = in.nIArr(n);
int max = ar[0];
int c = 0;
for(int i=1;i<n;i++){
if(max>ar[i])
c++;
else{
//out.println("Here");
c=1;
max = ar[i];
}
if(c==k){
break;
}
}
out.println(max);
out.close();
}
public static void main(String[] args) {
new Thread(null, new Runnable() {
public void run() {
try {
solve();
} catch (Exception e) {
e.printStackTrace();
}
}
}, "1", 1 << 26).start();
}
static class Pair implements Comparable<Pair> {
int x, y, i;
Pair(int x, int y, int i) {
this.x = x;
this.y = y;
this.i = i;
}
Pair(int x, int y) {
this.x = x;
this.y = y;
}
public int compareTo(Pair o) {
if (this.x != o.x)
return Integer.compare(this.x, o.x);
else
return Integer.compare(this.y, o.y);
//return 0;
}
public boolean equals(Object o) {
if (o instanceof Pair) {
Pair p = (Pair) o;
return p.x == x && p.y == y && p.i == i;
}
return false;
}
public int hashCode() {
return new Integer(x).hashCode() * 31 + new Integer(y).hashCode() + new Integer(i).hashCode() * 37;
}
}
public static long add(long a, long b) {
long x = (a + b);
while (x >= mod)
x -= mod;
return x;
}
public static long sub(long a, long b) {
long x = (a - b);
while (x < 0)
x += mod;
return x;
}
public static long mul(long a, long b) {
long x = (a * b);
while (x >= mod)
x -= mod;
return x;
}
public static boolean isPal(String s) {
for (int i = 0, j = s.length() - 1; i <= j; i++, j--) {
if (s.charAt(i) != s.charAt(j))
return false;
}
return true;
}
public static String rev(String s) {
StringBuilder sb = new StringBuilder(s);
sb.reverse();
return sb.toString();
}
public static long gcd(long x, long y) {
if (x % y == 0)
return y;
else
return gcd(y, x % y);
}
public static int gcd(int x, int y) {
if (x % y == 0)
return y;
else
return gcd(y, x % y);
}
static int modInverse(int a, int m) {
int m0 = m, t, q;
int x0 = 0, x1 = 1;
if (m == 1)
return 0;
while (a > 1) {
// q is quotient
q = a / m;
t = m;
// m is remainder now, process same as
// Euclid's algo
m = a % m;
a = t;
t = x0;
x0 = x1 - q * x0;
x1 = t;
}
// Make x1 positive
if (x1 < 0)
x1 += m0;
return x1;
}
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 mulmod(long a, long b, long m) {
if (m <= 1000000009)
return a * b % m;
long res = 0;
while (a > 0) {
if ((a & 1) != 0) {
res += b;
if (res >= m)
res -= m;
}
a >>= 1;
b <<= 1;
if (b >= m)
b -= m;
}
return res;
}
public static int abs(int a, int b) {
return (int) Math.abs(a - b);
}
public static long abs(long a, long b) {
return (long) Math.abs(a - b);
}
public static int max(int a, int b) {
if (a > b)
return a;
else
return b;
}
public static int min(int a, int b) {
if (a > b)
return b;
else
return a;
}
public static long max(long a, long b) {
if (a > b)
return a;
else
return b;
}
public static long min(long a, long b) {
if (a > b)
return b;
else
return a;
}
public static long pow(long n, long p, long m) {
long result = 1;
if (p == 0)
return 1;
if (p == 1)
return n;
while (p != 0) {
if (p % 2 == 1)
result *= n;
if (result >= m)
result %= m;
p >>= 1;
n *= n;
if (n >= m)
n %= m;
}
return result;
}
public static long pow(long n, long p) {
long result = 1;
if (p == 0)
return 1;
if (p == 1)
return n;
while (p != 0) {
if (p % 2 == 1)
result *= n;
p >>= 1;
n *= n;
}
return result;
}
public static void debug(Object... o) {
System.out.println(Arrays.deepToString(o));
}
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 ni() {
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 nl() {
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[] nIArr(int n) {
int a[] = new int[n];
for (int i = 0; i < n; i++) {
a[i] = ni();
}
return a;
}
public long[] nLArr(int n) {
long a[] = new long[n];
for (int i = 0; i < n; i++) {
a[i] = nl();
}
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 int n, i, a, t = 0, maxm = 0, tempmax = 0, k;
cin >> n >> k;
vector<long long int> A;
for (i = 0; i < n; i++) {
cin >> a;
A.push_back(a);
if (maxm < a) maxm = a;
}
if (k >= n - 1)
cout << maxm << endl;
else {
tempmax = A[0];
for (i = 1; i < n; i++) {
if (A[i] == maxm || tempmax == maxm) {
cout << maxm << endl;
break;
} else {
if (tempmax > A[i]) {
t++;
if (t == k) {
cout << tempmax << endl;
break;
}
} else {
tempmax = A[i];
t = 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 | l1 = raw_input().split()
players = int(l1[0])
wins = int(l1[1])
l1 = map(int,raw_input().split())
if players < wins:
print max(l1)
else:
d = {}
for i in l1:
d[int(i)] = 0 # Number of wins
while True:
i = int(l1[0])
j = int(l1[1])
if i > j:
d[i] += 1
if d[i] == wins:
print i
break
else:
l1.pop(1)
l1.append(j)
else:
d[j] += 1
if d[j] == wins:
print j
break
else:
l1.pop(0)
l1.append(i)
| PYTHON |
879_B. Table Tennis | n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner.
For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner.
Input
The first line contains two integers: n and k (2 β€ n β€ 500, 2 β€ k β€ 1012) β the number of people and the number of wins.
The second line contains n integers a1, a2, ..., an (1 β€ ai β€ n) β powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct.
Output
Output a single integer β power of the winner.
Examples
Input
2 2
1 2
Output
2
Input
4 2
3 1 2 4
Output
3
Input
6 2
6 5 3 1 2 4
Output
6
Input
2 10000000000
2 1
Output
2
Note
Games in the second sample:
3 plays with 1. 3 wins. 1 goes to the end of the line.
3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner. | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
const int maxm = 100005;
int a[maxm], flag[maxm];
int main() {
long long k;
int n, i, j, sum;
scanf("%d%lld", &n, &k);
for (i = 1; i <= n; i++) scanf("%d", &a[i]);
int now = a[1];
for (i = 2; i <= n; i++) {
if (now > a[i]) {
flag[now]++;
if (flag[now] == k) {
printf("%d\n", now);
return 0;
}
} else {
now = a[i], flag[a[i]]++;
if (flag[a[i]] == k) {
printf("%d\n", a[i]);
return 0;
}
}
}
printf("%d\n", n);
return 0;
}
| CPP |
879_B. Table Tennis | n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner.
For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner.
Input
The first line contains two integers: n and k (2 β€ n β€ 500, 2 β€ k β€ 1012) β the number of people and the number of wins.
The second line contains n integers a1, a2, ..., an (1 β€ ai β€ n) β powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct.
Output
Output a single integer β power of the winner.
Examples
Input
2 2
1 2
Output
2
Input
4 2
3 1 2 4
Output
3
Input
6 2
6 5 3 1 2 4
Output
6
Input
2 10000000000
2 1
Output
2
Note
Games in the second sample:
3 plays with 1. 3 wins. 1 goes to the end of the line.
3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner. | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) {
if (b == 0)
return a;
else {
return gcd(b, a % b);
}
}
long long lcm(long long a, long long b) { return (a * b) / gcd(a, b); }
int main() {
long long n, k;
cin >> n >> k;
long long t = k;
long long x, player = -1;
for (long long c = 0; c < n; c++) {
cin >> x;
if (player == -1)
player = x;
else {
if (player > x)
t--;
else
player = x, t = k - 1;
if (t == 0) {
cout << player << endl;
return 0;
}
}
}
cout << player << 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 = input().split()
n,k = int(n),int(k)
a = [int(i) for i in input().strip().split()]
if(k>=n):
print(max(a))
else:
tmp = a[0]
count =0
for i in range(1,n):
if(tmp>a[i]):
count = count +1
if(count==k):
break
else:
count =1
tmp = a[i]
print(tmp) | 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()))
k1 = k - 1
a1 = max(a[0], a[1])
for i in range(2, n):
if a1 > a[i]:
k1 -= 1
else:
a1 = a[i]
k1 = k - 1
if k1 == 0:
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 |
[n, k] = list(map(int, input().split(' ')))
seq = list(map(int, input().split(' ')))
if k > n:
print(max(seq))
else:
currentWinner = max(seq[0], seq[1])
wins = 1
index = 2
while True:
if index >= len(seq) or wins == k:
break
winner = max(currentWinner, seq[index])
if winner != currentWinner:
currentWinner = winner
wins = 1
else:
wins += 1
index += 1
print(currentWinner)
| PYTHON3 |
879_B. Table Tennis | n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner.
For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner.
Input
The first line contains two integers: n and k (2 β€ n β€ 500, 2 β€ k β€ 1012) β the number of people and the number of wins.
The second line contains n integers a1, a2, ..., an (1 β€ ai β€ n) β powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct.
Output
Output a single integer β power of the winner.
Examples
Input
2 2
1 2
Output
2
Input
4 2
3 1 2 4
Output
3
Input
6 2
6 5 3 1 2 4
Output
6
Input
2 10000000000
2 1
Output
2
Note
Games in the second sample:
3 plays with 1. 3 wins. 1 goes to the end of the line.
3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner. | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int main() {
int i, n, t;
long long k, cnt = 0;
cin >> n >> k;
int cc = (n < k) ? n : k;
int *a = (int *)malloc(sizeof(int) * n);
for (i = 0; i < n; i++) {
cin >> a[i];
}
i = 0;
t = a[0];
do {
i = (++i) % n;
if (t > a[i]) {
cnt++;
if (i == 0)
a[n - 1] = a[i];
else
a[i - 1] = a[i];
} else {
cnt = 1;
t = a[i];
}
} while (cnt < cc);
cout << t;
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;
using namespace std::chrono;
void solve() {
long long int n, i, m = 0, q = 0, mx = -20000000000000000,
mn = 20000000000000000, k = 0, j = 0, c = 0, d = 0, t = 0,
sm = 0, pdt = 1, l = 0, r = 0;
cin >> n;
long long int v[n];
cin >> m;
queue<long long int> qu;
for (i = 0; i < n; i++) {
cin >> v[i];
qu.push(v[i]);
}
k = 0;
c = qu.front();
qu.pop();
i = 0;
while (true) {
if (i == n + 1) {
cout << c << endl;
return;
}
if (k == m) {
cout << c << endl;
return;
}
d = qu.front();
qu.pop();
if (c > d) {
k++;
qu.push(d);
} else {
k = 1;
qu.push(c);
c = d;
}
i++;
}
return;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long int t;
t = 1;
while (t--) {
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 | def main():
n, k = map(int, input().split())
m, *bb = map(int, input().split())
if k < n:
c = 0
for i in range(3):
aa, bb = bb, []
for a in aa:
if m < a:
bb.append(m)
m, c = a, 1
else:
bb.append(a)
c += 1
if c == k:
print(m)
return
print(n)
if __name__ == '__main__':
main()
| PYTHON3 |
879_B. Table Tennis | n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner.
For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner.
Input
The first line contains two integers: n and k (2 β€ n β€ 500, 2 β€ k β€ 1012) β the number of people and the number of wins.
The second line contains n integers a1, a2, ..., an (1 β€ ai β€ n) β powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct.
Output
Output a single integer β power of the winner.
Examples
Input
2 2
1 2
Output
2
Input
4 2
3 1 2 4
Output
3
Input
6 2
6 5 3 1 2 4
Output
6
Input
2 10000000000
2 1
Output
2
Note
Games in the second sample:
3 plays with 1. 3 wins. 1 goes to the end of the line.
3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner. | 2 | 8 | n = [int(x) for x in input().split(" ")]
poderes = [int(x) for x in input().split(" ")]
ind = 0
cont = 0
maior = poderes[0]
if(n[1] > n[0]):
print(max(poderes))
else:
while(cont < n[1]):
if(ind == n[0]):
ind = 0
elif(poderes[ind] == maior):
ind+=1
elif(maior > poderes[ind]):
cont+=1
ind+=1
else:
maior = poderes[ind]
cont = 1
ind+=1
print(maior)
| 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, players):
p = deque(players)
i = 0
while i < len(p):
wcounter = 0
has_fought = []
fcounter = 0
while True:
fcounter += 1
if(p[0] > p[1]):
if fcounter == 1 and p[0] > p[-1]:
wcounter += 2
else:
wcounter += 1
has_fought.append(p[1])
if wcounter == wins or (len(has_fought) + 1 == len(p) and max(p) == p[0]):
print(p[0])
return
else:
temp = p[1]
p.remove(temp)
p.append(temp)
else:
val = p.popleft()
p.append(val)
break
i += 1
def main():
first_line = [int(x) for x in input().split(' ')]
second_line = [int(x) for x in input().split(' ')]
ping_pong(first_line[1], second_line)
main() | PYTHON3 |
879_B. Table Tennis | n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner.
For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner.
Input
The first line contains two integers: n and k (2 β€ n β€ 500, 2 β€ k β€ 1012) β the number of people and the number of wins.
The second line contains n integers a1, a2, ..., an (1 β€ ai β€ n) β powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct.
Output
Output a single integer β power of the winner.
Examples
Input
2 2
1 2
Output
2
Input
4 2
3 1 2 4
Output
3
Input
6 2
6 5 3 1 2 4
Output
6
Input
2 10000000000
2 1
Output
2
Note
Games in the second sample:
3 plays with 1. 3 wins. 1 goes to the end of the line.
3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner. | 2 | 8 | def main():
n, k = map(int, input().split())
power = list(map(int,input().split()))
cnt = 0
win = power[0]
del(power[0])
while True:
for i in range(0, len(power)):
if win > power[i]:
cnt = cnt + 1
if cnt >= len(power):
print(win)
return
if cnt >= k:
print(win)
return
else:
for j in range(0, i):
power.append(power[0])
del(power[0])
power.append(win)
win = power[0]
del(power[0])
cnt = 1
break
if __name__ == "__main__":
main()
| PYTHON3 |
879_B. Table Tennis | n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner.
For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner.
Input
The first line contains two integers: n and k (2 β€ n β€ 500, 2 β€ k β€ 1012) β the number of people and the number of wins.
The second line contains n integers a1, a2, ..., an (1 β€ ai β€ n) β powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct.
Output
Output a single integer β power of the winner.
Examples
Input
2 2
1 2
Output
2
Input
4 2
3 1 2 4
Output
3
Input
6 2
6 5 3 1 2 4
Output
6
Input
2 10000000000
2 1
Output
2
Note
Games in the second sample:
3 plays with 1. 3 wins. 1 goes to the end of the line.
3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner. | 2 | 8 | pV = input().split()
p = int(pV[0])
vNeeded = int(pV[1])
powP = list(map(int, input().rstrip().split()))
if (p < vNeeded):
print(max(powP))
else:
win = 0
while win < vNeeded:
if int(powP[0]) > int(powP[1]):
powP.append(powP.pop(1))
win = win + 1
else:
win = 0
powP.append(powP.pop(0))
win = win + 1
print(powP[0])
| PYTHON3 |
879_B. Table Tennis | n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner.
For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner.
Input
The first line contains two integers: n and k (2 β€ n β€ 500, 2 β€ k β€ 1012) β the number of people and the number of wins.
The second line contains n integers a1, a2, ..., an (1 β€ ai β€ n) β powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct.
Output
Output a single integer β power of the winner.
Examples
Input
2 2
1 2
Output
2
Input
4 2
3 1 2 4
Output
3
Input
6 2
6 5 3 1 2 4
Output
6
Input
2 10000000000
2 1
Output
2
Note
Games in the second sample:
3 plays with 1. 3 wins. 1 goes to the end of the line.
3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner. | 2 | 8 | #what
l1 = raw_input().split()
players = int(l1[0])
wins = int(l1[1])
l1 = map(int,raw_input().split())
if players < wins:
print max(l1)
else:
d = {}
for i in l1:
d[int(i)] = 0 # Number of wins
while True:
i = int(l1[0])
j = int(l1[1])
if i > j:
d[i] += 1
if d[i] == wins:
print i
break
else:
l1.pop(1)
l1.append(j)
else:
d[j] += 1
if d[j] == wins:
print j
break
else:
l1.pop(0)
l1.append(i)
| PYTHON |
879_B. Table Tennis | n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner.
For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner.
Input
The first line contains two integers: n and k (2 β€ n β€ 500, 2 β€ k β€ 1012) β the number of people and the number of wins.
The second line contains n integers a1, a2, ..., an (1 β€ ai β€ n) β powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct.
Output
Output a single integer β power of the winner.
Examples
Input
2 2
1 2
Output
2
Input
4 2
3 1 2 4
Output
3
Input
6 2
6 5 3 1 2 4
Output
6
Input
2 10000000000
2 1
Output
2
Note
Games in the second sample:
3 plays with 1. 3 wins. 1 goes to the end of the line.
3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner. | 2 | 8 | n,k=map(int,input().split())
a=list(map(int,input().split()))
if k>=n-1:
print(max(a))
else:
w=a[0];t,q,l=int(w),int(k),max(a)
for i in range(1,n):
w=max(w,a[i])
if w!=t:q=int(k)-1
else:q-=1
t=int(w)
if q==0 or w==l:break
print(w) | PYTHON3 |
879_B. Table Tennis | n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner.
For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner.
Input
The first line contains two integers: n and k (2 β€ n β€ 500, 2 β€ k β€ 1012) β the number of people and the number of wins.
The second line contains n integers a1, a2, ..., an (1 β€ ai β€ n) β powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct.
Output
Output a single integer β power of the winner.
Examples
Input
2 2
1 2
Output
2
Input
4 2
3 1 2 4
Output
3
Input
6 2
6 5 3 1 2 4
Output
6
Input
2 10000000000
2 1
Output
2
Note
Games in the second sample:
3 plays with 1. 3 wins. 1 goes to the end of the line.
3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner. | 2 | 8 | a,b=map(int,input().split())
z=list(map(int,input().split()))
r=z.index(max(z));j=0;i=0
while i<r:
s=0
if i!=0:
if z[i-1]<z[i]:s+=1
for j in range(i+1,r):
if z[i]>z[j]:s+=1
else:break
if s>=b:exit(print(z[i]))
i=max(i+1,j)
print(max(z)) | PYTHON3 |
879_B. Table Tennis | n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner.
For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner.
Input
The first line contains two integers: n and k (2 β€ n β€ 500, 2 β€ k β€ 1012) β the number of people and the number of wins.
The second line contains n integers a1, a2, ..., an (1 β€ ai β€ n) β powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct.
Output
Output a single integer β power of the winner.
Examples
Input
2 2
1 2
Output
2
Input
4 2
3 1 2 4
Output
3
Input
6 2
6 5 3 1 2 4
Output
6
Input
2 10000000000
2 1
Output
2
Note
Games in the second sample:
3 plays with 1. 3 wins. 1 goes to the end of the line.
3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner. | 2 | 8 | import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
public class Main
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
long k = sc.nextLong();
int [] arr = new int[n];
for (int i =0;i<n;i++)
arr[i] = sc.nextInt();
int max =arr[0];
int count =0;
for (int i =1;i<n;i++)
{
if (arr[i]>max)
{
max=arr[i];
count=1;
}
else
count++;
if (count==k)
break;
}
System.out.println(max);
sc.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 | IL = lambda: list(map(int, input().split()))
I = lambda: int(input())
n, k = IL()
a = IL()
ans = 0
score = 0
for i in range(n-1):
if a[0] > a[1]:
score += 1
else:
score = 1
if score == k:
ans = a[0]
break
p1, p2 = a[:2]
a.pop(0)
a[0] = max(p1, p2)
a.append(min(p1, p2))
if ans==0:
ans = max(a)
print(ans) | PYTHON3 |
879_B. Table Tennis | n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner.
For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner.
Input
The first line contains two integers: n and k (2 β€ n β€ 500, 2 β€ k β€ 1012) β the number of people and the number of wins.
The second line contains n integers a1, a2, ..., an (1 β€ ai β€ n) β powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct.
Output
Output a single integer β power of the winner.
Examples
Input
2 2
1 2
Output
2
Input
4 2
3 1 2 4
Output
3
Input
6 2
6 5 3 1 2 4
Output
6
Input
2 10000000000
2 1
Output
2
Note
Games in the second sample:
3 plays with 1. 3 wins. 1 goes to the end of the line.
3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner. | 2 | 8 | size,wins=map(int,raw_input().split())
arr=map(int,raw_input().split())
if wins>(size-1):
print max(arr)
else:
wins_till_now=wins
while wins_till_now:
first,second=arr[0],arr[1]
if first>second:
arr.remove(second)
arr.append(second)
wins_till_now-=1
else:
arr.remove(first)
arr.append(first)
wins_till_now=wins-1
print first
| PYTHON |
879_B. Table Tennis | n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner.
For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner.
Input
The first line contains two integers: n and k (2 β€ n β€ 500, 2 β€ k β€ 1012) β the number of people and the number of wins.
The second line contains n integers a1, a2, ..., an (1 β€ ai β€ n) β powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct.
Output
Output a single integer β power of the winner.
Examples
Input
2 2
1 2
Output
2
Input
4 2
3 1 2 4
Output
3
Input
6 2
6 5 3 1 2 4
Output
6
Input
2 10000000000
2 1
Output
2
Note
Games in the second sample:
3 plays with 1. 3 wins. 1 goes to the end of the line.
3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner. | 2 | 8 | n, k = map(int, input().split())
powers = list(map(int, input().split()))
max_power = max(powers)
def get_winner():
i = 0
while True:
if powers[i] == max_power:
return powers[i]
for j in range(i + 1, min(i + k + int(i == 0), n)):
if powers[i] < powers[j]:
i = j
break
else:
return powers[i]
print(get_winner())
| PYTHON3 |
879_B. Table Tennis | n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner.
For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner.
Input
The first line contains two integers: n and k (2 β€ n β€ 500, 2 β€ k β€ 1012) β the number of people and the number of wins.
The second line contains n integers a1, a2, ..., an (1 β€ ai β€ n) β powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct.
Output
Output a single integer β power of the winner.
Examples
Input
2 2
1 2
Output
2
Input
4 2
3 1 2 4
Output
3
Input
6 2
6 5 3 1 2 4
Output
6
Input
2 10000000000
2 1
Output
2
Note
Games in the second sample:
3 plays with 1. 3 wins. 1 goes to the end of the line.
3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner. | 2 | 8 | n,k=[int(i) for i in input().split()]
list1=[int(i) for i in input().split()]
if n==2 or k>10**6:
print(max(list1))
else:
total=0
player=list1[0]
waiter=list1[1:]
while total<k:
if player>waiter[0]:
total+=1
waiter=waiter[1:]+[waiter[0]]
else:
total=1
waiter=waiter[:]+[player]
player=waiter[0]
waiter=waiter[1:]
print(player)
| PYTHON3 |
879_B. Table Tennis | n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner.
For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner.
Input
The first line contains two integers: n and k (2 β€ n β€ 500, 2 β€ k β€ 1012) β the number of people and the number of wins.
The second line contains n integers a1, a2, ..., an (1 β€ ai β€ n) β powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct.
Output
Output a single integer β power of the winner.
Examples
Input
2 2
1 2
Output
2
Input
4 2
3 1 2 4
Output
3
Input
6 2
6 5 3 1 2 4
Output
6
Input
2 10000000000
2 1
Output
2
Note
Games in the second sample:
3 plays with 1. 3 wins. 1 goes to the end of the line.
3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner. | 2 | 8 |
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.stream.IntStream;
import java.util.stream.Stream;
/**
* Success is not final, failure is not fatal. The courage to continue is what counts.
* @author bodmas
*/
public class ProblemB {
private int solve(int[] b, long k) {
Node head = new Node(b[0]);
Node tail = head;
for (int i = 1; i < b.length; i++) {
Node curr = new Node(b[i]);
tail.next = curr;
tail = curr;
}
tail.next = head;
int previousWinner = -1;
int cnt = 0;
while (head.value != b.length) {
while (true) {
int winner = Math.max(head.value, head.next.value);
if (winner == previousWinner && ++cnt == k || winner != previousWinner && (cnt = 1) == k)
return winner;
previousWinner = winner;
if (head.value > head.next.value) {
tail.next = head.next;
tail = tail.next;
Node former = head.next;
head.next = head.next.next;
former.next = head;
} else {
tail = tail.next;
head = head.next;
break;
}
}
}
return head.value;
}
private static class Node {
Node next;
int value;
Node(int value) {
this.value = value;
}
}
public static void main(String[] args) throws IOException {
try (BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out))) {
long k = Stream.of(in.readLine().split(" ")).skip(1).mapToLong(Long::parseLong).findAny().getAsLong();
int[] a = Stream.of(in.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
out.println(new ProblemB().solve(a, k));
}
}
} | JAVA |
879_B. Table Tennis | n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner.
For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner.
Input
The first line contains two integers: n and k (2 β€ n β€ 500, 2 β€ k β€ 1012) β the number of people and the number of wins.
The second line contains n integers a1, a2, ..., an (1 β€ ai β€ n) β powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct.
Output
Output a single integer β power of the winner.
Examples
Input
2 2
1 2
Output
2
Input
4 2
3 1 2 4
Output
3
Input
6 2
6 5 3 1 2 4
Output
6
Input
2 10000000000
2 1
Output
2
Note
Games in the second sample:
3 plays with 1. 3 wins. 1 goes to the end of the line.
3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner. | 2 | 8 | n,k=map(int,input().split())
a=[int(i) for i in input().split()]
if n==2:
print(max(a))
exit()
if k>n:
print(max(a))
exit()
k+=1
#a=a+a
a.insert(0,10**9)
for i in range(1,n-k+2):
if a[i]==max(a[i-1:i+k-1]) or a[i]==max(a[i:i+k]):
print(a[i])
exit()
print(max(a[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 | // package CF;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
public class A {
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
PrintWriter out = new PrintWriter(System.out);
int n = sc.nextInt();
long k = sc.nextLong();
int [] a = new int[n];
int max = -1;
for (int i = 0; i < a.length; ++i) {
a[i] = sc.nextInt();
if(max == -1 || a[max] < a[i]){
max = i;
}
}
int ans = a[max];
int prev = a[0], wins = 0;
for (int i = 1; i < max; ++i) {
if(prev > a[i]){
++wins;
}
else{
prev = a[i];
wins = 1;
}
if(wins == k)
{
ans = prev;
break;
}
}
out.println(ans);
out.flush();
out.close();
}
static class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream s) {
br = new BufferedReader(new InputStreamReader(s));
}
public Scanner(FileReader fileReader) {
br = new BufferedReader(fileReader);
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public String nextLine() throws IOException {
return br.readLine();
}
public boolean ready() throws IOException {
return br.ready();
}
}
} | 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()))
p=max(a)
t=0
if k>600:
print(p)
else:
while t!=k:
if a[0]>a[1]:
if a[0]==p:
print(p)
exit()
m=a[1]
del a[1]
a.append(m)
t+=1
else:
m=a[0]
del a[0]
a.append(m)
t=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;
int main() {
int n;
long long k;
cin >> n >> k;
int a[n];
int i;
for (i = 0; i < n; i++) cin >> a[i];
if (k >= n) {
int m = a[0];
for (i = 1; i < n; i++) {
m = max(m, a[i]);
}
cout << m;
} else {
int j;
for (j = 1; j < k + 1; j++) {
if (a[j % n] > a[0]) break;
}
if (j == k + 1) {
cout << a[0];
exit(0);
}
for (i = 1; i < n; i++) {
int m = a[i];
for (j = i + 1; j < i + k; j++) {
if (a[j % n] > a[i]) break;
}
if (j == i + k) break;
}
cout << a[i];
}
}
| 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()))
curr_player_power = powers[0]
curr_player_wins = 0
for i in range(1, n):
if powers[i] < curr_player_power:
curr_player_wins += 1
else:
curr_player_power = powers[i]
curr_player_wins = 1
if curr_player_wins == k:
break
print(curr_player_power)
| PYTHON3 |
879_B. Table Tennis | n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner.
For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner.
Input
The first line contains two integers: n and k (2 β€ n β€ 500, 2 β€ k β€ 1012) β the number of people and the number of wins.
The second line contains n integers a1, a2, ..., an (1 β€ ai β€ n) β powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct.
Output
Output a single integer β power of the winner.
Examples
Input
2 2
1 2
Output
2
Input
4 2
3 1 2 4
Output
3
Input
6 2
6 5 3 1 2 4
Output
6
Input
2 10000000000
2 1
Output
2
Note
Games in the second sample:
3 plays with 1. 3 wins. 1 goes to the end of the line.
3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner. | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
long long n, k;
cin >> n >> k;
int co;
queue<int> q;
cin >> co;
for (int i = 1; i < n; i++) {
int t;
cin >> t;
q.push(t);
}
int res = n;
long long rk = 0;
while (co != n) {
int va = q.front();
q.pop();
if (va > co) {
swap(co, va);
rk = 0;
}
q.push(va);
rk++;
if (rk >= k) {
res = co;
break;
}
}
cout << res << '\n';
}
| CPP |
879_B. Table Tennis | n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner.
For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner.
Input
The first line contains two integers: n and k (2 β€ n β€ 500, 2 β€ k β€ 1012) β the number of people and the number of wins.
The second line contains n integers a1, a2, ..., an (1 β€ ai β€ n) β powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct.
Output
Output a single integer β power of the winner.
Examples
Input
2 2
1 2
Output
2
Input
4 2
3 1 2 4
Output
3
Input
6 2
6 5 3 1 2 4
Output
6
Input
2 10000000000
2 1
Output
2
Note
Games in the second sample:
3 plays with 1. 3 wins. 1 goes to the end of the line.
3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner. | 2 | 8 | n,k=map(int,input().split())
l=list(map(int,input().split()))
c=0
if k>=n:
print(sorted(l)[-1])
else:
while c<k:
if l[0]>l[1]:
c=c+1
l.insert(n,l[1])
del l[1]
else:
c=1
l.insert(n,l[0])
del l[0]
print(l[0]) | PYTHON3 |
879_B. Table Tennis | n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner.
For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner.
Input
The first line contains two integers: n and k (2 β€ n β€ 500, 2 β€ k β€ 1012) β the number of people and the number of wins.
The second line contains n integers a1, a2, ..., an (1 β€ ai β€ n) β powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct.
Output
Output a single integer β power of the winner.
Examples
Input
2 2
1 2
Output
2
Input
4 2
3 1 2 4
Output
3
Input
6 2
6 5 3 1 2 4
Output
6
Input
2 10000000000
2 1
Output
2
Note
Games in the second sample:
3 plays with 1. 3 wins. 1 goes to the end of the line.
3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner. | 2 | 8 | import java.io.*;
import java.util.*;
public class A {
public static void main(String[] args) throws Exception {
Scanner in = new Scanner(System.in);
PrintWriter out = new PrintWriter(System.out);
//int[] a = new int[500];
ArrayList<Integer> a = new ArrayList<>(501);
int k = 0, n = 0;
n = in.nextInt();
String s = in.next();
if (s.length() > 3)
k = 500;
else
k = Integer.valueOf(s);
if (k > 500)
k = 500;
for (int i = 0; i < n; i++)
a.add(in.nextInt());
int m = 0;
while (m < k) {
if (a.get(0) > a.get(1)) {
a.add(a.get(1));
a.remove(1);
m++;
} else {
a.add(a.get(0));
a.remove(0);
m = 1;
}
}
out.print(a.get(0));
out.close();
//// out.print(max);
//
// out.println();
// 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 | import java.io.*;
import java.util.*;
import java.math.*;
public class Main {
public static void main(String[] args) throws IOException
{
// StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
Scanner sc=new Scanner(System.in);
//FastScanner sc = new FastScanner();
/* FileWriter fw = new FileWriter("e:\\1.out");*/
int n;
long k;
int a[]=new int[600];
int st;
n=sc.nextInt();
k=sc.nextLong();
for(int i=0;i<n;++i)
a[i]=sc.nextInt();
int ans = a[0];
st=0;
for(int i=1;i<n;++i)
{
if(st>=k)
{
out.println(ans);
out.flush();
return ;
}
if(ans>a[i])
{
st++;
}
else
{
st=1;
ans = a[i];
}
}
out.println(ans);
out.flush();
}
static class FastScanner {
BufferedReader br;
StringTokenizer st;
private FastScanner() {
try {
br = new BufferedReader(new InputStreamReader(System.in));
st = new StringTokenizer(br.readLine());
} catch (Exception e){e.printStackTrace();}
}
private boolean hasNextToken()
{
if(st.countTokens()!=StreamTokenizer.TT_EOF)
{
return true;
}
else
return false;
}
private String next() {
if (st.hasMoreTokens()) return st.nextToken();
try {st = new StringTokenizer(br.readLine());}
catch (Exception e) {e.printStackTrace();}
return st.nextToken();
}
private BigInteger nextBigInteger(){return new BigInteger(next());}
private BigDecimal nextBigDecimal(){return new BigDecimal(next());}
private int nextInt() {return Integer.parseInt(next());}
private long nextLong() {return Long.parseLong(next());}
private double nextDouble() {return Double.parseDouble(next());}
private String nextLine() {
String line = "";
if(st.hasMoreTokens()) line = st.nextToken();
else try {return br.readLine();}catch(IOException e){e.printStackTrace();}
while(st.hasMoreTokens()) line += " "+st.nextToken();
return line;
}
private int[] nextIntArray(int n) {
int[] a = new int[n];
for(int i = 0; i < n; i++) a[i] = nextInt();
return a;
}
private long[] nextLongArray(int n){
long[] a = new long[n];
for(int i = 0; i < n; i++) a[i] = nextLong();
return a;
}
private double[] nextDoubleArray(int n){
double[] a = new double[n];
for(int i = 0; i < n; i++) a[i] = nextDouble();
return a;
}
private char[][] nextGrid(int n, int m){
char[][] grid = new char[n][m];
for(int i = 0; i < n; i++) grid[i] = next().toCharArray();
return grid;
}
private void sort(int arr[])
{
int cnt[]=new int[(1<<16)+1];
int ys[]=new int[arr.length];
for(int j=0;j<=16;j+=16){
Arrays.fill(cnt,0);
for(int x:arr){cnt[(x>>j&0xFFFF)+1]++;}
for(int i=1;i<cnt.length;i++){cnt[i]+=cnt[i-1];}
for(int x:arr){ys[cnt[x>>j&0xFFFF]++]=x;}
{ final int t[]=arr;arr=ys;ys=t;}
}
if(arr[0]<0||arr[arr.length-1]>=0)return;
int i,j,c;
for(i=arr.length-1,c=0;arr[i]<0;i--,c++){ys[c]=arr[i];}
for(j=arr.length-1;i>=0;i--,j--){arr[j]=arr[i];}
for(i=c-1;i>=0;i--){arr[i]=ys[c-1-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 sys
stuff = [int(i) for i in raw_input().split()]
n = stuff[0]
k = stuff[1]
if k > n:
print n
sys.exit()
powers = [int(i) for i in raw_input().split()]
scores = [0]*(n+1)
while True:
if powers[0] > powers[1]:
x = powers[1]
powers.pop(1)
powers.append(x)
scores[powers[0]] += 1
else:
x = powers[0]
powers.pop(0)
powers.append(x)
scores[powers[0]] += 1
if scores[powers[0]] == k:
print powers[0]
sys.exit()
| PYTHON |
879_B. Table Tennis | n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner.
For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner.
Input
The first line contains two integers: n and k (2 β€ n β€ 500, 2 β€ k β€ 1012) β the number of people and the number of wins.
The second line contains n integers a1, a2, ..., an (1 β€ ai β€ n) β powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct.
Output
Output a single integer β power of the winner.
Examples
Input
2 2
1 2
Output
2
Input
4 2
3 1 2 4
Output
3
Input
6 2
6 5 3 1 2 4
Output
6
Input
2 10000000000
2 1
Output
2
Note
Games in the second sample:
3 plays with 1. 3 wins. 1 goes to the end of the line.
3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner. | 2 | 8 | # -*- coding: utf-8 -*-
def writeline(l): print " ".join(map(str, l))
def readline(): return map(int, raw_input().split())
def readint(): return int(raw_input(), 10)
def main():
n, k = readline()
if k >= n - 1:
print n
else:
best = 0 # < any
wins = None
for a in readline():
if a > best:
best = a
wins = 1 if wins is not None else 0
else:
wins += 1
if wins == k:
print best
return
print n
main()
| 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 | nk = list(map(int,input().split()))
n = nk[0]
k = nk[1]
powers = list(map(int, input().split()))
p1 = powers.pop(0)
p2 = powers.pop(0)
w = 0
if k > 10000:
k = 10000
while w < k:
if p1 > p2:
w += 1
powers.append(p2)
p2 = powers.pop(0)
elif p1 < p2:
powers.append(p1)
p1 = p2
p2 = powers.pop(0)
w = 1
print(p1) | PYTHON3 |
879_B. Table Tennis | n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner.
For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner.
Input
The first line contains two integers: n and k (2 β€ n β€ 500, 2 β€ k β€ 1012) β the number of people and the number of wins.
The second line contains n integers a1, a2, ..., an (1 β€ ai β€ n) β powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct.
Output
Output a single integer β power of the winner.
Examples
Input
2 2
1 2
Output
2
Input
4 2
3 1 2 4
Output
3
Input
6 2
6 5 3 1 2 4
Output
6
Input
2 10000000000
2 1
Output
2
Note
Games in the second sample:
3 plays with 1. 3 wins. 1 goes to the end of the line.
3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner. | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
long long int n, k;
int arr[500], max = 0, cnt;
bool found = false;
cin >> n >> k;
for (int i = 0; i < n; i++) {
cin >> arr[i];
if (arr[i] > max) {
max = arr[i];
if (i != 0)
cnt = 1;
else
cnt = 0;
}
if (max != arr[i]) cnt++;
if (cnt == k) {
cout << max;
found = true;
return 0;
}
}
if (!found) cout << max;
}
| 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()))
if n <= k:
k = n-1
winner = 0
count = 0
for i in range(1, n):
if count == k:
break
new = max(powers[i], powers[winner])
if powers[winner] == new:
count += 1
else:
count = 1
winner = i
print(powers[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.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Tennis
{
private int peopleCount;
private long wins;
private List<Integer> powers;
private int maxPower;
//Constructor
public Tennis()
{
this.takeInput();
System.out.println(this.getWinner());
}
//Private Methods
private void takeInput()
{
this.maxPower = 0;
this.powers = new ArrayList<Integer>();
Scanner sc = new Scanner(System.in);
this.peopleCount = sc.nextInt();
this.wins = sc.nextLong();
sc.nextLine();
for (int i=0; i<this.peopleCount; i++)
{
int p = sc.nextInt();
this.powers.add(p);
if (p > this.maxPower)
{
this.maxPower = p;
}
}
sc.close();
}
private int getWinner()
{
int winner = -1;
long winStreak = 0;
while(winStreak < this.wins)
{
int p1 = this.powers.get(0);
int p2 = this.powers.get(1);
if (p1 > p2)
{
if (winner != p1)
{
winStreak = 1;
winner = p1;
}
else
{
winStreak++;
}
this.powers.remove(1);
this.powers.add(p2);
}
else
{
if (winner != p2)
{
winStreak = 1;
winner = p2;
}
else
{
winStreak++;
}
this.powers.remove(0);
this.powers.add(p1);
}
if (this.powers.get(0) == this.maxPower)
{
break;
}
}
return winner;
}
public static void main (String args[])
{
new Tennis();
}
}
| JAVA |
879_B. Table Tennis | n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner.
For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner.
Input
The first line contains two integers: n and k (2 β€ n β€ 500, 2 β€ k β€ 1012) β the number of people and the number of wins.
The second line contains n integers a1, a2, ..., an (1 β€ ai β€ n) β powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct.
Output
Output a single integer β power of the winner.
Examples
Input
2 2
1 2
Output
2
Input
4 2
3 1 2 4
Output
3
Input
6 2
6 5 3 1 2 4
Output
6
Input
2 10000000000
2 1
Output
2
Note
Games in the second sample:
3 plays with 1. 3 wins. 1 goes to the end of the line.
3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner. | 2 | 8 | n, k = map(int, raw_input().split())
powers = map(int, raw_input().split())
cont = 0
player = powers[0]
for i in range(1,len(powers)):
# print player, cont, powers[i], 'flag'
if cont == k:
break
if player > powers[i]:
cont += 1
# print player, powers[i],
else:
player = powers[i]
cont = 1
# print player, powers[i], 'if'
print player
| PYTHON |
879_B. Table Tennis | n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner.
For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner.
Input
The first line contains two integers: n and k (2 β€ n β€ 500, 2 β€ k β€ 1012) β the number of people and the number of wins.
The second line contains n integers a1, a2, ..., an (1 β€ ai β€ n) β powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct.
Output
Output a single integer β power of the winner.
Examples
Input
2 2
1 2
Output
2
Input
4 2
3 1 2 4
Output
3
Input
6 2
6 5 3 1 2 4
Output
6
Input
2 10000000000
2 1
Output
2
Note
Games in the second sample:
3 plays with 1. 3 wins. 1 goes to the end of the line.
3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner. | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int main() {
int len;
int n[510];
long long int k, c = 0;
scanf("%d %lld", &len, &k);
for (int i = 0; i < len; i++) scanf("%d", &n[i]);
int max_ve = n[0], temp = n[0];
bool f = false;
for (int i = 1; i < len; i++) {
if (max_ve < n[i]) {
max_ve = n[i];
c = 1;
}
if (temp == max_ve)
f = true;
else {
temp = max_ve;
f = false;
}
if (f)
c++;
else
c = 1;
if (c == k) break;
}
printf("%d\n", max_ve);
return 0;
}
| CPP |
879_B. Table Tennis | n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner.
For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner.
Input
The first line contains two integers: n and k (2 β€ n β€ 500, 2 β€ k β€ 1012) β the number of people and the number of wins.
The second line contains n integers a1, a2, ..., an (1 β€ ai β€ n) β powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct.
Output
Output a single integer β power of the winner.
Examples
Input
2 2
1 2
Output
2
Input
4 2
3 1 2 4
Output
3
Input
6 2
6 5 3 1 2 4
Output
6
Input
2 10000000000
2 1
Output
2
Note
Games in the second sample:
3 plays with 1. 3 wins. 1 goes to the end of the line.
3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner. | 2 | 8 | def solve():
from collections import deque
N, K = map(int, input().split())
dq = deque((map(int, input().split())))
popleft, append = dq.popleft, dq.append
win = 0
cur = popleft()
while dq:
vs = popleft()
if cur > vs:
win += 1
append(vs)
else:
win = 1
append(cur)
cur = vs
if win == K or win > N:
print(cur)
break
if __name__ == "__main__":
solve() | PYTHON3 |
879_B. Table Tennis | n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner.
For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner.
Input
The first line contains two integers: n and k (2 β€ n β€ 500, 2 β€ k β€ 1012) β the number of people and the number of wins.
The second line contains n integers a1, a2, ..., an (1 β€ ai β€ n) β powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct.
Output
Output a single integer β power of the winner.
Examples
Input
2 2
1 2
Output
2
Input
4 2
3 1 2 4
Output
3
Input
6 2
6 5 3 1 2 4
Output
6
Input
2 10000000000
2 1
Output
2
Note
Games in the second sample:
3 plays with 1. 3 wins. 1 goes to the end of the line.
3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner. | 2 | 8 | def solve(players,round,queue) :
index = 1
currentStrongest = queue[0]
winStreak = 0
while index < players and winStreak < round :
if currentStrongest < queue[index] :
currentStrongest = queue[index]
winStreak = 1
else :
winStreak += 1
index += 1
return currentStrongest
b,g = list(map(int,input().split()))
seq = list(map(int,input().split()))
print (solve(b,g,seq))
| 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(i) for i in input().split()]
streak = 0
while streak < k:
if a[0] > a[1]:
val = a[1]
a.remove(val)
a.append(val)
streak += 1
if a[0] == n:
streak = k
else:
val = a[0]
a.remove(val)
a.append(val)
streak = 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 | # -*- coding: utf-8 -*-
# Baqir Khan
# Software Engineer (Backend)
n, k = map(int, input().split())
a = list(map(int, input().split()))
if a[0] > a[1]:
cur_max = a[0]
else:
cur_max = a[1]
cur_max_len = 1
for i in range(2, n):
if a[i] < cur_max:
cur_max_len += 1
if cur_max_len == k:
break
else:
cur_max = a[i]
cur_max_len = 1
print(cur_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 | n, k = map(int, raw_input().strip('\n').split())
a = map(int, raw_input().strip('\n').split())
count = 0
pre = 0
flag = 0
for i, x in enumerate(a):
if i == 0:
pre = x
else:
if x < pre:
count += 1
else:
count = 1
pre = x
#print pre, count
if count == k:
print pre
flag = 1
break
if not flag:
print pre | 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 sys
n,k=map(int,input().split())
a=list(map(int,input().split()))
if k>n:
print(n)
else:
curr=0
currpow=a[0]
if currpow==n:
print(n)
sys.exit()
for i in range(1,n):
if a[i]>currpow:
curr=1
currpow=a[i]
if currpow==n:
print(n)
sys.exit()
else:
curr+=1
if curr>=k:
print(currpow)
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 | #include <bits/stdc++.h>
using namespace std;
int main() {
long long n, k;
cin >> n >> k;
long long arr[n];
for (long long i = 0; i < n; i++) {
cin >> arr[i];
}
long long power = arr[0];
long long counter = 0;
long long p = 0;
for (long long j = p + 1; j < n; j++) {
if (arr[j] > arr[p] && counter < k) {
p = j;
power = arr[j];
counter = 1;
} else if (arr[j] < arr[p]) {
counter++;
if (counter >= k) {
break;
}
}
}
cout << power << endl;
return 0;
}
| CPP |
879_B. Table Tennis | n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner.
For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner.
Input
The first line contains two integers: n and k (2 β€ n β€ 500, 2 β€ k β€ 1012) β the number of people and the number of wins.
The second line contains n integers a1, a2, ..., an (1 β€ ai β€ n) β powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct.
Output
Output a single integer β power of the winner.
Examples
Input
2 2
1 2
Output
2
Input
4 2
3 1 2 4
Output
3
Input
6 2
6 5 3 1 2 4
Output
6
Input
2 10000000000
2 1
Output
2
Note
Games in the second sample:
3 plays with 1. 3 wins. 1 goes to the end of the line.
3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner. | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
struct node {
int data;
int win_time;
};
int main() {
vector<node> q;
int n;
long long k;
scanf("%d%lld", &n, &k);
int Max = 0;
for (int i = 0; i < n; i++) {
int now;
scanf("%d", &now);
Max = max(now, Max);
q.push_back(node{now, 0});
}
for (int i = 0; i < 1000; i++) {
if (q[0].data > q[1].data) {
q[0].win_time++;
if (q[0].win_time == k) return 0 * printf("%d\n", q[0].data);
node now = q[1];
q.erase(q.begin() + 1);
q.push_back(now);
} else {
q[1].win_time++;
if (q[1].win_time == k) return 0 * printf("%d\n", q[1].data);
node now = q[0];
q.erase(q.begin());
q.push_back(now);
}
}
return 0 * printf("%d\n", Max);
}
| 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 input().split()]
p = [int(x) for x in input().split()]
win = p[0]
count = 1
for i in range(1,len(p)):
if win < p[i]:
win = p[i]
count = 2
elif win > p[i] and count == k:
print(win)
break
else:
count = count + 1
else:
print(win)
| PYTHON3 |
879_B. Table Tennis | n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner.
For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner.
Input
The first line contains two integers: n and k (2 β€ n β€ 500, 2 β€ k β€ 1012) β the number of people and the number of wins.
The second line contains n integers a1, a2, ..., an (1 β€ ai β€ n) β powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct.
Output
Output a single integer β power of the winner.
Examples
Input
2 2
1 2
Output
2
Input
4 2
3 1 2 4
Output
3
Input
6 2
6 5 3 1 2 4
Output
6
Input
2 10000000000
2 1
Output
2
Note
Games in the second sample:
3 plays with 1. 3 wins. 1 goes to the end of the line.
3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner. | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int main() {
long long int n, k;
cin >> n >> k;
int d[n];
int t = 0, ma = -1;
for (int i = 0; i < n; i++) {
cin >> d[i];
if (d[i] > ma) {
ma = d[i];
t = i;
}
}
long long int p = d[0], l = 0;
for (int i = 1; i < t; i++) {
if (d[i] > p) {
l = 1;
p = d[i];
} else {
l++;
}
if (l == k) {
cout << p << endl;
return 0;
}
}
cout << ma << 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.lang.*;
import java.util.*;
public class Abhishek {
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
long n = Long.parseLong(sc.next());
long k = Long.parseLong(sc.next());
Queue<Long> players = new LinkedList<>();
long max = Long.MIN_VALUE;
for (int i = 0; i < n; i++) {
long a = sc.nextLong();
players.add(a);
max = Math.max(a,max);
}
if(k>=n-1){
System.out.println(max);
return;
}
long current = players.remove();
long c = 0;
while (c!=k){
long p2 = players.remove();
if(p2>current){
players.add(current);
current = p2;
c=1;
}else{
c++;
players.add(p2);
}
}
System.out.println(current);
}
} | JAVA |
879_B. Table Tennis | n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner.
For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner.
Input
The first line contains two integers: n and k (2 β€ n β€ 500, 2 β€ k β€ 1012) β the number of people and the number of wins.
The second line contains n integers a1, a2, ..., an (1 β€ ai β€ n) β powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct.
Output
Output a single integer β power of the winner.
Examples
Input
2 2
1 2
Output
2
Input
4 2
3 1 2 4
Output
3
Input
6 2
6 5 3 1 2 4
Output
6
Input
2 10000000000
2 1
Output
2
Note
Games in the second sample:
3 plays with 1. 3 wins. 1 goes to the end of the line.
3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner. | 2 | 8 | n, k = map(int, input().split())
powers = list(map(int, input().split()))
current_plyr_pwr = powers[0]
current_plyr_wn = 0
for i in range (1, n):
if powers[i] < current_plyr_pwr:
current_plyr_wn +=1
else:
current_plyr_pwr = powers[i]
current_plyr_wn =1
if current_plyr_wn == k:
break
print(current_plyr_pwr)
| 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;
vector<long long> vec;
int main() {
long long n, i, j, k, p, q, x, y;
cin >> n >> k;
for (i = 0; i < n; i++) {
cin >> p;
vec.push_back(p);
}
i = j = 0;
while (j < min(n, k)) {
if (vec[i] > vec[i + 1]) {
swap(vec[i], vec[i + 1]);
vec.push_back(vec[i]);
j++;
} else {
vec.push_back(vec[i]);
j = 1;
}
i++;
}
printf("%I64d\n", vec[i]);
return 0;
}
| CPP |
879_B. Table Tennis | n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner.
For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner.
Input
The first line contains two integers: n and k (2 β€ n β€ 500, 2 β€ k β€ 1012) β the number of people and the number of wins.
The second line contains n integers a1, a2, ..., an (1 β€ ai β€ n) β powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct.
Output
Output a single integer β power of the winner.
Examples
Input
2 2
1 2
Output
2
Input
4 2
3 1 2 4
Output
3
Input
6 2
6 5 3 1 2 4
Output
6
Input
2 10000000000
2 1
Output
2
Note
Games in the second sample:
3 plays with 1. 3 wins. 1 goes to the end of the line.
3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner. | 2 | 8 | n, k = list( map( int, input().split() ) )
p = list( map( int, input().split() ) )
def wins( i ):
tot = 0
j = max( 0, i-1 )
while tot < k:
if p[j] > p[i]:
return ( False )
else:
tot += 1
j += 1
j %= n
return ( tot == k )
if n <= k:
print( max( p ) )
else:
i = 0
ans = 0
while i < n:
j = i+1
fl = False
tot = 0
if i > 0:
tot = 1
if j < n:
while p[i] > p[j] and j+1 < n:
j += 1
tot += 1
fl = True
if tot >= k and fl:
print( p[i] )
exit(0)
i = max( j, i+1 )
print( max(p) ) | PYTHON3 |
902_D. GCD of Polynomials | Suppose you have two polynomials <image> and <image>. Then polynomial <image> can be uniquely represented in the following way:
<image>
This can be done using [long division](https://en.wikipedia.org/wiki/Polynomial_long_division). Here, <image> denotes the degree of polynomial P(x). <image> is called the remainder of division of polynomial <image> by polynomial <image>, it is also denoted as <image>.
Since there is a way to divide polynomials with remainder, we can define Euclid's algorithm of finding the greatest common divisor of two polynomials. The algorithm takes two polynomials <image>. If the polynomial <image> is zero, the result is <image>, otherwise the result is the value the algorithm returns for pair <image>. On each step the degree of the second argument decreases, so the algorithm works in finite number of steps. But how large that number could be? You are to answer this question.
You are given an integer n. You have to build two polynomials with degrees not greater than n, such that their coefficients are integers not exceeding 1 by their absolute value, the leading coefficients (ones with the greatest power of x) are equal to one, and the described Euclid's algorithm performs exactly n steps finding their greatest common divisor. Moreover, the degree of the first polynomial should be greater than the degree of the second. By a step of the algorithm we mean the transition from pair <image> to pair <image>.
Input
You are given a single integer n (1 β€ n β€ 150) β the number of steps of the algorithm you need to reach.
Output
Print two polynomials in the following format.
In the first line print a single integer m (0 β€ m β€ n) β the degree of the polynomial.
In the second line print m + 1 integers between - 1 and 1 β the coefficients of the polynomial, from constant to leading.
The degree of the first polynomial should be greater than the degree of the second polynomial, the leading coefficients should be equal to 1. Euclid's algorithm should perform exactly n steps when called using these polynomials.
If there is no answer for the given n, print -1.
If there are multiple answer, print any of them.
Examples
Input
1
Output
1
0 1
0
1
Input
2
Output
2
-1 0 1
1
0 1
Note
In the second example you can print polynomials x2 - 1 and x. The sequence of transitions is
(x2 - 1, x) β (x, - 1) β ( - 1, 0).
There are two steps in it. | 2 | 10 | class polynomial:
def __init__(self, data):
self.data = data
def __lshift__(self, x):
return polynomial([0] * x + self.data)
def __len__(self):
return len(self.data)
def __sub__(self, other):
newData = [y - x for y, x in zip(self.data, other.data + [0] * 1000)]
while len(newData) > 0 and newData[-1] == 0:
del newData[-1]
return polynomial(newData)
def __add__(self, other):
newData = [(y + x) % 2 for y, x in zip(self.data + [0] * 1000, other.data + [0] * 1000)]
while len(newData) > 0 and newData[-1] == 0:
del newData[-1]
return polynomial(newData)
def __mul__(self, amt):
return polynomial([x * amt for x in self.data])
def __getitem__(self, idx):
return self.data[idx]
def __mod__(self, other):
tmp = self
times = 0
while len(tmp) >= len(other):
times += 1
if times > 1000:
print(*tmp.data)
print(*other.data)
exit(0)
tmp = tmp - (other << (len(tmp) - len(other))) * (tmp[-1] // other[-1])
return tmp
def gcdSteps(p1, p2, steps=0):
# print(*p1.data)
if len(p1) == 0 or len(p2) == 0:
return steps
else:
return gcdSteps(p2, p1 % p2, steps + 1)
a, b = polynomial([0, 1]), polynomial([1])
# x = b + (a << 1)
for i in range(int(input()) - 1):
# print(gcdSteps(a, b))
if gcdSteps(a, b) != i + 1:
print("error", i)
a, b = b + (a << 1), a
print(len(a) - 1)
print(*a.data)
print(len(b) - 1)
print(*b.data)
# print(gcdSteps(x, a)) | PYTHON3 |
902_D. GCD of Polynomials | Suppose you have two polynomials <image> and <image>. Then polynomial <image> can be uniquely represented in the following way:
<image>
This can be done using [long division](https://en.wikipedia.org/wiki/Polynomial_long_division). Here, <image> denotes the degree of polynomial P(x). <image> is called the remainder of division of polynomial <image> by polynomial <image>, it is also denoted as <image>.
Since there is a way to divide polynomials with remainder, we can define Euclid's algorithm of finding the greatest common divisor of two polynomials. The algorithm takes two polynomials <image>. If the polynomial <image> is zero, the result is <image>, otherwise the result is the value the algorithm returns for pair <image>. On each step the degree of the second argument decreases, so the algorithm works in finite number of steps. But how large that number could be? You are to answer this question.
You are given an integer n. You have to build two polynomials with degrees not greater than n, such that their coefficients are integers not exceeding 1 by their absolute value, the leading coefficients (ones with the greatest power of x) are equal to one, and the described Euclid's algorithm performs exactly n steps finding their greatest common divisor. Moreover, the degree of the first polynomial should be greater than the degree of the second. By a step of the algorithm we mean the transition from pair <image> to pair <image>.
Input
You are given a single integer n (1 β€ n β€ 150) β the number of steps of the algorithm you need to reach.
Output
Print two polynomials in the following format.
In the first line print a single integer m (0 β€ m β€ n) β the degree of the polynomial.
In the second line print m + 1 integers between - 1 and 1 β the coefficients of the polynomial, from constant to leading.
The degree of the first polynomial should be greater than the degree of the second polynomial, the leading coefficients should be equal to 1. Euclid's algorithm should perform exactly n steps when called using these polynomials.
If there is no answer for the given n, print -1.
If there are multiple answer, print any of them.
Examples
Input
1
Output
1
0 1
0
1
Input
2
Output
2
-1 0 1
1
0 1
Note
In the second example you can print polynomials x2 - 1 and x. The sequence of transitions is
(x2 - 1, x) β (x, - 1) β ( - 1, 0).
There are two steps in it. | 2 | 10 | n = int(input())
A = [0 for i in range(n + 1)]
B = [0 for i in range(n + 1)]
A[0] = 1
for i in range(n):
temp = A[:]
for j in reversed(range(1, n + 1)):
A[j] = (A[j - 1] + B[j]) % 2
A[0] = B[0]
B = temp
print(n)
for i in range(n):
print(A[i], end=' ')
print(A[n])
print(n - 1)
for i in range(n):
print(B[i], end=' ')
| PYTHON3 |
902_D. GCD of Polynomials | Suppose you have two polynomials <image> and <image>. Then polynomial <image> can be uniquely represented in the following way:
<image>
This can be done using [long division](https://en.wikipedia.org/wiki/Polynomial_long_division). Here, <image> denotes the degree of polynomial P(x). <image> is called the remainder of division of polynomial <image> by polynomial <image>, it is also denoted as <image>.
Since there is a way to divide polynomials with remainder, we can define Euclid's algorithm of finding the greatest common divisor of two polynomials. The algorithm takes two polynomials <image>. If the polynomial <image> is zero, the result is <image>, otherwise the result is the value the algorithm returns for pair <image>. On each step the degree of the second argument decreases, so the algorithm works in finite number of steps. But how large that number could be? You are to answer this question.
You are given an integer n. You have to build two polynomials with degrees not greater than n, such that their coefficients are integers not exceeding 1 by their absolute value, the leading coefficients (ones with the greatest power of x) are equal to one, and the described Euclid's algorithm performs exactly n steps finding their greatest common divisor. Moreover, the degree of the first polynomial should be greater than the degree of the second. By a step of the algorithm we mean the transition from pair <image> to pair <image>.
Input
You are given a single integer n (1 β€ n β€ 150) β the number of steps of the algorithm you need to reach.
Output
Print two polynomials in the following format.
In the first line print a single integer m (0 β€ m β€ n) β the degree of the polynomial.
In the second line print m + 1 integers between - 1 and 1 β the coefficients of the polynomial, from constant to leading.
The degree of the first polynomial should be greater than the degree of the second polynomial, the leading coefficients should be equal to 1. Euclid's algorithm should perform exactly n steps when called using these polynomials.
If there is no answer for the given n, print -1.
If there are multiple answer, print any of them.
Examples
Input
1
Output
1
0 1
0
1
Input
2
Output
2
-1 0 1
1
0 1
Note
In the second example you can print polynomials x2 - 1 and x. The sequence of transitions is
(x2 - 1, x) β (x, - 1) β ( - 1, 0).
There are two steps in it. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
long long al[200][200] = {};
long long ar[200][200] = {};
int main() {
al[1][0] = 0;
al[1][1] = 1;
ar[1][0] = 1;
al[2][0] = -1;
al[2][1] = 0;
al[2][2] = 1;
ar[2][0] = 0;
ar[2][1] = 1;
int n;
cin >> n;
for (int i = 3; i < 160; ++i) {
for (int j = 0; j < 160; ++j) {
ar[i][j] = al[i - 1][j];
}
for (int j = 0; j < 160; ++j) {
al[i][j + 1] = al[i - 1][j];
}
for (int j = 0; j < 160; ++j) {
al[i][j] += ar[i - 1][j];
}
bool hasTwo = false;
for (int j = 0; j < 160; ++j) {
if (al[i][j] != 0 && al[i][j] != 1 && al[i][j] != -1) {
hasTwo = true;
}
}
if (!hasTwo) {
continue;
}
for (int j = 0; j < 160; ++j) {
al[i][j] -= 2 * ar[i - 1][j];
}
}
cout << n << endl;
cout << al[n][0];
for (int i = 1; i <= n; ++i) {
cout << " " << al[n][i];
}
cout << endl;
cout << n - 1 << endl;
cout << ar[n][0];
for (int i = 1; i <= n - 1; ++i) {
cout << " " << ar[n][i];
}
cout << endl;
}
| CPP |
902_D. GCD of Polynomials | Suppose you have two polynomials <image> and <image>. Then polynomial <image> can be uniquely represented in the following way:
<image>
This can be done using [long division](https://en.wikipedia.org/wiki/Polynomial_long_division). Here, <image> denotes the degree of polynomial P(x). <image> is called the remainder of division of polynomial <image> by polynomial <image>, it is also denoted as <image>.
Since there is a way to divide polynomials with remainder, we can define Euclid's algorithm of finding the greatest common divisor of two polynomials. The algorithm takes two polynomials <image>. If the polynomial <image> is zero, the result is <image>, otherwise the result is the value the algorithm returns for pair <image>. On each step the degree of the second argument decreases, so the algorithm works in finite number of steps. But how large that number could be? You are to answer this question.
You are given an integer n. You have to build two polynomials with degrees not greater than n, such that their coefficients are integers not exceeding 1 by their absolute value, the leading coefficients (ones with the greatest power of x) are equal to one, and the described Euclid's algorithm performs exactly n steps finding their greatest common divisor. Moreover, the degree of the first polynomial should be greater than the degree of the second. By a step of the algorithm we mean the transition from pair <image> to pair <image>.
Input
You are given a single integer n (1 β€ n β€ 150) β the number of steps of the algorithm you need to reach.
Output
Print two polynomials in the following format.
In the first line print a single integer m (0 β€ m β€ n) β the degree of the polynomial.
In the second line print m + 1 integers between - 1 and 1 β the coefficients of the polynomial, from constant to leading.
The degree of the first polynomial should be greater than the degree of the second polynomial, the leading coefficients should be equal to 1. Euclid's algorithm should perform exactly n steps when called using these polynomials.
If there is no answer for the given n, print -1.
If there are multiple answer, print any of them.
Examples
Input
1
Output
1
0 1
0
1
Input
2
Output
2
-1 0 1
1
0 1
Note
In the second example you can print polynomials x2 - 1 and x. The sequence of transitions is
(x2 - 1, x) β (x, - 1) β ( - 1, 0).
There are two steps in it. | 2 | 10 | import java.util.*;
public class d {
public static void main(String[] args) {
int[][] res = new int[151][];
res[0] = new int[]{1};
res[1] = new int[]{0, 1};
res[2] = new int[]{-1, 0, 1};
for (int i=3; i<=150; i++) {
res[i] = new int[i+1];
for (int j=1; j<i+1; j++)
res[i][j] = res[i-1][j-1];
int[] tmp = Arrays.copyOf(res[i], i+1);
for (int j=0; j<i-1; j++)
res[i][j] += res[i-2][j];
for (int j=0; j<i-1; j++)
tmp[j] -= res[i-2][j];
if (pass(tmp)) res[i] = tmp;
}
Scanner stdin = new Scanner(System.in);
int n = stdin.nextInt();
for (int i=n; i>=n-1; i--) {
System.out.println(i);
print(res[i]);
}
}
public static void print(int[] a) {
for (int i=0; i<a.length-1; i++)
System.out.print(a[i]+" ");
System.out.println(a[a.length-1]);
}
public static boolean pass(int[] a) {
for (int i=0; i<a.length; i++)
if (a[i] < -1 || a[i] > 1)
return false;
return true;
}
} | JAVA |
902_D. GCD of Polynomials | Suppose you have two polynomials <image> and <image>. Then polynomial <image> can be uniquely represented in the following way:
<image>
This can be done using [long division](https://en.wikipedia.org/wiki/Polynomial_long_division). Here, <image> denotes the degree of polynomial P(x). <image> is called the remainder of division of polynomial <image> by polynomial <image>, it is also denoted as <image>.
Since there is a way to divide polynomials with remainder, we can define Euclid's algorithm of finding the greatest common divisor of two polynomials. The algorithm takes two polynomials <image>. If the polynomial <image> is zero, the result is <image>, otherwise the result is the value the algorithm returns for pair <image>. On each step the degree of the second argument decreases, so the algorithm works in finite number of steps. But how large that number could be? You are to answer this question.
You are given an integer n. You have to build two polynomials with degrees not greater than n, such that their coefficients are integers not exceeding 1 by their absolute value, the leading coefficients (ones with the greatest power of x) are equal to one, and the described Euclid's algorithm performs exactly n steps finding their greatest common divisor. Moreover, the degree of the first polynomial should be greater than the degree of the second. By a step of the algorithm we mean the transition from pair <image> to pair <image>.
Input
You are given a single integer n (1 β€ n β€ 150) β the number of steps of the algorithm you need to reach.
Output
Print two polynomials in the following format.
In the first line print a single integer m (0 β€ m β€ n) β the degree of the polynomial.
In the second line print m + 1 integers between - 1 and 1 β the coefficients of the polynomial, from constant to leading.
The degree of the first polynomial should be greater than the degree of the second polynomial, the leading coefficients should be equal to 1. Euclid's algorithm should perform exactly n steps when called using these polynomials.
If there is no answer for the given n, print -1.
If there are multiple answer, print any of them.
Examples
Input
1
Output
1
0 1
0
1
Input
2
Output
2
-1 0 1
1
0 1
Note
In the second example you can print polynomials x2 - 1 and x. The sequence of transitions is
(x2 - 1, x) β (x, - 1) β ( - 1, 0).
There are two steps in it. | 2 | 10 | import java.util.Arrays;
import java.util.Scanner;
public class d {
public static int bb = 0;
public static int ee = 9;
public static void main(String[] Args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[][] vs = new int[n + 2][n + 2];
vs[0][0] = 1;
vs[1][1] = 1;
// vs[2][0] = -1;
// vs[2][2] = 1;
// for (int i = 2; i < n; i++) {
// vs[i] = gen(vs[i - 1], vs[i], n + 2, i);
// }
if (n == 1) {
System.out.println(1);
System.out.println("0 1");
System.out.println(0);
System.out.println(1);
return;
}
if (!genb(vs[0], vs[1], n + 2, 1))
System.out.println(-1);
}
public static int[] gen(int[] a, int[] b, int len, int i) {
int[] ret = new int[len];
for (int k = bb; k <= ee; k++) {
int t1 = (k % 3) - 1;
int t2 = (k / 3) - 1;
for (int j = 0; j < len; j++) {
ret[j] = a[j] + b[j] * t1;
if (j > 0)
ret[j] += b[j - 1] * t2;
}
boolean good = (ret[i + 1] != 0);
for (int j = 0; j < len; j++) {
if (ret[j] > 1 || ret[j] < -1) {
good = false;
}
}
if (good) {
System.out.println(Arrays.toString(ret) + " " + k);
// break;
}
if (!good && k == ee)
System.out.println("BAD " + (i + 1));
}
return ret;
}
public static boolean genb(int[] a, int[] b, int len, int i) {
if (i == len - 2) {
System.out.println(i);
for (int j = 0; j <= i; j++)
System.out.print(b[j] * b[i] + " ");
System.out.println();
System.out.println(i - 1);
for (int j = 0; j < i; j++)
System.out.print(a[j] * a[i - 1] + " ");
System.out.println();
return true;
}
int[] ret = new int[len];
for (int k = bb; k <= ee; k++) {
int t1 = (k % 3) - 1;
int t2 = (k / 3) - 1;
for (int j = 0; j < len; j++) {
ret[j] = a[j] + b[j] * t1;
if (j > 0)
ret[j] += b[j - 1] * t2;
}
boolean good = (ret[i + 1] != 0);
for (int j = 0; j < len; j++) {
if (ret[j] > 1 || ret[j] < -1) {
good = false;
}
}
if (good) {
// for (int j = 0; j < i; j++)
// System.out.print(" ");
// System.out.println(i + " " + len);
if (genb(b, ret, len, i + 1)) {
return true;
}
}
}
return false;
}
}
| JAVA |
902_D. GCD of Polynomials | Suppose you have two polynomials <image> and <image>. Then polynomial <image> can be uniquely represented in the following way:
<image>
This can be done using [long division](https://en.wikipedia.org/wiki/Polynomial_long_division). Here, <image> denotes the degree of polynomial P(x). <image> is called the remainder of division of polynomial <image> by polynomial <image>, it is also denoted as <image>.
Since there is a way to divide polynomials with remainder, we can define Euclid's algorithm of finding the greatest common divisor of two polynomials. The algorithm takes two polynomials <image>. If the polynomial <image> is zero, the result is <image>, otherwise the result is the value the algorithm returns for pair <image>. On each step the degree of the second argument decreases, so the algorithm works in finite number of steps. But how large that number could be? You are to answer this question.
You are given an integer n. You have to build two polynomials with degrees not greater than n, such that their coefficients are integers not exceeding 1 by their absolute value, the leading coefficients (ones with the greatest power of x) are equal to one, and the described Euclid's algorithm performs exactly n steps finding their greatest common divisor. Moreover, the degree of the first polynomial should be greater than the degree of the second. By a step of the algorithm we mean the transition from pair <image> to pair <image>.
Input
You are given a single integer n (1 β€ n β€ 150) β the number of steps of the algorithm you need to reach.
Output
Print two polynomials in the following format.
In the first line print a single integer m (0 β€ m β€ n) β the degree of the polynomial.
In the second line print m + 1 integers between - 1 and 1 β the coefficients of the polynomial, from constant to leading.
The degree of the first polynomial should be greater than the degree of the second polynomial, the leading coefficients should be equal to 1. Euclid's algorithm should perform exactly n steps when called using these polynomials.
If there is no answer for the given n, print -1.
If there are multiple answer, print any of them.
Examples
Input
1
Output
1
0 1
0
1
Input
2
Output
2
-1 0 1
1
0 1
Note
In the second example you can print polynomials x2 - 1 and x. The sequence of transitions is
(x2 - 1, x) β (x, - 1) β ( - 1, 0).
There are two steps in it. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
int ans1[505][505], ans2[505][505], p, x = -1, y = -1;
int main() {
int n;
scanf("%d", &n);
ans1[1][1] = 1;
ans2[1][0] = 1;
for (int i = 2; i <= n; i++) {
p = 0;
for (int j = 0; j <= 200; j++) {
if (j) {
if (!p) {
ans1[i][j] = ans1[i - 1][j - 1] + ans2[i - 1][j];
if (ans1[i][j] > 1 || ans1[i][j] < -1) {
j = 0;
p = 1;
continue;
}
} else {
ans1[i][j] = -ans1[i - 1][j - 1] + ans2[i - 1][j];
}
} else
ans1[i][j] = ans2[i - 1][j];
ans2[i][j] = ans1[i - 1][j];
}
}
for (int i = 200; i >= 0; i--) {
if (ans1[n][i] && x == -1) x = i;
if (ans2[n][i] && y == -1) y = i;
}
int kk = ans1[n][x];
int pp = ans2[n][y];
printf("%d\n", x);
for (int i = 0; i <= x; i++) {
printf("%d", ans1[n][i] * kk);
if (i == x)
puts("");
else
printf(" ");
}
printf("%d\n", y);
for (int i = 0; i <= y; i++) {
printf("%d", ans2[n][i] * pp);
if (i == y)
puts("");
else
printf(" ");
}
}
| CPP |
902_D. GCD of Polynomials | Suppose you have two polynomials <image> and <image>. Then polynomial <image> can be uniquely represented in the following way:
<image>
This can be done using [long division](https://en.wikipedia.org/wiki/Polynomial_long_division). Here, <image> denotes the degree of polynomial P(x). <image> is called the remainder of division of polynomial <image> by polynomial <image>, it is also denoted as <image>.
Since there is a way to divide polynomials with remainder, we can define Euclid's algorithm of finding the greatest common divisor of two polynomials. The algorithm takes two polynomials <image>. If the polynomial <image> is zero, the result is <image>, otherwise the result is the value the algorithm returns for pair <image>. On each step the degree of the second argument decreases, so the algorithm works in finite number of steps. But how large that number could be? You are to answer this question.
You are given an integer n. You have to build two polynomials with degrees not greater than n, such that their coefficients are integers not exceeding 1 by their absolute value, the leading coefficients (ones with the greatest power of x) are equal to one, and the described Euclid's algorithm performs exactly n steps finding their greatest common divisor. Moreover, the degree of the first polynomial should be greater than the degree of the second. By a step of the algorithm we mean the transition from pair <image> to pair <image>.
Input
You are given a single integer n (1 β€ n β€ 150) β the number of steps of the algorithm you need to reach.
Output
Print two polynomials in the following format.
In the first line print a single integer m (0 β€ m β€ n) β the degree of the polynomial.
In the second line print m + 1 integers between - 1 and 1 β the coefficients of the polynomial, from constant to leading.
The degree of the first polynomial should be greater than the degree of the second polynomial, the leading coefficients should be equal to 1. Euclid's algorithm should perform exactly n steps when called using these polynomials.
If there is no answer for the given n, print -1.
If there are multiple answer, print any of them.
Examples
Input
1
Output
1
0 1
0
1
Input
2
Output
2
-1 0 1
1
0 1
Note
In the second example you can print polynomials x2 - 1 and x. The sequence of transitions is
(x2 - 1, x) β (x, - 1) β ( - 1, 0).
There are two steps in it. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
int fun[200][200];
int main(void) {
fun[0][0] = 1;
fun[1][0] = 0;
fun[1][1] = 1;
int now = 2;
int flag = 150;
while (flag--) {
for (int i = 0; i <= now; ++i) fun[now][i] = fun[(now - 2)][i];
for (int i = 1; i <= now; ++i)
fun[now][i] += fun[(now - 1)][i - 1], fun[now][i] %= 2;
now++;
}
int n;
cin >> n;
if (n > 2) {
cout << n << endl;
for (int i = 0; i <= n; ++i) cout << fun[n][i] << " ";
cout << endl;
cout << n - 1 << endl;
for (int i = 0; i < n; ++i) cout << fun[n - 1][i] << " ";
} else {
if (n == 1)
printf("1\n0 1\n0\n1");
else
printf("2\n-1 0 1\n1\n0 1");
}
return 0;
}
| CPP |
902_D. GCD of Polynomials | Suppose you have two polynomials <image> and <image>. Then polynomial <image> can be uniquely represented in the following way:
<image>
This can be done using [long division](https://en.wikipedia.org/wiki/Polynomial_long_division). Here, <image> denotes the degree of polynomial P(x). <image> is called the remainder of division of polynomial <image> by polynomial <image>, it is also denoted as <image>.
Since there is a way to divide polynomials with remainder, we can define Euclid's algorithm of finding the greatest common divisor of two polynomials. The algorithm takes two polynomials <image>. If the polynomial <image> is zero, the result is <image>, otherwise the result is the value the algorithm returns for pair <image>. On each step the degree of the second argument decreases, so the algorithm works in finite number of steps. But how large that number could be? You are to answer this question.
You are given an integer n. You have to build two polynomials with degrees not greater than n, such that their coefficients are integers not exceeding 1 by their absolute value, the leading coefficients (ones with the greatest power of x) are equal to one, and the described Euclid's algorithm performs exactly n steps finding their greatest common divisor. Moreover, the degree of the first polynomial should be greater than the degree of the second. By a step of the algorithm we mean the transition from pair <image> to pair <image>.
Input
You are given a single integer n (1 β€ n β€ 150) β the number of steps of the algorithm you need to reach.
Output
Print two polynomials in the following format.
In the first line print a single integer m (0 β€ m β€ n) β the degree of the polynomial.
In the second line print m + 1 integers between - 1 and 1 β the coefficients of the polynomial, from constant to leading.
The degree of the first polynomial should be greater than the degree of the second polynomial, the leading coefficients should be equal to 1. Euclid's algorithm should perform exactly n steps when called using these polynomials.
If there is no answer for the given n, print -1.
If there are multiple answer, print any of them.
Examples
Input
1
Output
1
0 1
0
1
Input
2
Output
2
-1 0 1
1
0 1
Note
In the second example you can print polynomials x2 - 1 and x. The sequence of transitions is
(x2 - 1, x) β (x, - 1) β ( - 1, 0).
There are two steps in it. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
int n, a[200][200];
int main() {
cin >> n;
a[0][0] = 1, a[1][1] = 1;
for (int i = 2; i <= 150; i++) {
bool check = 0;
for (int j = i + 1; j; j--)
a[i][j] = a[i - 1][j - 1] + a[i - 2][j], check |= (abs(a[i][j]) > 1);
a[i][0] = a[i - 2][0];
if (check) {
for (int j = i + 1; j; j--) a[i][j] = a[i - 1][j - 1] - a[i - 2][j];
a[i][0] = -a[i - 2][0];
}
}
cout << n << '\n';
for (int i = 0; i <= n; i++) cout << a[n][i] << ' ';
cout << '\n' << n - 1 << '\n';
for (int i = 0; i < n; i++) cout << a[n - 1][i] << ' ';
cout << '\n';
}
| CPP |
902_D. GCD of Polynomials | Suppose you have two polynomials <image> and <image>. Then polynomial <image> can be uniquely represented in the following way:
<image>
This can be done using [long division](https://en.wikipedia.org/wiki/Polynomial_long_division). Here, <image> denotes the degree of polynomial P(x). <image> is called the remainder of division of polynomial <image> by polynomial <image>, it is also denoted as <image>.
Since there is a way to divide polynomials with remainder, we can define Euclid's algorithm of finding the greatest common divisor of two polynomials. The algorithm takes two polynomials <image>. If the polynomial <image> is zero, the result is <image>, otherwise the result is the value the algorithm returns for pair <image>. On each step the degree of the second argument decreases, so the algorithm works in finite number of steps. But how large that number could be? You are to answer this question.
You are given an integer n. You have to build two polynomials with degrees not greater than n, such that their coefficients are integers not exceeding 1 by their absolute value, the leading coefficients (ones with the greatest power of x) are equal to one, and the described Euclid's algorithm performs exactly n steps finding their greatest common divisor. Moreover, the degree of the first polynomial should be greater than the degree of the second. By a step of the algorithm we mean the transition from pair <image> to pair <image>.
Input
You are given a single integer n (1 β€ n β€ 150) β the number of steps of the algorithm you need to reach.
Output
Print two polynomials in the following format.
In the first line print a single integer m (0 β€ m β€ n) β the degree of the polynomial.
In the second line print m + 1 integers between - 1 and 1 β the coefficients of the polynomial, from constant to leading.
The degree of the first polynomial should be greater than the degree of the second polynomial, the leading coefficients should be equal to 1. Euclid's algorithm should perform exactly n steps when called using these polynomials.
If there is no answer for the given n, print -1.
If there are multiple answer, print any of them.
Examples
Input
1
Output
1
0 1
0
1
Input
2
Output
2
-1 0 1
1
0 1
Note
In the second example you can print polynomials x2 - 1 and x. The sequence of transitions is
(x2 - 1, x) β (x, - 1) β ( - 1, 0).
There are two steps in it. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
vector<int> mult(std::vector<int> V1, std::vector<int> V2) {
vector<int> res;
vector<int> v1 = V1;
vector<int> v2 = V2;
reverse((v1).begin(), (v1).end());
v1.push_back(0);
reverse((v1).begin(), (v1).end());
res.resize(v1.size());
while (v2.size() != v1.size()) {
v2.push_back(0);
}
for (int i = 0; i < (res.size()); ++i) {
res[i] = (v1[i] + v2[i]) % 2;
}
return res;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
vector<int> v1;
vector<int> v2;
vector<int> temp;
v1.push_back(0);
v1.push_back(1);
v2.push_back(1);
for (int i = 0; i < n - 1; i++) {
temp = v1;
v1 = mult(v1, v2);
v2 = temp;
}
cout << v1.size() - 1 << endl;
for (int i = 0; i < (v1.size()); ++i) cout << v1[i] << " ";
cout << endl;
cout << v2.size() - 1 << endl;
for (int i = 0; i < (v2.size()); ++i) cout << v2[i] << " ";
cout << endl;
return 0;
}
| CPP |
902_D. GCD of Polynomials | Suppose you have two polynomials <image> and <image>. Then polynomial <image> can be uniquely represented in the following way:
<image>
This can be done using [long division](https://en.wikipedia.org/wiki/Polynomial_long_division). Here, <image> denotes the degree of polynomial P(x). <image> is called the remainder of division of polynomial <image> by polynomial <image>, it is also denoted as <image>.
Since there is a way to divide polynomials with remainder, we can define Euclid's algorithm of finding the greatest common divisor of two polynomials. The algorithm takes two polynomials <image>. If the polynomial <image> is zero, the result is <image>, otherwise the result is the value the algorithm returns for pair <image>. On each step the degree of the second argument decreases, so the algorithm works in finite number of steps. But how large that number could be? You are to answer this question.
You are given an integer n. You have to build two polynomials with degrees not greater than n, such that their coefficients are integers not exceeding 1 by their absolute value, the leading coefficients (ones with the greatest power of x) are equal to one, and the described Euclid's algorithm performs exactly n steps finding their greatest common divisor. Moreover, the degree of the first polynomial should be greater than the degree of the second. By a step of the algorithm we mean the transition from pair <image> to pair <image>.
Input
You are given a single integer n (1 β€ n β€ 150) β the number of steps of the algorithm you need to reach.
Output
Print two polynomials in the following format.
In the first line print a single integer m (0 β€ m β€ n) β the degree of the polynomial.
In the second line print m + 1 integers between - 1 and 1 β the coefficients of the polynomial, from constant to leading.
The degree of the first polynomial should be greater than the degree of the second polynomial, the leading coefficients should be equal to 1. Euclid's algorithm should perform exactly n steps when called using these polynomials.
If there is no answer for the given n, print -1.
If there are multiple answer, print any of them.
Examples
Input
1
Output
1
0 1
0
1
Input
2
Output
2
-1 0 1
1
0 1
Note
In the second example you can print polynomials x2 - 1 and x. The sequence of transitions is
(x2 - 1, x) β (x, - 1) β ( - 1, 0).
There are two steps in it. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
int n;
int a[160], b[160], c[160];
int main() {
int times, i;
bool flag;
scanf("%d", &n);
a[0] = 1;
b[1] = 1;
for (times = 1; times < n; times++) {
c[0] = 0;
for (i = 1; i <= n; i++) c[i] = b[i - 1];
for (i = 0; i <= n; i++) c[i] += a[i];
flag = 1;
for (i = 0; i <= n; i++)
if (c[i] > 1 || c[i] < -1) flag = 0;
if (flag) {
for (i = 0; i <= n; i++) a[i] = b[i];
for (i = 0; i <= n; i++) b[i] = c[i];
continue;
}
c[0] = 0;
for (i = 1; i <= n; i++) c[i] = b[i - 1];
for (i = 0; i <= n; i++) c[i] -= a[i];
for (i = 0; i <= n; i++) a[i] = b[i];
for (i = 0; i <= n; i++) b[i] = c[i];
}
printf("%d\n", n);
for (i = 0; i <= n; i++) printf("%d ", b[i]);
printf("\n%d\n", n - 1);
for (i = 0; i <= n - 1; i++) printf("%d ", a[i]);
}
| CPP |
902_D. GCD of Polynomials | Suppose you have two polynomials <image> and <image>. Then polynomial <image> can be uniquely represented in the following way:
<image>
This can be done using [long division](https://en.wikipedia.org/wiki/Polynomial_long_division). Here, <image> denotes the degree of polynomial P(x). <image> is called the remainder of division of polynomial <image> by polynomial <image>, it is also denoted as <image>.
Since there is a way to divide polynomials with remainder, we can define Euclid's algorithm of finding the greatest common divisor of two polynomials. The algorithm takes two polynomials <image>. If the polynomial <image> is zero, the result is <image>, otherwise the result is the value the algorithm returns for pair <image>. On each step the degree of the second argument decreases, so the algorithm works in finite number of steps. But how large that number could be? You are to answer this question.
You are given an integer n. You have to build two polynomials with degrees not greater than n, such that their coefficients are integers not exceeding 1 by their absolute value, the leading coefficients (ones with the greatest power of x) are equal to one, and the described Euclid's algorithm performs exactly n steps finding their greatest common divisor. Moreover, the degree of the first polynomial should be greater than the degree of the second. By a step of the algorithm we mean the transition from pair <image> to pair <image>.
Input
You are given a single integer n (1 β€ n β€ 150) β the number of steps of the algorithm you need to reach.
Output
Print two polynomials in the following format.
In the first line print a single integer m (0 β€ m β€ n) β the degree of the polynomial.
In the second line print m + 1 integers between - 1 and 1 β the coefficients of the polynomial, from constant to leading.
The degree of the first polynomial should be greater than the degree of the second polynomial, the leading coefficients should be equal to 1. Euclid's algorithm should perform exactly n steps when called using these polynomials.
If there is no answer for the given n, print -1.
If there are multiple answer, print any of them.
Examples
Input
1
Output
1
0 1
0
1
Input
2
Output
2
-1 0 1
1
0 1
Note
In the second example you can print polynomials x2 - 1 and x. The sequence of transitions is
(x2 - 1, x) β (x, - 1) β ( - 1, 0).
There are two steps in it. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
const int maxn = 1e3;
int a[maxn + 10], b[maxn + 10], tmp[maxn + 10];
void work(int &lena, int &lenb) {
for (int i = 0; i < lena; i++) tmp[i + 1] = a[i];
tmp[0] = 0;
for (int i = 0; i < lenb; i++) tmp[i] += b[i];
bool flag = true;
for (int i = 0; i <= lena; i++)
if (tmp[i] > 1 || tmp[i] < -1) flag = false;
if (flag) {
for (int i = 0; i < lena; i++) b[i] = a[i];
lenb = lena;
lena++;
for (int i = 0; i < lena; i++) a[i] = tmp[i];
return;
}
for (int i = 0; i < lena; i++) tmp[i + 1] = -a[i];
tmp[0] = 0;
for (int i = 0; i < lenb; i++) tmp[i] += b[i];
for (int i = 0; i < lena; i++) b[i] = a[i];
lenb = lena;
lena++;
for (int i = 0; i < lena; i++) a[i] = tmp[i];
return;
}
int main() {
int n;
scanf("%d", &n);
int lena = 1, lenb = 0;
a[0] = 1;
while (n--) {
work(lena, lenb);
}
if (a[lena - 1] == -1)
for (int i = 0; i < lena; i++) a[i] = -a[i];
if (b[lenb - 1] == -1)
for (int i = 0; i < lenb; i++) b[i] = -b[i];
printf("%d\n", lena - 1);
for (int i = 0; i < lena; i++)
if (i == lena - 1)
printf("%d\n", a[i]);
else
printf("%d ", a[i]);
printf("%d\n", lenb - 1);
for (int i = 0; i < lenb; i++)
if (i == lenb - 1)
printf("%d\n", b[i]);
else
printf("%d ", b[i]);
return 0;
}
| CPP |
902_D. GCD of Polynomials | Suppose you have two polynomials <image> and <image>. Then polynomial <image> can be uniquely represented in the following way:
<image>
This can be done using [long division](https://en.wikipedia.org/wiki/Polynomial_long_division). Here, <image> denotes the degree of polynomial P(x). <image> is called the remainder of division of polynomial <image> by polynomial <image>, it is also denoted as <image>.
Since there is a way to divide polynomials with remainder, we can define Euclid's algorithm of finding the greatest common divisor of two polynomials. The algorithm takes two polynomials <image>. If the polynomial <image> is zero, the result is <image>, otherwise the result is the value the algorithm returns for pair <image>. On each step the degree of the second argument decreases, so the algorithm works in finite number of steps. But how large that number could be? You are to answer this question.
You are given an integer n. You have to build two polynomials with degrees not greater than n, such that their coefficients are integers not exceeding 1 by their absolute value, the leading coefficients (ones with the greatest power of x) are equal to one, and the described Euclid's algorithm performs exactly n steps finding their greatest common divisor. Moreover, the degree of the first polynomial should be greater than the degree of the second. By a step of the algorithm we mean the transition from pair <image> to pair <image>.
Input
You are given a single integer n (1 β€ n β€ 150) β the number of steps of the algorithm you need to reach.
Output
Print two polynomials in the following format.
In the first line print a single integer m (0 β€ m β€ n) β the degree of the polynomial.
In the second line print m + 1 integers between - 1 and 1 β the coefficients of the polynomial, from constant to leading.
The degree of the first polynomial should be greater than the degree of the second polynomial, the leading coefficients should be equal to 1. Euclid's algorithm should perform exactly n steps when called using these polynomials.
If there is no answer for the given n, print -1.
If there are multiple answer, print any of them.
Examples
Input
1
Output
1
0 1
0
1
Input
2
Output
2
-1 0 1
1
0 1
Note
In the second example you can print polynomials x2 - 1 and x. The sequence of transitions is
(x2 - 1, x) β (x, - 1) β ( - 1, 0).
There are two steps in it. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
const int MOD = 1e9 + 7;
int a[155], b[155], t[155], m;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
memset(a, 0, sizeof(a));
memset(b, 0, sizeof(b));
a[1] = 1;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i + 1; j++) t[j] = a[j];
for (int j = i + 1; j >= 1; j--) a[j] = a[j - 1];
for (int j = 1; j <= i; j++) {
a[j] += b[j];
if (a[j] == 2) a[j] = 0;
}
for (int j = 1; j <= i + 1; j++) b[j] = t[j];
}
cout << n << endl;
for (int i = 1; i <= n + 1; i++) {
if (i > 1) cout << ' ';
cout << a[i];
}
cout << endl;
cout << n - 1 << endl;
for (int i = 1; i <= n; i++) {
if (i > 1) cout << ' ';
cout << b[i];
}
cout << endl;
return 0;
}
| CPP |
902_D. GCD of Polynomials | Suppose you have two polynomials <image> and <image>. Then polynomial <image> can be uniquely represented in the following way:
<image>
This can be done using [long division](https://en.wikipedia.org/wiki/Polynomial_long_division). Here, <image> denotes the degree of polynomial P(x). <image> is called the remainder of division of polynomial <image> by polynomial <image>, it is also denoted as <image>.
Since there is a way to divide polynomials with remainder, we can define Euclid's algorithm of finding the greatest common divisor of two polynomials. The algorithm takes two polynomials <image>. If the polynomial <image> is zero, the result is <image>, otherwise the result is the value the algorithm returns for pair <image>. On each step the degree of the second argument decreases, so the algorithm works in finite number of steps. But how large that number could be? You are to answer this question.
You are given an integer n. You have to build two polynomials with degrees not greater than n, such that their coefficients are integers not exceeding 1 by their absolute value, the leading coefficients (ones with the greatest power of x) are equal to one, and the described Euclid's algorithm performs exactly n steps finding their greatest common divisor. Moreover, the degree of the first polynomial should be greater than the degree of the second. By a step of the algorithm we mean the transition from pair <image> to pair <image>.
Input
You are given a single integer n (1 β€ n β€ 150) β the number of steps of the algorithm you need to reach.
Output
Print two polynomials in the following format.
In the first line print a single integer m (0 β€ m β€ n) β the degree of the polynomial.
In the second line print m + 1 integers between - 1 and 1 β the coefficients of the polynomial, from constant to leading.
The degree of the first polynomial should be greater than the degree of the second polynomial, the leading coefficients should be equal to 1. Euclid's algorithm should perform exactly n steps when called using these polynomials.
If there is no answer for the given n, print -1.
If there are multiple answer, print any of them.
Examples
Input
1
Output
1
0 1
0
1
Input
2
Output
2
-1 0 1
1
0 1
Note
In the second example you can print polynomials x2 - 1 and x. The sequence of transitions is
(x2 - 1, x) β (x, - 1) β ( - 1, 0).
There are two steps in it. | 2 | 10 | import java.util.Scanner;
public class D {
private static int[][] p = new int[151][151];
static {
p[0][0] = 1;
p[1][1] = 1;
}
public static void main(String[] args) {
int n = new Scanner(System.in).nextInt();
for (int i = 2; i <= n; i++) {
p[i][0] = p[i-2][0];
for (int j = 1; j <= i; j++)
p[i][j] = (p[i-1][j-1] + p[i-2][j]) % 2;
}
print(n);
print(n-1);
}
private static void print(int n) {
System.out.println(n);
for (int i = 0; i <= n; i++)
System.out.print(p[n][i] + " ");
System.out.println();
}
}
| JAVA |
902_D. GCD of Polynomials | Suppose you have two polynomials <image> and <image>. Then polynomial <image> can be uniquely represented in the following way:
<image>
This can be done using [long division](https://en.wikipedia.org/wiki/Polynomial_long_division). Here, <image> denotes the degree of polynomial P(x). <image> is called the remainder of division of polynomial <image> by polynomial <image>, it is also denoted as <image>.
Since there is a way to divide polynomials with remainder, we can define Euclid's algorithm of finding the greatest common divisor of two polynomials. The algorithm takes two polynomials <image>. If the polynomial <image> is zero, the result is <image>, otherwise the result is the value the algorithm returns for pair <image>. On each step the degree of the second argument decreases, so the algorithm works in finite number of steps. But how large that number could be? You are to answer this question.
You are given an integer n. You have to build two polynomials with degrees not greater than n, such that their coefficients are integers not exceeding 1 by their absolute value, the leading coefficients (ones with the greatest power of x) are equal to one, and the described Euclid's algorithm performs exactly n steps finding their greatest common divisor. Moreover, the degree of the first polynomial should be greater than the degree of the second. By a step of the algorithm we mean the transition from pair <image> to pair <image>.
Input
You are given a single integer n (1 β€ n β€ 150) β the number of steps of the algorithm you need to reach.
Output
Print two polynomials in the following format.
In the first line print a single integer m (0 β€ m β€ n) β the degree of the polynomial.
In the second line print m + 1 integers between - 1 and 1 β the coefficients of the polynomial, from constant to leading.
The degree of the first polynomial should be greater than the degree of the second polynomial, the leading coefficients should be equal to 1. Euclid's algorithm should perform exactly n steps when called using these polynomials.
If there is no answer for the given n, print -1.
If there are multiple answer, print any of them.
Examples
Input
1
Output
1
0 1
0
1
Input
2
Output
2
-1 0 1
1
0 1
Note
In the second example you can print polynomials x2 - 1 and x. The sequence of transitions is
(x2 - 1, x) β (x, - 1) β ( - 1, 0).
There are two steps in it. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t = 1;
while (t--) {
int n, i, j, add = -1;
cin >> n;
vector<int> a, b, c;
a.push_back(1);
for (i = 0; i < n; i++) {
c = a;
a.push_back(0);
for (j = a.size() - 1; j > 0; j--) a[j] = a[j - 1];
a[0] = 0;
for (j = 0; j < b.size(); j++) {
a[j] += add * b[j];
if (abs(a[j]) > 1) a[j] = 0;
}
add = add * -1;
b = c;
}
cout << a.size() - 1 << "\n";
for (i = 0; i < a.size(); i++) cout << a[i] << " ";
cout << "\n" << b.size() - 1 << "\n";
for (i = 0; i < b.size(); i++) cout << b[i] << " ";
}
return 0;
}
| CPP |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.