二分搜索-HihoCoder1128
题目链接: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的更多相关文章
- [LeetCode] Largest BST Subtree 最大的二分搜索子树
Given a binary tree, find the largest subtree which is a Binary Search Tree (BST), where largest mea ...
- hdu 2199 Can you solve this equation?(二分搜索)
Can you solve this equation? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ( ...
- hdu 2199:Can you solve this equation?(二分搜索)
Can you solve this equation? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ( ...
- 二分搜索 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.注意会爆 ...
- hdu 1075 二分搜索
还是写一下,二分搜索好了 这道题开数组比较坑... 二分,需要注意边界问题,例如:左闭右闭,左闭右开,否则查找不到or死循环 先上AC代码 #include<iostream> #incl ...
- K Best(最大化平均数)_二分搜索
Description Demy has n jewels. Each of her jewels has some value vi and weight wi. Since her husband ...
- HDU 2852 KiKi's K-Number(树状数组+二分搜索)
题意:给出三种操作 0 e:将e放入容器中 1 e:将e从容器中删除,若不存在,则输出No Elment! 2 a k:搜索容器中比a大的第k个数,若不存在,则输出Not Find! 思路:树状数组+ ...
- nyoj914Yougth的最大化(二分搜索 + 贪心)
Yougth的最大化 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 Yougth现在有n个物品的重量和价值分别是Wi和Vi,你能帮他从中选出k个物品使得单位重量的价值最大吗 ...
- 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 ...
随机推荐
- QMouseEvent鼠标事件
Qt中的QMouseEvent一般只涉及鼠标左键或右键的单击.释放等操作,而对鼠标滚轮的响应则通过QWheeEvent来处理
- tf.matmul()和tf.multipy()的区别
首先我们分析一下下面的代码: import tensorflow as tf import numpy as np a=tf.constant([[1., 2., 3.],[4., 5., 6.]]) ...
- 2017CCPC秦皇岛 E题String of CCPC&&ZOJ3985【模拟】
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3985 题意: 给定一个字符串,由c和p组成,可以添加c或者p. 串中出现一 ...
- CF448C Painting Fence (贪心分治)
题面 \(solution:\) 一道蛮水的分治题,但思想很不错(虽然我还是非常天真的以为是积木大赛原题,并且居然还有30分) 看到这个题目,根据贪心的一贯风格,我们肯定能想到将整个栅栏的下面某部分直 ...
- 2018-2019-2 网络对抗技术 20165227 Exp2 后门原理与实践
2018-2019-2 网络对抗技术 20165227 Exp2 后门原理与实践 (1)例举你能想到的一个后门进入到你系统中的可能方式? 接收邮件的方式 (2)例举你知道的后门如何启动起来(win及l ...
- JNI打通java和c
1.JNI简介 The Java Native Interface (JNI) is a programming framework that enables Java code running in ...
- cartographer 安装问题
安装主要参考hitcm教程: http://www.cnblogs.com/hitcm/p/5939507.html 这里只说安装过程中遇到的问题, ceres-solver 与 eigen3 版本不 ...
- win10-Anaconda2-Theano-cuda7.5-VS2013
两天的辗转反侧,终于灵光一现找到了错误. 首先,我在win10下配置好了gpu和cudnn版本的caffe.但是因为win平台的限制,caffe用的不够舒服.因为之前用过一阵子theano,虽然很慢, ...
- C语言中,float在内存中的储存方式
浮点型变量在计算机内存中占用4字节(Byte),即32-bit. 遵循IEEE-754格式标准. 一个浮点数由2部分组成:底数m 和 指数e. ±mantissa × 2exponent (注意,公式 ...
- windows2008r2系统破解登录密码方法
破解windows 2008 r2系统登录密码方法: 1.重启系统,使用windows2008r2安装光盘引导 按住shift+f10 2.切换到d:windows\system32目录(使用cmd. ...