B. Interesting drink
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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.
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的序列,再给出q次询问,每次给出一个数,求序列中比这个数小的数有几个
思路分析:
如果模拟一遍的话肯定要复杂度为O(n*q),肯定要超时的,可以现将序列从小到大排序,然后用二分,找出大于等于前一个数,小于后一个数的序列号,就是我们要求的答案。
在进行二分之前要进行特判,因为如果这个数大于等于所有的数或者这个数小于所有的数都不会对ans进行改变,但是如果大于所有的数的话应该输出的是n,所以先进行特判,然后千万要注意的是在每次询问,也就是for循环之内对ans赋初值,否则的话ans存储的还是上一次询问的答案。
二分内的语句就是先判断符合不符合,符合的话直接跳出循环,不符合的话如果这个数比mid大,就把将l赋值成mid+1,否则将r赋值成mid-1
C. Divisibility by Eight
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
You are given a non-negative integer n, its decimal representation consists of at most 100 digits and doesn’t contain leading zeroes.
Your task is to determine if it is possible in this case to remove some of the digits (possibly not remove any digit at all) so that the result contains at least one digit, forms a non-negative integer, doesn’t have leading zeroes and is divisible by 8. After the removing, it is forbidden to rearrange the digits.
If a solution exists, you should print it.
Input
The single line of the input contains a non-negative integer n. The representation of number