ACM刷题之codeforce————Table Tennis

版权声明:本文为小时的枫原创文章,未经博主允许不得转载。 https://blog.csdn.net/xiaofeng187/article/details/79342702
Table Tennis
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner.

For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner.

Input

The first line contains two integers: n and k (2 ≤ n ≤ 5002 ≤ k ≤ 1012) — the number of people and the number of wins.

The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ n) — powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ai are distinct.

Output

Output a single integer — power of the winner.

Examples
input
Copy
2 2
1 2
output
2 
input
Copy
4 2
3 1 2 4
output
3 
input
Copy
6 2
6 5 3 1 2 4
output
6 
input
Copy
2 10000000000
2 1
output
2
Note

Games in the second sample:

3 plays with 13 wins. 1 goes to the end of the line.

3 plays with 23 wins. He wins twice in a row. He becomes the winner.


一道不难的题目,模拟一下就好了。但是第33个测试数据卡了我好久。

看了数据后才发现,原来自己忘记把攻擂赢了的次数算上。、


下面是代码:

#include<bits/stdc++.h>
using namespace std;
#define MID(x,y) ((x+y)>>1)
#define CLR(arr,val) memset(arr,val,sizeof(arr))
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
const double PI = acos(-1.0);
const int INF = 0x3f3f3f3f;
const int N=2e7+7;

int a[N];

int main()
{
	//freopen("f:/input.txt", "r", stdin);
	int maxn=-INF;
	long long k,n;
	int cnt,le;
	CLR(a, 0);
	cin>>n>>k;
	for (int i = 0 ; i < n ; i++) {
		scanf("%d", &a[i]);
		if (a[i] > maxn) maxn = a[i];
	}
	if (n <= k) {
		cout<<maxn<<endl;
		return 0;
	}
	cnt = n;
	
	for (int i = 0;i < n; i++) {
		if (i==0)
			le = 0;
		else if (a[i] != 0 && a[i] != maxn)
			le = 1; 
		else if (a[i] == maxn) {
			cout<<maxn<<endl;
			return 0;
		}
		for (int j = i + 1;a[j] != 0; j++) {
			if (a[i] > a[j]) {
				a[n] = a[j];
				a[j] = 0;
				++le;
				if (le == k) {
					printf("%d\n", a[i]);
					return 0;
				}
				++n;
			} else {
				a[n] = a[i];
				++n;
				break;
			}
		}
	}
}

猜你喜欢

转载自blog.csdn.net/xiaofeng187/article/details/79342702