Codeforces 706B Interesting drink

B. Interesting drink

time limit per test:2 seconds

memory limit per test:256 megabytes

input:standard input

output:standard output

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.

题目链接:http://codeforces.com/problemset/problem/706/B

分析:二分的简单应用吧!在区间内查找上限下限,然后根据这些求出即可!

下面给出AC代码:

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

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏风口上的猪的文章

.NET面试题系列[14] - LINQ to SQL与IQueryable

"理解IQueryable的最简单方式就是,把它看作一个查询,在执行的时候,将会生成结果序列。" - Jon Skeet

1181
来自专栏更流畅、简洁的软件开发方式

我的数据访问函数库的源代码(三)——返回结构数组

/* 2008 4 25 更新 */ 我的数据访问函数库的源码。整个类有1400行,原先就是分开来写的,现在更新后还是分开来发一下吧。 第三部分:返回结构 ...

2026
来自专栏编程

这或许是对小白最友好的python入门了吧——9,数字深入体验

先给大家介绍一个函数:range(),这个函数是用来干嘛的呢?很简单,数数的,怎么数呢,我先给大家演示一下: for num in range(1,5): ...

2089
来自专栏JackeyGao的博客

Leetcode 算法 -3. Longest Substring Without Repeating Characters

我们以ababcbb为例说明, 这里hash表的值-1是初始值, 这样在方便做+1操作. index 作为开始索引值, 起初index为0, 这是理所当然的。当...

512
来自专栏GreenLeaves

EF基础知识小记六(使用Code First建模自引用关系,常用于系统菜单、文件目录等有层级之分的实体)

日常开发中,经常会碰到一些自引用的实体,比如系统菜单、目录实体,这类实体往往自己引用自己,所以我们必须学会使用Code First来建立这一类的模型. 以下是自...

2196
来自专栏木宛城主

SharePoint CAML In Action——Part II

在SharePoint中,相对于Linq to SharePoint而言,CAML是轻量化的。当然缺点也是显而易见的,"Hard Code"有时会让你抓狂。在实...

1945
来自专栏跟着阿笨一起玩NET

PropertyGrid中的枚举显示为中文

本文转载:http://www.cnblogs.com/yank/archive/2011/09/17/2179598.html

1252
来自专栏菩提树下的杨过

温故而知新:c#中的特性(attribute)

特性(Attribute)是微软在.Net中自创的一种新技术,对于很多初学者来讲,特性一直是一块难啃的骨头。 既然弄不懂,那我们就暂时绕过它吧,回想一下我们在写...

1969
来自专栏逸鹏说道

C# 温故而知新:Stream篇(三)

TextWriter 和 StreamWriter 目录: 为何介绍TextWriter? TextWriter的构造,常用属性和方法 IFormatProvi...

3267
来自专栏大内老A

Delegate如何进行类型转换?

我们知道对于两个不具有继承关系的两个类型,如果没有为它们定义转换器,两这之间的类型转换是不允许的,Delegate也是如此。但是有时候我们却希望“兼容”的两种D...

2028

扫码关注云+社区

领取腾讯云代金券