Codeforces Round #641 (Div. 2) D. Orac and Medians(构造)

题目链接


1.题目大意:给出一个序列,现定义一个区间的"中位数"为 ( l e n + 1 ) / 2 (len+1)/2 (len+1)/2(向下取整),每选定一个区间可以将这个区间的数都变成该中位数,问能否将整个序列都变成 k k k

2.首先序列中必须含有 k k k,有一个特别重要的点是,如果某次变动出现了连续的两个 k k k,那么以每两个连续的 k k k再加上另一个相邻的任意元素,即可将这个长度为 3 3 3的区间都变成k,那么不断扩散最终一定能将整个序列变为 k k k,下面就是看如何能构造出两个连续的 k k k

  • 考虑当区间长度为 2 2 2 { k , x } \{k,x\} {k,x} x x x必须大于 k k k才能将这个区间变为 k k k
  • 考虑区间长度为 3 3 3 { k , x , y } \{k,x,y\} {k,x,y} x x x y y y必须一个大于 k k k一个小于 k k k

没必要再往更长的区间考虑了,因为如果满足了长度为 2 2 2 3 3 3即可满足更长的区间了

3.还有一个特别难想到的点是我们不一定去操作 k k k,也可以操作另外的大于 k k k的数,将整个序列不断迫近 k k k的地方都变成比 k k k大的数,如" 8   1   9   2   3   4   5   2 8~1~9~2~3~4~5~2 8 1 9 2 3 4 5 2", k = 4 k=4 k=4,那么我们对于 { 8   1   9 } \{8~1~9\} {8 1 9}直接都变成 { 9   9   9 } \{9~9~9\} {9 9 9},再不断延伸得到" 8   8   8   8   8   4   5   2 8~8~8~8~8~4~5~2 8 8 8 8 8 4 5 2"这样再扩散整个序列变成 4 4 4即可

综上:

我们只需看是否存在两个位置 i , j i,j i,j,使得 a [ i ] ≥ k    & &    a [ j ] ≥ k a[i] \geq k~~\&\&~~a[j] \geq k a[i]k  &&  a[j]k j − i ≤ 2 j-i \leq 2 ji2(前提是序列中包含 k k k

#include <set>
#include <map>
#include <stack>
#include <queue>
#include <math.h>
#include <cstdio>
#include <string>
#include <cstring>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <unordered_map>
using namespace std;
#define lowbit(x) (x&(-x))
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> P;
const double eps=1e-8;
const double pi=acos(-1.0);
const int inf=0x3f3f3f3f;
const ll INF=1e18;
const int Mod=1e9+7;
const int maxn=2e5+10;

int a[maxn];

int main(){
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    int t,n,k;
    cin>>t;
    while(t--){
        cin>>n>>k;
        int flag=0;
        for(int i=1,x;i<=n;i++){
            cin>>x;
            if(x==k){
                a[i]=0,flag=1;
                continue;
            }
            a[i]=x>k?1:-1;

        }
        if(!flag){
            cout<<"no\n";
            continue;
        }
        if(n==1){
            cout<<"yes\n";
            continue;
        }
        flag=0;
        for(int i=1;i<=n;i++){
            //cout<<a[i]<<endl;
            if(a[i]>=0){
                for(int j=i+1;j<=n && j-i<=2;j++)
                    if(a[j]>=0){
                        flag=1;
                    }
            }
            if(flag) break;
        }
        if(flag) cout<<"yes\n";
        else cout<<"no\n";
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值