洛谷P1102 A-B数对

题目描述

出题是一件痛苦的事情!

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

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

输入输出格式

输入格式:

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

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

输出格式:

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

对于90%的数据,N <= 2000;

对于100%的数据,N <= 200000。

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

输入输出样例

输入样例#1:
4 1
1 1 2 3
输出样例#1:
3

map AC 做法
#include<bits/stdc++.h>
using namespace std;
int i,m,n,j,k;
int b[200001];
long long int ans;
int main(){
    map<int,int>a;
    scanf("%d%d",&n,&m);
    for(i=1;i<=n;i++)
    {
        scanf("%d",b+i);
        a[b[i]]++;
    }
    for(i=1;i<=n;i++)
        ans+=a[b[i]+m];
    cout<<ans;
    return 0;
}

另存一下 死活60分 WA四个点的哈希做法

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#define in long long
#define mo1 13831
#define mo2 18481
using namespace std;

struct node
{
    in cs;
    in next;
    in to;
}edge[2000001];
in ans,tot,head[2000001],a[200001],i,j,n,c,b;
in qr()
{
    in x=1,f=0;
    char ch=getchar();
    while(ch<'0'||ch>'9')
    {
        if(ch=='-') x=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9')
    {
        f=f*10+(in)ch-48;
        ch=getchar();
    }
    return x*f;
}
in get_hash1(in k)
{
    in h=0;
    while(k)
    {
        h=(h*33+k)%mo1;
        k/=10;
    }
    return h;
}
in get_hash2(in q)
{
    in h=0;
    while(q)
    {
        h=(h*53+q)%mo2;
        q/=10; 
    }
    return q;
}
void lj(in from,in to)
{
    for(int l=head[from];l;l=edge[l].next)
    {
        if(edge[l].to==to)
        {
            edge[l].cs++;
            return;
        }
    }
}
void add(in u,in v)
{
    if(head[u]) lj(u,v);
    else
    {
        tot++;
        edge[tot].next=head[u];
        edge[tot].to=v;
        head[u]=tot;
        edge[tot].cs++;
    }
}
int query(in u,in v)
{
    for(j=head[u];j;j=edge[j].next)
    {
        if(edge[j].to==v)
        return edge[j].cs;
    }
    return edge[j].cs;
}
int main()
{
    n=qr();c=qr();
    for(i=0;i<n;++i)
    {
        a[i]=qr();
        in x=get_hash1(a[i]);
        in y=get_hash2(a[i]);
        add(x,y);
    }
    sort(a,a+n);
    for(i=0;i<n;++i)
    {
        b=c+a[i];
        in y=get_hash1(b);
        in z=get_hash2(b);
        ans+=query(y,z);
    }
    cout<<ans;
    return 0;
}
View Code

 100分AC哈希代码 在xxy大神帮助下,才知道取哈希值要有正确的的姿势,还有我犯了特别低级的错误。。

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#define in long long
#define mo 20047
#define mo2 13831
using namespace std;
struct node
{
    in cs;
    in next;
    in to;
}edge[2000001];
in ans,tot,head[2000001],a[200001],i,j,n,c,b;
in qr()
{
    in x=1,f=0;
    char ch=getchar();
    while(ch<'0'||ch>'9')
    {
        if(ch=='-') x=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9')
    {
        f=f*10+(in)ch-48;
        ch=getchar();
    }
    return x*f;
}
in get_hash1(in k)
{
    return k*1007%mo;
}
in get_hash2(in q)
{
    return q*1009%mo2;
}
void lj(in from,in to)
{ 
    for(int l=head[from];l;l=edge[l].next)
    {
        if(edge[l].to==to)
        {
            edge[l].cs++;
            return;
        }
    }
     tot++;
    edge[tot].next=head[from];
    edge[tot].to=to;
    head[from]=tot;
    edge[tot].cs++;
}
void add(in u,in v)
{
    if(head[u])
    lj(u,v);
    else
    {
        tot++;
        edge[tot].next=head[u];
        edge[tot].to=v;
        head[u]=tot;
        edge[tot].cs++;
    }
}
int query(in u,in v)
{
    for(j=head[u];j;j=edge[j].next)
    {
        if(edge[j].to==v)
        return edge[j].cs;
    }
    return 0;
}
int main()
{
    n=qr();c=qr();
    for(i=0;i<n;++i)
    {
        a[i]=qr();
        in x=get_hash1(a[i]);
        in y=get_hash2(a[i]);
        add(x,y);
    }
    for(i=0;i<n;++i)
    {
        b=c+a[i];
        in y=get_hash1(b);
        in z=get_hash2(b);
        ans+=query(y,z);
    }
    cout<<ans;
    return 0;
}