B. Orac and Medians(Codeforces Round 641 (Div. 1))

Orac and Medians

https://codeforces.com/contest/1349/problem/B

题面翻译

询问 a 1 , a 2 , ⋯ a n a_1,a_2,\cdots a_n a1,a2,an能否通过若干次将任意区间全部赋值为其中位数这个操作,来使得整个序列全部变为 k k k。(中位数指第 ⌊ ∣ s ∣ + 1 2 ⌋ \lfloor \frac {∣s∣+1} 2 \rfloor 2s+1小的数)
多次询问,每次第一行两个整数, n n n k k k;第二行 n n n个整数, a 1 , a 2 , ⋯ a n a_1,a_2,\cdots a_n a1,a2,an
数据范围: 1 ≤ n ≤ 1 0 5 , 1 ≤ k ≤ 1 0 9 , 1 ≤ a i ≤ 1 0 9 1 \le n \le 10^5,1 \le k \le 10^9,1 \le a_i \le 10^9 1n105,1k109,1ai109,并保证所有询问中 n n n的和不超过 1 0 5 10^5 105

题目描述

Slime has a sequence of positive integers $ a_1, a_2, \ldots, a_n $ .

In one operation Orac can choose an arbitrary subsegment $ [l \ldots r] $ of this sequence and replace all values $ a_l, a_{l + 1}, \ldots, a_r $ to the value of median of $ {a_l, a_{l + 1}, \ldots, a_r} $ .

In this problem, for the integer multiset $ s $ , the median of $ s $ is equal to the $ \lfloor \frac{|s|+1}{2}\rfloor $ -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 $ a_1 = a_2 = \ldots = a_n = 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.

输入格式

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\le n\le 100,000) $ and $ k\ (1\le k\le 10^9) $ , the second line contains $ n $ positive integers $ a_1,a_2,\dots,a_n\ (1\le a_i\le 10^9) $

The total sum of $ n $ is at most $ 100,000 $ .

输出格式

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.

样例 #1

样例输入 #1

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

样例输出 #1

no
yes
yes
no
yes

提示

In the first query, Orac can’t turn all elements into $ 3 $ .

In the second query, $ a_1=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的一定是输出no的,这是第一种情况。如果有两个相邻且等于k的数,一定是输出yes的。
1:相邻的三个数,如果有两个相等,那么这个进行操作一定是能够全成为那个相等的数的
2:相邻的数是可以变成那个相对较小的数的,
找到一个满足1的情况的,就是三个相邻的数中有两个大于等于k的数,可以令所有数组小于k的都变成大于等于k的数,然后必然会有一个k是与大于等于k的数挨着的,这样就可以令k之前的所有数都变成k,最后一定是能够全部变成k的。
如果说大于等于k的数记为1,小于k的数记为0,那么根据以上思路,只要数组中出现1 1,或者1 0 1,或者1 1 1(其实可以舍去),那么一定就是yes。
注意数组边界问题。或者也可以直接清空数组。

#include<bits/stdc++.h>
using namespace std;
using i64=long long;
const int N=1e6+10; 
i64 a[N];
const i64 mod=1e9+7;
void solve(){
	int n,k;
	cin>>n>>k;
	int flag=0;
	for(int i=1;i<=n;i++){
		cin>>a[i];
		a[i+1]=0;
		if(a[i]==k) flag=1;
	}
	if(flag==0){
		cout<<"no\n";
		return ;
	}
	else {
		if(n==1){
			cout<<"yes\n";
			return ;
		}
		for(int i=1;i<=n;i++){
			if((a[i]>=k&&a[i+1]>=k)||(a[i]>=k&&a[i-1]>=k)){
				cout<<"yes\n";
				return ;
			}
		}
		for(int i=1;i<=n;i++){
			if(a[i-1]>=k&&a[i+1]>=k){
				cout<<"yes\n";
				return ;
			}
		}
	}
	cout<<"no\n";
}
signed main(){
	ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	int t;
	cin>>t;
	while(t--){
		solve();
	}
} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值