二分 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对跳交谊舞.每个男孩都不会和同一个女孩跳两首(或更多)舞曲 ...
随机推荐
- Api容器在应用架构演化中的用途
单层架构 在最开始编程的时候相信大家都写过下面这种架构,界面代码,业务代码,数据库连接全部在工程面完成.当然这种架构在处理很小的程序的时候依然有生命力 两层架构 后来我们发现数据访问的代码大量重复,应 ...
- IOS 长按+轻扫(手势识别)
@interface NJViewController () @property (weak, nonatomic) IBOutlet UIView *customView; @end @implem ...
- JS判断单、多张图片加载完成
转:http://www.daqianduan.com/6419.html 试想,如果模板中有图片,此时如何判断图片是否加载完成? 在此之前来了解一下jquery的ready与window.onloa ...
- Jquery-EasyUI combobox下拉框使用
制作一个json文件: <input data-options="url:'${pageContext.request.contextPath }/json/combobox_data ...
- question 002: dev c++ 当中如何调整字体大小?How to get the first program with C++? c++属于什么软件?
方法:按住ctrl+鼠标滑轮滚动 c++属于系统软件还是应用软件? 说哪个都不对,编译之前属于应用软件,after compile ,it belongs to system software. #i ...
- 自动布局之-NSLayoutConstraint
AutoLayout概念是苹果自iOS6开始引入的概念. 目前为止,实现自动布局技术选型方面也可以使用xib和storyboard.在开发过程中通常登录.注册等变动可能性较小的视图,我会采用xib开发 ...
- A1020 Tree Traversals (25 分)
Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and i ...
- BZOJ1576: [Usaco2009 Jan]安全路经Travel(最短路 并查集)
题意 给你一张无向图,保证从1号点到每个点的最短路唯一.对于每个点求出删掉号点到它的最短路上的最后一条边(就是这条路径上与他自己相连的那条边)后1号点到它的最短路的长度 Sol emmm,考场上想了个 ...
- MySQL - FULL JOIN
SQL FULL JOIN 关键字 只要其中某个表存在匹配,FULL JOIN 关键字就会返回行. FULL JOIN 关键字语法 SELECT column_name(s) FROM table_n ...
- 判断Datable是否有数据
采用any()方法 检查表格的数据是否为空 var table = $('#example').DataTable(); if ( ! table.data().any() ) { alert( 'E ...