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("0n");
18 else if(num>=a[n])
19 printf("%dn",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("%dn",ans);
35 }
36 }
37 }
38 return 0;
39 }
- Android基础总结(6)——内容提供器
- No.004 Median of Two Sorted Arrays
- No.001 Two Sum
- No.002 Add Two Numbers
- 【Python环境】探索 Python、机器学习和 NLTK 库
- 【机器学习】机器学习之组合算法总结
- IE 11浏览器0day漏洞(CVE-2015-2425)UAF分析
- 【Python环境】Python可视化工具综述
- 【Spark研究】Spark编程指南(Python版)
- Java细粒度锁实现的3种方式
- 教你在Java接口中定义方法
- 【Spark研究】如何用 Spark 快速开发应用?
- 高性能Java解析器实现过程详解
- Java内存模型深度解读
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 域控安全基础.md
- Win平台安全配置.md
- 机房建设标准及专业术语解释
- WAF安全拦截绕过思路和手法
- 网络拓扑图基础入门
- 【网络技术联盟站】centos7.6环境下redis5.0.8集群搭建文档
- WAF原理及其使用说明
- 还在写Bug?GitHub官方代码扫描工具上线,免费查找漏洞
- n1.Docker安装运行所遇异常解决
- n3.Docker之Win10和Server使用实例
- GitHub 再见 Master !
- IT运维面试问题总结-Linux基础
- 6.Docker使用辅助工具汇总
- IT运维面试问题总结-基础服务、磁盘管理、虚拟平台和系统管理
- IT运维面试问题总结-运维工具、开源应用(Ansible、Ceph、Docker、Apache、Nginx等)