二分 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对跳交谊舞.每个男孩都不会和同一个女孩跳两首(或更多)舞曲 ...
随机推荐
- Mac Office 2016 卸载
https://support.office.com/zh-cn/article/%E5%8D%B8%E8%BD%BD-Office-2016-for-Mac-eefa1199-5b58-43af-8 ...
- IOS CALayer基本使用 (图层)
● 其实UIView之所以能显示在屏幕上,完全是因为它内部的一个图层(CALayer) ● 在创建UIView对象时,UIView内部会自动创建一个图层(即CALayer对象),通过UIView 的l ...
- 2017.12.14 Java实现-----图书管理系统
通过对图书的增删改查操作 用数组实现 Manager类 package demo55; import java.util.*; public class Manager { Scanner sc = ...
- MAC 设置登录欢迎语
MacdeMacBook-Pro:etc mac$ cd /etc MacdeMacBook-Pro:etc mac$ cat motd 技术沉淀,空杯心态! _______ _______ _ __ ...
- 由一道CTF pwn题深入理解libc2.26中的tcache机制
本文首发安全客:https://www.anquanke.com/post/id/104760 在刚结束的HITB-XCTF有一道pwn题gundam使用了2.26版本的libc.因为2.26版本中加 ...
- 四面体ply格式文件图和数据对应关系分析
通过一个简单的文件来理解ply格式的文件是有所帮助的,我在网上找了一个四面体的ply文件,我通过meshlab打开看到的效果如下所示,我录制成gif文件,希望可以从不同角度展示出来: 同时我截图少许, ...
- 防止sql注入方法 如何防止java中将MySQL的数据库验证密码加上 ' or '1'= '1 就可以出现万能密码 的PreparedStatement
package com.swift; import java.sql.Connection; import java.sql.DriverManager; import java.sql.Prepar ...
- django 图片上传与显示
由于图片上传的需要,学习了一波上传 1. 上传 前端代码 <form action="写上相应的定向位置" method="post" enctype=& ...
- web前后台数据交互的几种方式
1.利用cookie对象 Cookie是服务器保存在客户端中的一小段数据信息.使用Cookie有一个前提,就是客户端浏览器允许使用Cookie并对此做出相应的设置.一般不赞成使用Cookie. (1) ...
- 第15课 栏目的排序处理(组件化) Thinkphp5商城第四季
目录 要实现的功能 思路: 视图层 控制器里: 扩展函数里 要实现的功能 用表单里的提交过来的sort数据,批量修改表里的排序值 界面效果: 思路: 视图层表单提交数据主键=>sort值 控制器 ...