【例题 8-3 UVA - 1152】4 Values whose Sum is 0

【链接】 我是链接,点我呀:)
【题意】

在这里输入题意

【题解】

显然中间相遇。 自己写了个hash处理一下冲突就可以了。

【代码】

/*
  	1.Shoud it use long long ?
  	2.Have you ever test several sample(at least therr) yourself?
  	3.Can you promise that the solution is right? At least,the main ideal
  	4.use the puts("") or putchar() or printf and such things?
  	5.init the used array or any value?
  	6.use error MAX_VALUE?
  	7.use scanf instead of cin/cout?
  	8.whatch out the detail input require
*/
/*
    一定在这里写完思路再敲代码!!!
*/
#include <bits/stdc++.h>
const int MOD = 2000007;
using namespace std;

const int N = 4e3;

struct node{
    int x,num;
    node *nex;
    node(){
        x = 0;num = 0;nex=NULL;
    }
    node(int xx){
        x = xx;num = 1;nex=NULL;
    }
};

int n,a[4][N+10];
node *Hash[MOD+10];

void inc(int temp){
    int x = abs(temp)%MOD;
    node *p = Hash[x];
    while (p->nex!=NULL){
        p = p->nex;
        if(p->x==temp){
            p->num++;
            return;
        }
    }
    p->nex = new node(temp);
}

int getnum(int temp){
    int x= abs(temp)%MOD;
    node *p = Hash[x];
    while (p->nex!=NULL){
        p = p->nex;
        if(p->x==temp) return p->num;
    }
    return 0;
}

int main(){
	#ifdef LOCAL_DEFINE
	    freopen("rush_in.txt", "r", stdin);
	#endif

	int T,kase = 0;
	scanf("%d",&T);
	while (T--){
        for (int i = 0;i < MOD;i++) Hash[i] = new node();
        if (kase>0) puts("");
        kase++;
        scanf("%d",&n);
        for (int i = 1;i <= n;i++)
            for (int j = 0;j < 4;j++)
                scanf("%d",&a[j][i]);
        for (int i = 1;i <= n;i++)
            for (int j = 1;j <= n;j++)
                inc(-a[0][i]-a[1][j]);

        long long ans = 0;
        for (int i = 1;i <= n;i++)
                    for (int j = 1;j <= n;j++)
                        ans += getnum(a[2][i]+a[3][j]);
        printf("%lld\n",ans);
	}

	return 0;
}
posted @ 2018-01-02 19:52  AWCXV  阅读(133)  评论(0编辑  收藏  举报