• luogu P1102 A-B 数对 |二分查找


    题目描述

    出题是一件痛苦的事情!

    题目看多了也有审美疲劳,于是我舍弃了大家所熟悉的 A+B Problem,改用 A-B 了哈哈!

    好吧,题目是这样的:给出一串数以及一个数字 C,要求计算出所有 A-B=C 的数对的个数。(不同位置的数字一样的数对算不同的数对)

    输入格式

    第一行包括22个非负整数N和C,中间用空格隔开。

    第二行有N个整数,中间用空格隔开,作为要求处理的那串数。

    输出格式

    输出一行,表示该串数中包含的所有满足A-B=C的数对的个数。

    说明/提示

    对于73%73%的数据,N≤2000;

    对于100%100%的数据,N≤200000。

    所有输入数据都在longint范围内。


    #include<cstdio>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    #define int long long
    using namespace std;
    const int N=2e5+10;
    int A[N];
    signed main(){
    	int n,c;
    	cin>>n>>c;
    	for(int i=1;i<=n;i++)
    	scanf("%lld",A+i);
    	sort(A+1,A+n+1);
    	int ans=0;
    	for(int i=1;i<=n;i++){
    		int a=c+A[i];
    		int op=upper_bound(A+1,A+1+n,a)-A;
    		op-=lower_bound(A+1,A+1+n,a)-A;
    		ans+=op;
    	}
    	cout<<ans<<endl;
    }
    
    不以物喜,不以己悲
  • 相关阅读:
    poj 3744 题解
    hdu 1850 题解
    New World
    CSP2019游记
    LOJ6052 DIV
    CF809E Surprise me!
    Luogu4548 歌唱王国
    Luogu4581 想法
    Note 5.26-5.28
    LOJ6519 魔力环
  • 原文地址:https://www.cnblogs.com/naruto-mzx/p/11857194.html
Copyright © 2020-2023  润新知