#641 (Div. 2)D. Orac and Medians

題目描述

Slime has a sequence of positive integers a1,a2,…,an.
In one operation Orac can choose an arbitrary subsegment [l…r] of this sequence and replace all values al,al+1,…,ar to the value of median of {al,al+1,…,ar}.
In this problem, for the integer multiset s, the median of s is equal to the ⌊|s|+1/2⌋-th smallest number in it. For example, the median of {1,4,4,6,5} is 4, and the median of {1,7,5,8} is 5.
Slime wants Orac to make a1=a2=…=an=k using these operations.
Orac thinks that it is impossible, and he does not want to waste his time, so he decided to ask you if it is possible to satisfy the Slime’s requirement, he may ask you these questions several times.

Input

The first line of the input is a single integer t: the number of queries.
The first line of each query contains two integers n (1≤n≤100000) and k (1≤k≤109), the second line contains n positive integers a1,a2,…,an (1≤ai≤109)
The total sum of n is at most 100000.

Output

The output should contain t lines. The i-th line should be equal to ‘yes’ if it is possible to make all integers k in some number of operations or ‘no’, otherwise. You can print each letter in lowercase or uppercase.

Example

input
5
5 3
1 5 2 6 1
1 6
6
3 2
1 2 3
4 3
3 1 2 3
10 3
1 2 3 4 5 6 7 8 9 10
output
no
yes
yes
no
yes

Note

In the first query, Orac can’t turn all elements into 3.
In the second query, a1=6 is already satisfied.
In the third query, Orac can select the complete array and turn all elements into 2.
In the fourth query, Orac can’t turn all elements into 3.
In the fifth query, Orac can select [1,6] at first and then select [2,10].

題目大意

一個數組裏一段數可以都變成他的中位數,問整個序列中的數能不能都變成k。可以變化很多次。

題目分析

能成功變化需要滿足的條件有:

  1. a[]中有k這個數。
  2. 有a[i]>=k,a[j]>=k,且|j-i|<=2或n==1。

若不滿足這兩個條件,則輸出no。

代碼如下
#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <stack>
#include <map>
#include <unordered_map>
#include <queue>
#include <vector>
#include <unordered_set> 
#include <algorithm>
#include <iomanip>
#define LL long long
using namespace std;
int const N=1e5+5;
int a[N];
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		int n,k;
		scanf("%d%d",&n,&k);
		
		bool flag=true;
		for(int i=1;i<=n;i++)   //判斷a[]中是否有k
		{
			scanf("%d",&a[i]);
			if(a[i]==k) flag=false;
		}
		if(flag) {puts("no"); continue;}
		
		for(int i=1;i<n;i++)     //判斷條件二
		if(a[i]>=k)
		{
			if(a[i+1]>=k||(a[i+2]>=k&&i+2<=n))
			{
				flag=true;
				break;
			}
		}
		if(flag||n==1) puts("yes");
		else puts("no");
	}
	return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章