mfcheer

poj-2785

版权声明:转载请注明出处。 https://blog.csdn.net/u014427196/article/details/40431589

题目连接 :http://poj.org/problem?id=2785

折半枚举就OK了

#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <algorithm>
#include <queue>
#include <iostream>
#include <set>
#include <string.h>
#include <functional>

using namespace std;

int a[4444],b[4444],c[4444],d[4444];
int p [4444 * 4444];

int main()
{
    int n ;
    while (scanf("%d",&n)!=EOF)
    {
        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++)
        {
            p[i*n+j] = a[i] + b[j] ;
        }

        sort (p,p+n*n);
        long long  ans = 0;
        for (int i=0;i<n;i++)
            for (int j=0;j<n;j++)
        {
            int cd = -1*(c[i] + d[j]);
            ans += (upper_bound(p,p+n*n ,cd) - lower_bound(p,p+n*n,cd));
        }
        printf("%I64d\n",ans);
    }
    return 0;
}
← 全部文章

留言