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

【A + B + C + D】 问题

时间:2014-10-09 23:04:21      阅读:249      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   color   io   os   ar   java   for   

A + B + C + D

Time Limit: 40000/20000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 130    Accepted Submission(s): 44

Problem Description
Little Robert asked his mother for two cents. "What did you do with the money I gave you yesterday?""I gave it to a poor old woman," he answered."You‘re a good boy," said the mother proudly. "Here are two cents more. But why are you so interested in the old woman?""She is the one who sells the candy."A joke, here entered
Below n four tuple (A, B, C, D), respectively, from the A, B, C, D selected a, b ,c ,d To calculate the number of combinations of a+b+c+d = 0;
 

 

Input
The first line of the input file contains the size of the lists n (1<= n <= 4000)
Then every line containing four integer values (with absolute value as large as 2^28 )
 
Output
For each input file, your program has to write the number whose sum is zero
 

 

Sample Input
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
 
 
 
#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;

const int maxn=4010;

int n;
int s1[maxn*maxn];
int s2[maxn*maxn];
int a[maxn],b[maxn],c[maxn],d[maxn];

int main()
{
//   freopen("in.txt", "r", stdin);
    while(~scanf("%d", &n))
    {for (int i=0;i<n;i++)
        scanf("%d%d%d%d",&a[i],&b[i],&c[i],&d[i]);
    for (int i=0;i<n;i++)
        for (int j=0;j<n;j++)
            s1[i*n+j]=a[i]+b[j];
    for (int i=0;i<n;i++)
        for (int j=0;j<n;j++)
            s2[i*n+j] = c[i]+d[j];
    sort(s1,s1+n*n);
    sort(s2,s2+n*n);
    int r = n*n-1;
    int ans =0;
    for (int i=0;i<n*n;i++)
    {
        while(r>=0 && s1[i]+s2[r]>0)
            r--;
        if (r < 0)break;
        int tmp = r;
        while (tmp >= 0 && s1[i]+s2[tmp] == 0)
            ans++, tmp--;
    }
    printf("%d\n", ans);
    }
    return 0;
}

 

【A + B + C + D】 问题

标签:des   style   blog   color   io   os   ar   java   for   

原文地址:http://www.cnblogs.com/balfish/p/4014505.html

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