24h購物| | PChome| 登入
2012-04-21 08:32:21| 人氣1,130| 回應0 | 上一篇 | 下一篇

[UVA][hash] 1152 - 4 Values whose Sum is 0

推薦 0 收藏 0 轉貼0 訂閱站台

The SUM problem can be formulated as follows: given four lists A, B, C, D of integer values, compute how many quadruplet (a, b, c, d ) $ in$ A x B x C x D are such that a + b + c + d = 0 . In the following, we assume that all lists have the same size n .

Input 

The input begins with a single positive integer on a line by itself indicating the number of the cases following, each of them as described below. This line is followed by a blank line, and there is also a blank line between two consecutive inputs.


The first line of the input file contains the size of the lists n (this value can be as large as 4000). We then have n lines containing four integer values (with absolute value as large as 228 ) that belong respectively to A, B, C and D .

Output 

For each test case, the output must follow the description below. The outputs of two consecutive cases will be separated by a blank line.


For each input file, your program has to write the number quadruplets whose sum is zero.

Sample Input 

1

6
-45 22 42 -16
-41 -27 56 30
-36 53 -37 77
-36 30 -75 -46
26 -38 -10 62
-32 -54 -6 45

Sample Output 

5


Sample Explanation: Indeed, the sum of the five following quadruplets is zero: (-45, -27, 42, 30), (26, 30, -10, -46), (-32, 22, 56, -46),(-32, 30, -75, 77), (-32, -54, 56, 30).


STL 的 map 過不了, 因此自己寫 hash
O(n*n*k)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define hashR 2000000

int hash[hashR], size;
struct Node {
    int v, t, next;
} Node[16000000];
void addHash(int v) {
    int m = abs(v)%hashR;
    int head, prev;
    prev = 0;
    head = hash[m];
    while(head != 0) {
        if(Node[head].v == v) {
            Node[head].t++;
            return;
        } else if(Node[head].v < v) {
            prev = head;
            head = Node[head].next;
        } else
            break;
    }
    ++size;
    if(!prev)
        hash[m] = size;
    else
        Node[prev].next = size;
    Node[size].v = v, Node[size].t = 1;
    Node[size].next = head;
}
int getHash(int v) {
    int m = abs(v)%hashR;
    int head, prev;
    prev = 0;
    head = hash[m];
    while(head != 0) {
        if(Node[head].v == v) {
            return Node[head].t;
        } else if(Node[head].v < v) {
            prev = head;
            head = Node[head].next;
        } else
            return 0;
    }
}
int main() {
    int t, n, A[4000], B[4000], C[4000], D[4000];
    int i, j;
    scanf("%d", &t);
    while(t--) {
        scanf("%d", &n);
        memset(hash, 0, sizeof(hash));
        size = 0;
        for(i = 0; i < n; i++) {
            scanf("%d %d %d %d", &A[i], &B[i], &C[i], &D[i]);
        }
        for(i = 0; i < n; i++) {
            for(j = 0; j < n; j++) {
                addHash(A[i]+B[j]);
            }
        }
        int ans = 0;
        for(i = 0; i < n; i++) {
            for(j = 0; j < n; j++) {
                ans += getHash(-C[i]-D[j]);
            }
        }
        printf("%d\n", ans);
        if(t)
            puts("");
    }
    return 0;
}



台長: Morris
人氣(1,130) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA] 11879 - Multiple of 17
此分類上一篇:[UVA][V2] 11002 - Towards Zero

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文