• K


    K - 4 Values whose Sum is 0
    Appoint description:

    Description

     
    Download as PDF

    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$AxBxCxD 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).

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cmath>
     5 #include <algorithm>
     6 #include <string>
     7 #include <vector>
     8 #include <stack>
     9 #include <queue>
    10 #include <set>
    11 #include <map>
    12 #include <list>
    13 #include <iomanip>
    14 #include <cstdlib>
    15 #include <sstream>
    16 using namespace std;
    17 typedef long long LL;
    18 const int INF=0x5fffffff;
    19 const double EXP=1e-8;
    20 const int MS=4005;
    21 int A[MS],B[MS],C[MS],D[MS],n,sum[MS*MS];
    22 
    23 int main()
    24 {
    25     int T;
    26     scanf("%d",&T);
    27     while(T--)
    28     {
    29         scanf("%d",&n);
    30         for(int i=0;i<n;i++)
    31             scanf("%d%d%d%d",&A[i],&B[i],&C[i],&D[i]);
    32         int cnt=0;
    33         for(int i=0;i<n;i++)
    34             for(int j=0;j<n;j++)
    35                 sum[cnt++]=A[i]+B[j];
    36         sort(sum,sum+cnt);
    37         LL ans=0;
    38         for(int i=0;i<n;i++)
    39             for(int j=0;j<n;j++)
    40         {
    41             ans+=upper_bound(sum,sum+cnt,-C[i]-D[j])-lower_bound(sum,sum+cnt,-C[i]-D[j]);
    42         }
    43         printf("%lld
    ",ans);
    44         if(T)
    45             printf("
    ");
    46     }
    47     return 0;
    48 }
  • 相关阅读:
    wzplayer2 支持mac 了,最新谍报
    关于duilib的理解
    DMS的实现转载
    视频通话最新谍报
    新人补钙系列教程之:Function类的重要方法apply()
    新人补钙系列教程之:webgame好友模块原型开发一
    新人补钙系列教程之: 大型 webGame 开发系列之 pipes
    新人补钙系列教程之:模拟java多线程Thread类
    flash学习网站
    新人补钙系列教程之:AS3与服务器通信
  • 原文地址:https://www.cnblogs.com/767355675hutaishi/p/4335778.html
Copyright © 2020-2023  润新知