Codeforces 706B 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 ≤ 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

Copy

53 10 8 6 114110311

output

Copy

0415

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.

这题目可以,题目说xi不超过100000,于是我就开了个数组用树状数组,提交runtime error,一看,哦,查询的是范围是1000000000,所以加了一步判断,这次是答案错误,看样子xi也是这么大的范围,这样一来,这题怕是通解另有其他,估计是二分,可是树状数组不行了吗,数组开不了特别大,那就map一下,过了,再写个二分,二分耗时少,果然是二分。。。

树状数组代码:

#include <iostream>
#include <map>
#include <cstdio>
#define MAX 1000000000
using namespace std;
int n,m;
map<int,int> sum;
int lowbit(int t) {
    return t&(-t);
}
void update(int x) {
    while(x <= MAX) {
        sum[x] ++;
        x += lowbit(x);
    }
}
int getans(int x) {
    int ans = 0;
    while(x > 0) {
        ans += sum[x];
        x -= lowbit(x);
    }
    return ans;
}
int main() {
    int d;
    scanf("%d",&n);
    for(int i = 0;i < n;i ++) {
        scanf("%d",&d);
        update(d);
    }
    scanf("%d",&m);
    for(int i = 0;i < m;i ++) {
        scanf("%d",&d);
        printf("%d\n",getans(d));
    }
}

二分代码:

#include <iostream>
#include <algorithm>
#include <cstdio>
#define MAX 100000
using namespace std;
int n,m;
int x[MAX];

int main() {
    int d;
    scanf("%d",&n);
    for(int i = 0;i < n;i ++) {
        scanf("%d",&x[i]);
    }
    sort(x,x + n);
    scanf("%d",&m);
    for(int i = 0;i < m;i ++) {
        scanf("%d",&d);
        int l = 0,r = n,mid;
        while(l < r) {
            mid = (l + r) / 2;
            if(x[mid] <= d) l = mid + 1;
            else r = mid;
        }
        printf("%d\n",l);
    }
}

原文地址:https://www.cnblogs.com/8023spz/p/9743703.html

时间: 2024-12-21 07:06:01

Codeforces 706B Interesting drink的相关文章

CodeForces - 706B Interesting drink(二分查找)

Interesting drink Problem 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 th

CodeForces 706B Interesting drink (二分查找)

题意:给定 n 个数,然后有 m 个询问,每个询问一个数,问你小于等于这个数的数有多少个. 析:其实很简单么,先排序,然后十分查找,so easy. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #include <cmath> #include <

codeforces 482B. Interesting Array【线段树区间更新】

题目:codeforces 482B. Interesting Array 题意:给你一个值n和m中操作,每种操作就是三个数 l ,r,val.就是区间l---r上的与的值为val,最后问你原来的数组是多少?如果不存在输出no 分析:分析发现要满足所有的区间,而一个点上假如有多个区间的话,这个点的值就是所有区间或的值,因为只有这样才能满足所有区间的,把所有位上的1都保存下来了,那么可以发现用线段树来维护,但是那么怎么判断满不满足条件呢?可以也用线段树,更新了之后在整个维护一遍看看满不满足题意,如

Codeforces 482B Interesting Array(线段树)

题目链接:Codeforces 482B Interesting Array 题目大意:给定一个长度为N的数组,现在有M个限制,每个限制有l,r,q,表示从a[l]~a[r]取且后的数一定为q,问是 否有满足的数列. 解题思路:线段树维护,每条限制等于是对l~r之间的数或上q(取且的性质,相应二进制位一定为1),那么处理完所有的 限制,在进行查询,查询对应每个l~r之间的数取且是否还等于q.所以用线段树维护取且和,修改为或操作. #include <cstdio> #include <c

Codeforces 482B Interesting Array(线段树)

题目链接 Interesting Array 区间更新.然后对于每一个约数重新求一遍区间的&值,不符合就跳出. 1 #include <bits/stdc++.h> 2 3 using namespace std; 4 5 #define rep(i, a, b) for (int i(a); i <= (b); ++i) 6 #define lson i << 1, L, mid 7 #define rson i << 1 | 1, mid + 1, R

[Codeforces 482B] Interesting Array

[题目链接] https://codeforces.com/contest/482/problem/B [算法] 显然 , 当qi二进制表示下第j位为1时 , [li,ri]中每个数二进制表示下的第j为也为1 根据这个性质 , 计算出要求的序列a, 然后用线段树检验序列是否合法即可 时间复杂度 : O(NlogN) [代码] #include<bits/stdc++.h> using namespace std; const int MAXN = 1e5 + 10; #define MAXLO

codeforces 88E Interesting Game

题目大意: 两个好朋友再将一堆物品分堆,每次都将一堆物品分成数量连续的至少两个堆,直到一个人不能分堆为输 第一次做博弈问题,看了百度文库的http://wenku.baidu.com/link?url=C6qxEhqBEJJFDPC2nSW8kaOer2s_WyOxAhUi0QzF_-B38Gw7KqbbjFvuiaLUvuoGYtliZE_JAH_PO1VPpOT1Vo5OvbyPzBR3Q5IlmWYIHuy 感觉讲的还是蛮好的 这里因为每次至少要分两个堆 所以初始sg[0] = sg[1]

Codeforces 482B Interesting Array 构造+线段树判可行

题目链接:点击打开链接 题意: 构造一个n长的序列,m个限制: 每个限制[l, r] q 序列要满足 区间[l,r]的所有数 & 起来结果是q 思路: 直接构造,然后判可行就好了.. #include <stdio.h> #include <cstring> #include <iostream> #include <map> template <class T> inline bool rd(T &ret) { char c;

Codeforces Round #367 (Div. 2)

A. Beru-taxi (Codeforces 706A) 水题,求一下到每个点的距离,取最小值即可 #include<stdio.h> #include<string.h> #include<math.h> #include<algorithm> #define N using namespace std; int n,x,y,p,q,s; double t,ans=1000000000; int main() { scanf("%d%d%d&