【codeforces】706B—Interesting drink

今天药忘吃喽~ 2022-09-25 08:18 67阅读 0赞

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 x**i 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 m**i 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 x**i (1 ≤ x**i ≤ 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 m**i (1 ≤ m**i ≤ 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

  1. 5
  2. 3 10 8 6 11
  3. 4
  4. 1
  5. 10
  6. 3
  7. 11

output

  1. 0
  2. 4
  3. 1
  4. 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.

用二分和树状数组均可。

二分做法:

  1. #include<cstdio>
  2. #include<iostream>
  3. #include<algorithm>
  4. #include<cstring>
  5. using namespace std;
  6. const int N = 1e5+5;
  7. int a[N];
  8. int main() {
  9. int n;
  10. while(scanf("%d",&n)!=EOF) {
  11. for(int i=0; i<n; i++) {
  12. scanf("%d",&a[i]);
  13. }
  14. int q,num;
  15. scanf("%d",&q);
  16. sort(a,a+n);
  17. while(q--) {
  18. scanf("%d",&num);
  19. int ans=upper_bound(a,a+n,num)-a;
  20. printf("%d\n",ans);
  21. }
  22. }
  23. return 0;
  24. }

树状数组做法:

  1. #include<cstdio>
  2. #include<iostream>
  3. #include<cmath>
  4. #include<cstring>
  5. #include<algorithm>
  6. using namespace std;
  7. const int N = 100000+10;
  8. int C[N];
  9. int lowbit(int x) {
  10. return x&(-x);
  11. }
  12. void add(int t,int x,int nn) {
  13. while(t<=nn) {
  14. C[t]=C[t]+x;
  15. t=t+lowbit(t);
  16. }
  17. }
  18. int Sum(int h) {
  19. int s=0;
  20. while(h>0) {
  21. s=s+C[h];
  22. h-=lowbit(h);
  23. }
  24. return s;
  25. }
  26. int main() {
  27. int n;
  28. int nn=100000;
  29. while(scanf("%d",&n)!=EOF) {
  30. memset(C,0,sizeof(C));
  31. for(int i=0; i<n; i++) {
  32. int t;
  33. scanf("%d",&t);
  34. add(t,1,nn);
  35. }
  36. int q;
  37. scanf("%d",&q);
  38. while(q--) {
  39. int h;
  40. scanf("%d",&h);
  41. if(h>100000) h=100000;
  42. printf("%d\n",Sum(h));
  43. }
  44. }
  45. return 0;
  46. }

题目地址:http://codeforces.com/problemset/problem/706/B

发表评论

表情:
评论列表 (有 0 条评论,67人围观)

还没有评论,来说两句吧...

相关阅读

    相关 HZOJ Drink

    神仙题,打了个whs式暴力卡常卡A了(我没脸),正解还是要打的,然而作者的题解看不懂…… > Drink: > > 看惯了罗马音的小朋友们都会知道r发l的音,题目名:D L