题目链接:https://hihocoder.com/problemset/problem/1128

题目描述:

题目大意就是要我们编程找出K在数组a中的大小排序后的位置。

代码实现:

#include <cstdio>
#include <algorithm>
using namespace std;
int n,k;
const int MAXN = 1e6+;
int a[MAXN];
int C(int x){
if(a[x]==k){
return ;
}else if(a[x]>k){
return ;
}else{
return ;
}
} void solve(){
int lb=,ub=n-;
while(ub>=lb){
int mid=(ub+lb)/;
if(C(mid)==){
printf("%d\n",mid+);
return ;
}else if(C(mid)==){//如果搜索范围大了,缩小搜索范围
ub=mid-;
}else{//否则,加大搜索范围
lb=mid+;
}
}
printf("-1\n");
} int main(){
while(~scanf("%d%d",&n,&k)){
for(int i=;i<n;i++){
scanf("%d",&a[i]);
}
sort(a,a+n);
solve();
}
return ;
}

二分搜索-HihoCoder1128的更多相关文章

  1. [LeetCode] Largest BST Subtree 最大的二分搜索子树

    Given a binary tree, find the largest subtree which is a Binary Search Tree (BST), where largest mea ...

  2. hdu 2199 Can you solve this equation?(二分搜索)

    Can you solve this equation? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ( ...

  3. hdu 2199:Can you solve this equation?(二分搜索)

    Can you solve this equation? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ( ...

  4. 二分搜索 UVALive 6076 Yukari's Birthday (12长春K)

    题目传送门 题意:问使得sum (k^i) = n || n -1 (1 <= i <= r) 的min (r*k)组合的r和k  分析:r的最大不会超过40,枚举r,二分搜索k.注意会爆 ...

  5. hdu 1075 二分搜索

    还是写一下,二分搜索好了 这道题开数组比较坑... 二分,需要注意边界问题,例如:左闭右闭,左闭右开,否则查找不到or死循环 先上AC代码 #include<iostream> #incl ...

  6. K Best(最大化平均数)_二分搜索

    Description Demy has n jewels. Each of her jewels has some value vi and weight wi. Since her husband ...

  7. HDU 2852 KiKi's K-Number(树状数组+二分搜索)

    题意:给出三种操作 0 e:将e放入容器中 1 e:将e从容器中删除,若不存在,则输出No Elment! 2 a k:搜索容器中比a大的第k个数,若不存在,则输出Not Find! 思路:树状数组+ ...

  8. nyoj914Yougth的最大化(二分搜索 + 贪心)

    Yougth的最大化 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 Yougth现在有n个物品的重量和价值分别是Wi和Vi,你能帮他从中选出k个物品使得单位重量的价值最大吗 ...

  9. poj 2976 Dropping tests (二分搜索之最大化平均值之01分数规划)

    Description In a certain course, you take n tests. If you get ai out of bi questions correct on test ...

随机推荐

  1. python技巧 计算字符串中字母出现的次数并取出最大

    有一个字符串 “aaddfdfdercfghfyttefsfsfewretr123trefg5624sdfcgvfdgte6435234532”,现在需要取出里面出现次数最多的字符 第一种方法-装饰器 ...

  2. 基于json文件实现的gearman任务自动重启

    一:在gearman任务失败后,调用task_failed def task_failed(task, *args): info = '\n'.join(args) datetime = local_ ...

  3. redis的底层数据机构

    集群架构 参考 https://blog.csdn.net/wcf373722432/article/details/78678504 https://www.cnblogs.com/George19 ...

  4. oracle怎么给表和列加注释

    oracle添加注释的语法为: comment on column 字段名 is  '注释名' 举例: 创建表: CREATE TABLE t1{ id varchar2(32) primary ke ...

  5. Ubuntu/Debian 8 安装 Intel realsense 摄像头驱动

    ## Make Ubuntu/Debian Up-to-date1. sudo apt-get update && sudo apt-get upgrade && su ...

  6. Dubbo服务容错

    当一个服务调用另一个远程服务出现错误时的外观 Dubbo提供了多种容错方案,默认值为failover(重试) 1).Failover Cluster(默认) 失败自动切换,当出现失败,重试其他服务器, ...

  7. 使用python中的matplotlib 画图,show后关闭窗口,继续运行命令

    使用python中的matplotlib 画图,show后关闭窗口,继续运行命令 在用python中的matplotlib 画图时,show()函数总是要放在最后,且它阻止命令继续往下运行,直到1.0 ...

  8. freeRTOS中文实用教程5--内存管理

    1.前言 不同的嵌入式系统具有不同的内存配置和时间要求.所以单一的内存分配算法只可能适合部分应用程序. FreeRTOS 将内存分配作为可移植层面(相对于基本的内核代码部分而言).这使得不同的应用程序 ...

  9. nginx常见异常分析

    1.nginx不转发消息头header问题 proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_ ...

  10. freeradius 错误: error:140890C7:SSL routines:ssl3_get_client_certificate:peer did not return a certificate

    在进行802.1x 测试时遇到如下问题: Waking up in 4.6 seconds.(156) Received Access-Request Id 82 from 192.168.1.126 ...