zoukankan      html  css  js  c++  java
  • 洛谷 P3184 [USACO16DEC]Counting Haybales数草垛

    题目描述

    Farmer John has just arranged his NN haybales (1 leq N leq 100,0001N100,000 ) at various points along the one-dimensional road running across his farm. To make sure they are spaced out appropriately, please help him answer QQ queries (1 leq Q leq 100,0001Q100,000 ), each asking for the number of haybales within a specific interval along the road.

    农夫John在一条穿过他的农场的路上(直线)放了N个干草垛(1<=N<=100,000),每个干草垛都在不同的点上。为了让每个干草垛之间都隔着一定的距离,请你回答农夫John的Q个问题(1<=Q<=100,000),每个问题都会给出一个范围,询问在这个范围内有多少个干草垛。

    (其实就是有一条数轴上有N个不重复的点,再问Q个问题,每个问题是给出一个范围,问此范围内有多少个点?)

    (在给出范围的边界上也算)

    输入输出格式

    输入格式:

     

    The first line contains NN and QQ .

    The next line contains NN distinct integers, each in the range 0 ldots 1,000,000,00001,000,000,000 , indicating that there is a haybale at each of those locations.

    Each of the next QQ lines contains two integers AA and BB (0 leq A leq B leq 1,000,000,0000AB1,000,000,000 ) giving a query for the number of haybales between AA and BB , inclusive.

    第一行包括N和Q

    第二行有N个数字,每个数字的范围在0~1,000,000,000,表示此位置有一个干草垛。

    接下来的Q行,每行包括两个数字,A和B(0<=A<=B<=1,000,000,000)表示每个询问的范围

     

    输出格式:

     

    You should write QQ lines of output. For each query, output the number of haybales in its respective interval.

    总共Q行,每行输出此范围内的干草垛数量

     

    输入输出样例

    输入样例#1: 复制
    4 6
    3 2 7 5
    2 3
    2 4
    2 5
    2 7
    4 6
    8 10
    输出样例#1: 复制
    2
    2
    3
    4
    1
    0

    说明

    感谢@2014nhc 提供翻译

    思路:先排序,再二分,然后下标减一下就好了。

    upper_bound 找第一个大于查找值的元素。

    lower_bound 找第一个大于等于查找值的元素。

    #include<cstdio>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    #define MAXN 100010
    using namespace std;
    int n,m,q,tot;
    int pos[MAXN],hash[MAXN];
    int main(){
        scanf("%d%d",&n,&q);
        for(int i=1;i<=n;i++)    scanf("%d",&pos[i]);
        sort(pos+1,pos+1+n);
        for(int i=1;i<=q;i++){
            int x,y;scanf("%d%d",&x,&y);
            cout<<upper_bound(pos+1,pos+n+1,y)-lower_bound(pos+1,pos+n+1,x)<<endl;
        }
    }
    细雨斜风作晓寒。淡烟疏柳媚晴滩。入淮清洛渐漫漫。 雪沫乳花浮午盏,蓼茸蒿笋试春盘。人间有味是清欢。
  • 相关阅读:
    背水一战 Windows 10 (61)
    背水一战 Windows 10 (60)
    背水一战 Windows 10 (59)
    背水一战 Windows 10 (58)
    背水一战 Windows 10 (57)
    背水一战 Windows 10 (56)
    背水一战 Windows 10 (55)
    背水一战 Windows 10 (54)
    背水一战 Windows 10 (53)
    背水一战 Windows 10 (52)
  • 原文地址:https://www.cnblogs.com/cangT-Tlan/p/8745314.html
Copyright © 2011-2022 走看看