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

POJ 2785 折半搜索

时间:2018-07-08 12:41:18      阅读:134      评论:0      收藏:0      [点我收藏+]

标签:math   upper   stream   type   name   problem   col   大于等于   using   

https://vjudge.net/problem/POJ-2785

 

 

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
#include<set>
#include<algorithm>
#include<map>
#define maxn 4005
typedef long long ll;
using namespace std;
int n;
ll a[maxn],b[maxn],c[maxn],d[maxn];
ll cd[maxn*maxn];//记录c和d数组的和的数组
int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        for(int i=0;i<n;i++)
        {
            cin>>a[i]>>b[i]>>c[i]>>d[i];
        }
        //把c和d的和组成cd数组
        for(int i=0;i<n;i++)
            for(int j=0;j<n;j++){
            cd[i*n+j]=c[i]+d[j];
            }
            sort(cd,cd+n*n);
            ll res=0;
            for(int i=0;i<n;i++)
                for(int j=0;j<n;j++)
            {
                //cout<<"a"<<endl;
                ll ans=-(a[i]+b[j]);
                res+=upper_bound(cd,cd+n*n,ans)-lower_bound(cd,cd+n*n,ans);
                //   返回第一个大于等于ans的位置     返回第一个大于ans的位置
                //最后返回相等ans的位置
            }
            cout<<res<<endl;
    }
    return 0;
}

 

POJ 2785 折半搜索

标签:math   upper   stream   type   name   problem   col   大于等于   using   

原文地址:https://www.cnblogs.com/huangzzz/p/9279539.html

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