天天看點

Codeforces Round #367 (Div. 2) B. 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 xicoins.

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) — 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.

題解:二分。

AC代碼:

#pragma comment(linker, "/STACK:102400000,102400000")
//#include<bits/stdc++.h>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<vector>
#include<map>
#include<cmath>
#include<queue>
#include<set>
#include<stack>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define mst(a) memset(a, 0, sizeof(a))
#define M_P(x,y) make_pair(x,y)  
#define rep(i,j,k) for (int i = j; i <= k; i++)  
#define per(i,j,k) for (int i = j; i >= k; i--)  
#define lson x << 1, l, mid  
#define rson x << 1 | 1, mid + 1, r  
const int lowbit(int x) { return x&-x; }  
const double eps = 1e-8;  
const int inf = 0x7FFFFFFF;  
const int MOD = 1e9 + 7;  
const ll mod = (1LL<<32);
const int N = 2010; 
template <class T1, class T2>inline void getmax(T1 &a, T2 b) { if (b>a)a = b; }  
template <class T1, class T2>inline void getmin(T1 &a, T2 b) { if (b<a)a = b; }
int read()
{
  int v = 0, f = 1;
  char c =getchar();
  while( c < 48 || 57 < c ){
    if(c=='-') f = -1;
    c = getchar();
  }
  while(48 <= c && c <= 57) 
    v = v*10+c-48, c = getchar();
  return v*f;
}
int a[100010];
int main() 
{
//  freopen("in.txt","r",stdin);
  int n;
  n=read();
  for(int i=0;i<n;i++)
  {
    cin>>a[i];
  }
  sort(a,a+n); 
  int q;
  q=read();
  while(q--)
  {
    int m;
    int sum=0;
    m=read();
    printf("%d\n",upper_bound(a,a+n,m)-a);
  }
  return 0;
}
/*int main()
{
  int n,i,j,k,num,m;
  while(scanf("%d",&n)!=EOF)
  {
    for(i=1;i<=n;i++)
    scanf("%d",&a[i]);
    sort(a+1,a+n+1);
    scanf("%d",&m);
    while(m--)
    {
      scanf("%d",&num);
        if(num<a[1])
        printf("0\n");
        else if(num>=a[n])
        printf("%d\n",n);
        else
        {
          int left=1;
          int right=n;
          int mid;
          int ans;
          while(left<=right)
          {
            mid=(left+right)/2;
            if(a[mid]<=num)
            {
              ans=mid;
              left=mid+1;
          }
          else
          {
            right=mid-1;
          }
        }
        printf("%d\n",ans);
      }
    }
  }
  return 0;
}
*/