码迷,mamicode.com
首页 > 其他好文 > 详细

P1102 A-B 数对【map】

时间:2020-06-28 22:26:58      阅读:71      评论:0      收藏:0      [点我收藏+]

标签:names   idt   image   load   数字   iostream   info   name   clu   

题目

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

 

P1102 A-B 数对【map】

标签:names   idt   image   load   数字   iostream   info   name   clu   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!