POJ 2785 折半搜索

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;
}

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

时间: 2024-08-25 02:35:00

POJ 2785 折半搜索的相关文章

POJ 2785 (暴力搜索&amp;二分)

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 ) ∈ 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 .

POJ 2785 折半枚举

#include <cstdio> #include <iostream> #include <algorithm> #define LL long long using namespace std; int a[4][4005]; int b[2][4005*4005]; int main(){ int n; while(cin >> n){ for(int i = 0;i < n;i++){ for(int j = 0;j < 4;j++){

POJ 3977 折半搜索

Subset Time Limit: 30000MS   Memory Limit: 65536K Total Submissions: 4128   Accepted: 796 Description Given a list of N integers with absolute values no larger than 1015, find a non empty subset of these numbers which minimizes the absolute value of

poj 2785

#include <stdio.h>#include <algorithm>#define N 4000using namespace std; int a[N],b[N],c[N],d[N],ab[N*N],cd[N*N]; int main(){int n,ans,i,j,k;while(scanf("%d",&n)!=EOF){ k=0; for(i=0;i<n;i++) scanf("%d %d %d %d",&

POJ 2329 (暴力+搜索bfs)

Nearest number - 2 Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 3943 Accepted: 1210 Description Input is the matrix A of N by N non-negative integers. A distance between two elements Aij and Apq is defined as |i ? p| + |j ? q|. Your pro

POJ 2918 Tudoku [搜索]

和POJ2676一样哈,,, 原谅我水题目数 = =!... #include <cstdio> #include <cstring> #include <iostream> #include <cstdlib> using namespace std; int map[10][10]; char tmp[10][10]; bool row[10][10]; bool col[10][10]; bool grid[10][10]; bool DFS(int

poj 2785 4 Values whose Sum is 0(sort+二分)

题意: 给你ABCD四个集合,集合中数的个数都为N(N<=4000),如果分别在ABCD四个集合中取一个数,a b c d ,求有多少种可能使得a+b+c+d=0. 当然你可以尝试枚举所有的组合,绝对可以计算出结果,大概有N^4种吧,如果你有足够的时间还是可以算出来的,哈哈. 当然我不是用上面一种方法计算的,那样算肯定超时. 我的做法是求出所有a+b 到ab数组中, 和所有 c+d到cd数组中,然后排序,枚举每个ab,用二分在cd中查找有没有可能组成0.  有个问题就是二分只能返回一个结果,所以

E. Anya and Cubes (CF #297 (Div. 2) 折半搜索)

E. Anya and Cubes time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Anya loves to fold and stick. Today she decided to do just that. Anya has n cubes lying in a line and numbered from 1 to n

算法复习——哈希表+折半搜索(poj2549)

搬讲义~搬讲义~ 折半搜索感觉每次都是打暴力时用的啊2333,主要是用于降次··当复杂度为指数级别时用折半可以减少大量复杂度··其实专门考折半的例题并不多···一般都是中途的一个小优化··· 然后折半搜索常常与哈希表一起使用··尤其是遇到方程类的问题时··· 哈希表就不说了吧···毕竟比较简单···不懂得看下面题的代码就行了,一道折半与哈希表联合运用的经典方程模型题··· Description Given S, a set of integers, find the largest d suc