P1102 A-B 数对【map】

时间:2020-06-28 23:09:11   收藏:0   阅读:69

题目

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);
}

 

原文:https://www.cnblogs.com/Jason66661010/p/13205230.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!