CodeForces 706B Interesting drink (二分查找)

题意:给定 n 个数,然后有 m 个询问,每个询问一个数,问你小于等于这个数的数有多少个。

析:其实很简单么,先排序,然后十分查找,so easy。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <stack>
using namespace std;

typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const LL LNF = 100000000000000000;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1e5 + 5;
const int mod = 1e9 + 7;
const char *mark = "+-*";
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
int n, m;
inline bool is_in(int r, int c){
    return r >= 0 && r < n && c >= 0 && c < m;
}
inline LL Max(LL a, LL b){  return a < b ? b : a; }
inline LL Min(LL a, LL b){  return a > b ? b : a; }
int a[maxn];

int main(){
    while(scanf("%d", &n) == 1){
        for(int i = 0; i < n; ++i)  scanf("%d", &a[i]);
        sort(a, a+n);
        scanf("%d", &m);
        while(m--){
            int x;
            scanf("%d", &x);
            printf("%d\n", upper_bound(a, a+n, x) - a);
        }
    }
    return 0;
}
时间: 2024-12-22 21:21:53

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

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 "Bee

Codeforces 474B Worms (二分查找)

题目链接:Codeforces 474B Worms 题意:给出一串数字比如2 7 3 4 9. 表示第一堆编号是[1,2].第二堆编号是[3,9].第三堆编号是[10,12].第四堆编号是[13,16].第五堆编号是[17,25]. 预处理出每堆的上界二分查找答案. AC代码: </pre><pre name="code" class="cpp">#include<stdio.h> #include<map> #in

二分查找/暴力 Codeforces Round #166 (Div. 2) B. Prime Matrix

题目传送门 1 /* 2 二分查找/暴力:先埃氏筛选预处理,然后暴力对于每一行每一列的不是素数的二分查找最近的素数,更新最小值 3 */ 4 #include <cstdio> 5 #include <cstring> 6 #include <algorithm> 7 using namespace std; 8 9 const int MAXN = 5e2 + 10; 10 const int MAXM = 1e6 + 10; 11 const int INF = 0

CodeForces 689C(二分查找)

题意:有四个贼偷糖,后面一个贼偷糖的数量是前一个贼的k倍,现在给你一个方案数m,求满足m种偷取方案数的最小总糖数n. 题意:假设有n颗糖,偷取的方案数为( n/(2^3)+n/(3^3)+....+n(k^3) )种(k^3<=n),二分查找最小的n. #include <iostream> using namespace std; long long solve(long long x) { long long ans=0; for(long long k=2;;k++) { if(x

Codeforces Round #477 (rated, Div. 2, based on VK Cup 2018 Round 3) C. Stairs and Elevators【二分查找】

In the year of 30XX30XX participants of some world programming championship live in a single large hotel. The hotel has nn floors. Each floor has mm sections with a single corridor connecting all of them. The sections are enumerated from 11 to mm alo

Codeforces Round #626 (Div. 1, based on Moscow Open Olympiad in Informatics)B(位运算,二分查找)

从低到高枚举当前位i,把所有数字对1<<(i+1)取模,因为比i位高的数字不会影响到低位,在这些数中,两两组成一对,每对的和如果在第i位上为1,++计数,如果计数为奇数,则答案上这一位为1.这个组对的过程通过排序后二分查找完成. 1 #define HAVE_STRUCT_TIMESPEC 2 #include<bits/stdc++.h> 3 using namespace std; 4 int a[400007]; 5 int b[400007]; 6 int main(){

Codeforces Round #425 (Div. 2) Problem C (Codeforces 832C) Strange Radiation - 二分答案 - 数论

n people are standing on a coordinate axis in points with positive integer coordinates strictly less than 106. For each person we know in which direction (left or right) he is facing, and his maximum speed. You can put a bomb in some point with non-n

51Nod 1422 沙拉酱前缀 二分查找

1422 沙拉酱前缀 题目来源: CodeForces 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题  收藏  关注 沙拉酱非常喜欢数字序列.这正是他要弄一个关于构造序列的算法的原因. 沙拉酱拿了一张白纸.然后他开始用m个步骤来制作一个序列.每一步他要么向这个序列的末尾添加一个数字,要么拿这个序列的开头l个数字,然后在末尾添加c次.对于第二种操作,一般的,如果当前序列是 a1,a2,...,an ,那么经过操作之后序列将变成 a1,a2,...,an[,a1