• 题意:有一个长度为\(n\)的数组,问能否通过多次使某个区间的所有元素变成这个区间的中位数,来使整个数组变成题目所给定的\(k\).

  • 题解:首先这个\(k\)一定要在数组中存在,然后我们对中位数进行考虑,对于一个长度\(>1\)的数组来说,起码要有\(2\)个\(\ge k\)的数,才能使得\(k\)是某个区间的中位数,然后我们再将范围缩小,不难发现,如果\(m\ge k\),我们考虑最小的情况,如果一个区间里面有两个\(m\),我们想让\(m\)是这个区间的中位数,那么这个区间里面最多只能存在一个\(<m\)的数,也就是说,只有两个连续的\(m\)和两个\(m\)中夹着一个\(<m\)的情况才成立,之后,我们一定可以不断转化,使得整个区间变成\(k\).

  • 代码:

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <algorithm>
    #include <stack>
    #include <queue>
    #include <vector>
    #include <map>
    #include <set>
    #include <unordered_set>
    #include <unordered_map>
    #define ll long long
    #define fi first
    #define se second
    #define pb push_back
    #define me memset
    const int N = 1e6 + 10;
    const int mod = 1e9 + 7;
    using namespace std;
    typedef pair<int,int> PII;
    typedef pair<long,long> PLL; int t;
    int n,k,a[N]; int main() {
    ios::sync_with_stdio(false);cin.tie(0);
    cin>>t;
    while(t--){
    cin>>n>>k;
    for(int i=1;i<=n;++i) cin>>a[i]; a[n+1]=0,a[n+2]=0;
    bool ok=0,flag=0;
    for(int i=1;i<=n;++i){
    if(a[i]==k) ok=1;
    if((a[i]>=k&&a[i+1]>=k)||(a[i]>=k&&a[i+2]>=k)) flag=1;
    }
    if((flag && ok)|| (n==1&&ok)) puts("yes");
    else puts("no");
    } return 0;
    }

Codeforces Round #641 (Div. 2) D. Orac and Medians (贪心)的更多相关文章

  1. Codeforces Round #297 (Div. 2)C. Ilya and Sticks 贪心

    Codeforces Round #297 (Div. 2)C. Ilya and Sticks Time Limit: 2 Sec  Memory Limit: 256 MBSubmit: xxx  ...

  2. Codeforces Round #641 (Div. 2)

    只写了A~D A - Orac and Factors 题意:f(n)就是n的第二小因数,问执行k次 n=f(n)+n 后的结果. 题解:如果一直找第二小的因子的话,1e9肯定得t.看下边样例解释就会 ...

  3. Codeforces Round #329 (Div. 2)B. Anton and Lines 贪心

    B. Anton and Lines   The teacher gave Anton a large geometry homework, but he didn't do it (as usual ...

  4. Codeforces Round #370 (Div. 2)C. Memory and De-Evolution 贪心

    地址:http://codeforces.com/problemset/problem/712/C 题目: C. Memory and De-Evolution time limit per test ...

  5. Codeforces Round #376 (Div. 2) C. Socks---并查集+贪心

    题目链接:http://codeforces.com/problemset/problem/731/C 题意:有n只袜子,每只都有一个颜色,现在他的妈妈要去出差m天,然后让他每天穿第 L 和第 R 只 ...

  6. Codeforces Round #373 (Div. 2) A B C 水 贪心 模拟(四舍五入进位)

    A. Vitya in the Countryside time limit per test 1 second memory limit per test 256 megabytes input s ...

  7. Codeforces Round #352 (Div. 2) C. Recycling Bottles 暴力+贪心

    题目链接: http://codeforces.com/contest/672/problem/C 题意: 公园里有两个人一个垃圾桶和n个瓶子,现在这两个人需要把所有的瓶子扔进垃圾桶,给出人,垃圾桶, ...

  8. Codeforces Round #342 (Div. 2) D. Finals in arithmetic 贪心

    D. Finals in arithmetic 题目连接: http://www.codeforces.com/contest/625/problem/D Description Vitya is s ...

  9. Codeforces Round #337 (Div. 2) B. Vika and Squares 贪心

    B. Vika and Squares 题目连接: http://www.codeforces.com/contest/610/problem/B Description Vika has n jar ...

随机推荐

  1. self-taught CS resouce recommendation

    https://github.com/keithnull/TeachYourselfCS-CN/blob/master/TeachYourselfCS-CN.md#%E8%AE%A1%E7%AE%97 ...

  2. 修改hosts文件后不生效,该怎么办

    对于web开发来说,经常需要修改hosts文件,用来将域名与ip对应匹配.但是有时候发现hosts文件明明已经改了,但就是不生效,页面还会跳到某个丧心病狂的私人小站.hosts文件不生效有很多种原因, ...

  3. 在JavaScript种遇到这样的错误如何解决XML 解析错误:格式不佳 位置:http:/... 行 27,列 32:

    相信很多人在开发的过程中都会遇到在js中解析xml文档的问题.有时候文档解析失败,但就是不知道怎么失败的,哪里格式不对.这里教大家一个方法来排查JavaScript解析xml文档格式出错的办法. 1. ...

  4. zabbix的汉化

    1.在windows中找一个自己喜欢的字体(C:\Windows\Fonts)或者去网上下载一个 2.将字体上传到zabbix的web相关目录的fonts目录下 (我的zabbix的web相关的文件都 ...

  5. 【Spring】Spring中的Bean - 1、Baen配置

    Bean配置 简单记录-Java EE企业级应用开发教程(Spring+Spring MVC+MyBatis)-Spring中的Bean 什么是Spring中的Bean? Spring可以被看作是一个 ...

  6. 【RAC】Oracle 10g RAC相关启停命令,维护命令

    Oracle10g RAC关闭及启动步骤   情况1:需要关闭DB(所有实例),OS及Server. a.首先停止Oracle10g环境 $ lsnrctl stop (每个节点上停止监听,也可以用s ...

  7. Xctf攻防世界—crypto—Normal_RSA

    下载压缩包后打开,看到两个文件flag.enc和pubkey.pem,根据文件名我们知道应该是密文及公钥 这里我们使用一款工具进行解密 工具链接:https://github.com/3summer/ ...

  8. java 文件上传的那些事

    文件上传 逻辑 @Value("${sava_path}") private String sava_path; @Override public String saveFile( ...

  9. js reduce数组转对象

    借鉴:https://juejin.im/post/5cfcaa7ae51d45109b01b161#comment这位大佬的处理方法很妙,但是我一眼看过去没有明白,细细琢磨了下,终于明白了 1 co ...

  10. Java 迭代器的使用 Iterator

    Java的集合类可以使用for ... each循环 List Set Queue Deque 我们以List为例 其实一个java编译器并不知道如何遍历一个List 编译器只是把一个for ... ...