二分 by zzt
#include <bits/stdc++.h>
using namespace std; /*
Problem description:
There is an array A, the length of array A is N.
You need to perform Q queries.
Each query, you get an integer X, and you need to find the smallest integer Y in array A, meet Y > X.
If there exist such integer, please print the value of the integer.
Otherwise, please print "There is no number bigger than X". If you finish the above problem, please try the following problem:
1. Find the biggest integer Y in array A, Y < X.
2. Validate if there is a integer Y in array A, meet Y equals to X. 最后写这些:(key 就是上文中每次输入的 X)
1. 对于不下降序列 a,求最小的 i,使得 a[i] = key
2. 对于不下降序列 a,求最大的 i,使得 a[i] = key
3. 对于不下降序列 a,求最小的 i,使得 a[i] > key
4. 对于不下降序列 a,求最大的 i,使得 a[i] < key
5. 对于不上升序列 a,求最小的 i,使得 a[i] = key
6. 对于不上升序列 a,求最大的 i,使得 a[i] = key
7. 对于不上升序列 a,求最小的 i,使得 a[i] < key
8. 对于不上升序列 a,求最大的 i,使得 a[i] > key */ const int maxn = 1e5 + 10;
int n, q;
long long a[maxn]; int main() {
scanf("%d", &n);
for(int i = 1; i <= n; i ++) {
scanf("%lld", &a[i]);
}
sort(a + 1, a + 1 + n); scanf("%d", &q);
while(q --) {
long long x;
scanf("%lld", &x);
int L = 1, R = n, pos = -1;
while(L <= R) {
int mid = (L + R) / 2;
if(a[mid] <= x) L = mid + 1;
else R = mid - 1, pos = mid;
}
if(pos == -1) printf("There is no number bigger than %lld", x);
else printf("%lld\n", a[pos]);
} return 0;
}
二分 by zzt的更多相关文章
- JZOJ 4737. 金色丝线将瞬间一分为二 二分答案
4737. 金色丝线将瞬间一分为二 Time Limits: 1000 ms Memory Limits: 262144 KB Detailed Limits Goto ProblemSet ...
- BZOJ1012: [JSOI2008]最大数maxnumber [线段树 | 单调栈+二分]
1012: [JSOI2008]最大数maxnumber Time Limit: 3 Sec Memory Limit: 162 MBSubmit: 8748 Solved: 3835[Submi ...
- BZOJ 2756: [SCOI2012]奇怪的游戏 [最大流 二分]
2756: [SCOI2012]奇怪的游戏 Time Limit: 40 Sec Memory Limit: 128 MBSubmit: 3352 Solved: 919[Submit][Stat ...
- 整体二分QAQ
POJ 2104 K-th Number 时空隧道 题意: 给出一个序列,每次查询区间第k小 分析: 整体二分入门题? 代码: #include<algorithm> #include&l ...
- [bzoj2653][middle] (二分 + 主席树)
Description 一个长度为n的序列a,设其排过序之后为b,其中位数定义为b[n/2],其中a,b从0开始标号,除法取下整. 给你一个长度为n的序列s. 回答Q个这样的询问:s的左端点在[a,b ...
- [LeetCode] Closest Binary Search Tree Value II 最近的二分搜索树的值之二
Given a non-empty binary search tree and a target value, find k values in the BST that are closest t ...
- [LeetCode] Closest Binary Search Tree Value 最近的二分搜索树的值
Given a non-empty binary search tree and a target value, find the value in the BST that is closest t ...
- jvascript 顺序查找和二分查找法
第一种:顺序查找法 中心思想:和数组中的值逐个比对! /* * 参数说明: * array:传入数组 * findVal:传入需要查找的数 */ function Orderseach(array,f ...
- BZOJ 1305: [CQOI2009]dance跳舞 二分+最大流
1305: [CQOI2009]dance跳舞 Description 一次舞会有n个男孩和n个女孩.每首曲子开始时,所有男孩和女孩恰好配成n对跳交谊舞.每个男孩都不会和同一个女孩跳两首(或更多)舞曲 ...
随机推荐
- 使用POI 读取 Excel 文件,读取手机号码 变成 1.3471022771E10
使用POI 读取 Excel 文件,读取手机号码 变成 1.3471022771E10 [问题点数:40分,结帖人xieyongqiu] 不显示删除回复 ...
- C#获取Honeywell voyager 1400g扫码后的数据
一.在类方法中加入 System.IO.Ports.SerialPort com;二.在构造方法中加入 try { com = new System.IO.Ports.SerialPort(&qu ...
- PAT (Basic Level) Practise (中文)- 1006. 换个格式输出整数 (15)
http://www.patest.cn/contests/pat-b-practise/1006 让我们用字母B来表示“百”.字母S表示“十”,用“12...n”来表示个位数字n(<10),换 ...
- react 的虚拟dom
前端优化的主要方面就是减少页面的DOM操作,减少重排和重绘,React在这方面做了优化,采用了所谓的虚拟DOM,其实我们平时也会遇到虚拟DOM,只是你没有注意罢了,请听我娓娓道来. 所谓的虚拟DOM ...
- react组件间的传值方法
关于react的几个网站: http://react.css88.com/ 小书:http://huziketang.mangojuice.top/books/react/ http://www.re ...
- gulp的常用插件
gulp和webpack的差别:https://www.cnblogs.com/lovesong/p/6413546.html var gulp = require('gulp'); var del ...
- centos7.4安装rabbitmq服务(3.7.10版本)
一.需要安装erlang版本依赖,可以使用二进制安装方式,也可以通过rpm安装,但是安装的时候会提示需要erlang版本>=19.3,而且直接默认yum仓库中的版本较低.,为了节省时间,文章中直 ...
- centos 关闭AliYunDun
执行命令: service aegis stop #停止服务 chkconfig --del aegis # 删除服务
- 动态规划:POJ2576-Tug of War(二维费用的背包问题)
Tug of War Time Limit: 3000MS Memory Limit: 65536K Description A tug of war is to be arranged at the ...
- Python高级主题:Python ABC(抽象基类)
#抽象类实例 作用统一规范接口,降低使用复杂度.import abcclass Animal(metaclass = abc.ABCMeta): ##只能被继承,不能实例化,实例化会报错 @abc.a ...