Interesting drink

Problem

Vasiliy likes to rest after a hard work, so you may often meet him in some bar nearby. As all programmers do, he loves the famous drink "Beecola", which can be bought in n different shops in the city. It's known that the price of one bottle in the shop i is equal to xi coins.

Vasiliy plans to buy his favorite drink for q consecutive days. He knows, that on the i-th day he will be able to spent mi coins. Now, for each of the days he want to know in how many different shops he can buy a bottle of "Beecola".

Input

The first line of the input contains a single integer n (1 ≤ n ≤ 100 000) — the number of shops in the city that sell Vasiliy's favourite drink.

The second line contains n integers xi (1 ≤ xi ≤ 100 000) — prices of the bottles of the drink in the i-th shop.

The third line contains a single integer q (1 ≤ q ≤ 100 000) — the number of days Vasiliy plans to buy the drink.

Then follow q lines each containing one integer mi (1 ≤ mi ≤ 109) — the number of coins Vasiliy can spent on the i-th day.

Output

Print q integers. The i-th of them should be equal to the number of shops where Vasiliy will be able to buy a bottle of the drink on the i-th day.

Example

Input
5
3 10 8 6 11
4
1
10
3
11
Output
0
4
1
5

Note

On the first day, Vasiliy won't be able to buy a drink in any of the shops.

On the second day, Vasiliy can buy a drink in the shops 1, 2, 3 and 4.

On the third day, Vasiliy can buy a drink only in the shop number 1.

Finally, on the last day Vasiliy can buy a drink in any shop.

给出n个数,再输入若干个M,每输入一次M,求n个数里有多少个数小于M。
由于n是1e5的数量级,不可O(n^2)暴力,用二分即可。

 1 #include<stdio.h>
2 #include<string.h>
3 #include<algorithm>
4 using namespace std;
5 int a[100000+10];
6 int main()
7 {
8 int n,i,j,k,num,m;
9 while(scanf("%d",&n)!=EOF)
10 {
11 for(i=1;i<=n;i++)
12 scanf("%d",&a[i]);
13 sort(a+1,a+n+1);
14 scanf("%d",&m);
15 while(m--)
16 {
17 scanf("%d",&num);
18 if(num<a[1])
19 printf("0\n");
20 else if(num>=a[n])
21 printf("%d\n",n);
22 else
23 {
24 int left=1;
25 int right=n;
26 int mid;
27 int ans;
28 while(left<=right)
29 {
30 mid=(left+right)/2;
31 if(a[mid]<=num)
32 {
33 ans=mid;
34 left=mid+1;
35 }
36 else
37 {
38 right=mid-1;
39 }
40 }
41 printf("%d\n",ans);
42 }
43 }
44 }
45 return 0;
46 }

CodeForces - 706B Interesting drink(二分查找)的更多相关文章

  1. CodeForces 706B Interesting drink (二分查找)

    题意:给定 n 个数,然后有 m 个询问,每个询问一个数,问你小于等于这个数的数有多少个. 析:其实很简单么,先排序,然后十分查找,so easy. 代码如下: #pragma comment(lin ...

  2. Codeforces - 706B - Interesting drink - 二分 - 简单dp

    https://codeforces.com/problemset/problem/706/B 因为没有看见 $x_i$ 的上限是 $10^5$ ,就用了二分去做,实际上这道题因为可乐的价格上限是 $ ...

  3. 【二分】Codeforces 706B Interesting drink

    题目链接: http://codeforces.com/problemset/problem/706/B 题目大意: n (1 ≤ n ≤ 100 000)个商店卖一个东西,每个商店的价格Ai,你有m ...

  4. CodeForces 706B Interesting drink

    排序,二分. 将$x$数组从小到大排序,每次询问的时候只要二分一下位置就可以了. #pragma comment(linker, "/STACK:1024000000,1024000000& ...

  5. codeforces 706B B. Interesting drink(二分)

    题目链接: B. Interesting drink 题意: 给出第i个商店的价钱为x[i],现在询问mi能在多少个地方买酒; 思路: sort后再二分; AC代码: #include <ios ...

  6. Codeforces Round #367 (Div. 2) B. Interesting drink (模拟)

    Interesting drink 题目链接: http://codeforces.com/contest/706/problem/B Description Vasiliy likes to res ...

  7. CodeForces - 600B Queries about less or equal elements (二分查找 利用stl)

    传送门: http://codeforces.com/problemset/problem/600/B Queries about less or equal elements time limit ...

  8. 二分查找/暴力 Codeforces Round #166 (Div. 2) B. Prime Matrix

    题目传送门 /* 二分查找/暴力:先埃氏筛选预处理,然后暴力对于每一行每一列的不是素数的二分查找最近的素数,更新最小值 */ #include <cstdio> #include < ...

  9. Codeforces 475D 题解(二分查找+ST表)

    题面: 传送门:http://codeforces.com/problemset/problem/475/D Given a sequence of integers a1, -, an and q ...

随机推荐

  1. 06.I/O操作

    参考文章 https://www.cnblogs.com/xuwenfeng/articles/2238127.html 1. 驱动器操作 在Windows操作系统中,存储介质统称为驱动器,硬盘由于可 ...

  2. Java集合框架和数组的排序(转载)

    Java集合框架(*Collection)*和数组的排序 ​ 根据约定,在使用java编程的时候应尽可能的使用现有的类库,当然你也可以自己编写一个排序的方法,或者框架,但是有几个人能写得比JDK里的还 ...

  3. docker-01

    Docker介绍 1 什么是容器? Docker 是一个开源的应用容器引擎,基于 Go 语言 并遵从 Apache2.0 协议开源 Docker 可以让开发者打包他们的应用以及依赖包到一个轻量级.可移 ...

  4. MySQL-17-MHA高可用技术

    环境准备 环境准备 至少准备3台独立的虚拟机数据库实例,建议4台 这里实验只准备3台,需要配置好 基于GTID的主从复制,具体怎么配置可以参看前面的章节 db01 10.0.0.51 主库 db02 ...

  5. cmseasy&内网渗透 Writeup

    某CTF内网渗透 题目:www.whalwl.site:8021 目录 cmseasy 内网横向渗透 cmseasy 简单看一下网站架构 Apache/2.4.7 (Ubuntu) PHP/5.5.9 ...

  6. S3C2440—7.存储控制器访问外设

    文章目录 一.内存接口的概念 二.存储控制器(内存控制器) 2.1 什么是存储控制器? 2.2 S3C2440存储控制器介绍 2.3 存储控制器如何处理不同位宽的外设 2.4 怎么确定芯片的访问地址? ...

  7. gitlab-ci集成SonarQube代码质量检查

    SonarQube是管理代码质量一个开放平台,可以快速的定位代码中潜在的或者明显的错误. docker安装 1.拉取 postgres:docker pull postgres:10 2.拉取sona ...

  8. GC垃圾收集器

    垃圾收集器是垃圾收集算法的具体实现,是执行垃圾收集算法的,是守护线程. HotSport虚拟机采用分代收集(JVM规范并未对堆区进行划分),将堆分为年轻代和老年代,垃圾收集器也是这样组合使用的,不过已 ...

  9. rsync基本使用

    概念 rsync是linux系统下的数据镜像备份工具.使用快速增量备份工具Remote Sync可以远程同步,支持本地复制,或者与其他SSH.rsync主机同步. 目前,已支持跨平台,可以在Windo ...

  10. idea中的springboot的maven项目报错Failed to clean project: Failed to delete D:\new_shunyi\shunyi\target\shunyi\WEB-INF\classes\static\

    正准备打包上传到测试环境,本想先clean下,没想到报了个这个错,意思大概是无法删除target下的某个文件,没有权限(一脸懵逼): 后来百度发现可能是因为我之前启动了tomcat,未关闭,然后关闭了 ...