zoukankan      html  css  js  c++  java
  • Codeforces Round #641 (Div. 2) D. Orac and Medians (贪心)

    • 题意:有一个长度为\(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;
      }
      
  • 相关阅读:
    Fiddler 抓包工具总结(转)
    JMS之ActiveMQ下载安装
    Centos中查询目录中内容命名ls
    JS解析Excel
    Echarts图表
    Oracle 控制语句
    时间日期的获取
    Random随机类(11选5彩票)BigInteger大数据类(华为面试题1000的阶乘)
    Java的析构函数System的finalize()
    JSP Standard Tag Library JSP标准标签库
  • 原文地址:https://www.cnblogs.com/lr599909928/p/12919771.html
Copyright © 2011-2022 走看看