P1102 A-B 数对【map】

题目

https://www.luogu.com.cn/problem/P1102

 

 思路

使用一个map记录一样的数字出现的次数,使用另外一个map记录数字来寻找比自己大c和小c的数字

7 10 与10 7都算,最后整体除以2就行

代码

#include<iostream>
#include<cstdio>
#include<map>
using namespace std;
map<long long , long long>list;
map<long long, long long>amount;
int main()
{
    long long n, c;
    long long counts = 0;
    scanf("%lld%lld", &n, &c);
    for (int i = 1; i <=n; i++)
    {
        long long a;
        scanf("%lld", &a);
        list[a] = i;
        amount[a]++;
    }
    map<long long, long long>::iterator it = amount.begin();
    for (; it != amount.end(); it++)
    {
        if (list[it->first + c] != 0)counts += amount[it->first + c] * amount[it->first];
        if (list[it->first - c] != 0)counts += amount[it->first - c] * amount[it->first];
    }

    printf("%lld", counts/2);
}

 

posted @ 2020-06-28 21:54  Jason66661010  阅读(178)  评论(0编辑  收藏  举报