1152 - 4 Values whose Sum is 0

紫书上叫中途相遇法,还有一个名字感觉更加妥帖一点,叫:折半枚举。          有时候,当问题的规模较大时,无法枚举所有元素的组合,但能够枚举一半的元素组合,此时,将问题拆成两半后分别枚举,再合并他们的结果这一方法往往非常有效。

两重循环加二分,总复杂度为n^2logn

这里值得一提的是对集合CD的存储方式,我是用了一个有序数组,也可以用其他方式。

#include<bits/stdc++.h>
using namespace std;
const int max_n =4005;
int n,T;
long long A[max_n],B[max_n],C[max_n],D[max_n],CD[max_n*max_n];
void solve(){
    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);
    long long res = 0;
    for(int i=0;i<n;i++)
    for(int j=0;j<n;j++){
        int cd = -(A[i]+B[j]);
        res+=upper_bound(CD,CD+n*n,cd) - lower_bound(CD,CD+n*n,cd);
    }
    printf("%lld\n",res);
}
int main(){
    scanf("%d",&T);
    while(T--){
        scanf("%d",&n);
        for(int i=0;i<n;i++){
            scanf("%lld",&A[i]);
            scanf("%lld",&B[i]);
            scanf("%lld",&C[i]);
            scanf("%lld",&D[i]);
        }
        solve();
        if(T) printf("\n");
    }
    return 0;
}
时间: 2024-10-11 23:12:52

1152 - 4 Values whose Sum is 0的相关文章

1152 - 4 Values whose Sum is 0(好用的hash标记,多重循环简单化)

不得不说这个题就是炫酷啊! 首先说一说思路吧,是这么想的: 1.弄四重循环,爆破,明显会超时. 2.为了处理多重循环,就枚举a+b+c,只需要在d中找到a+b+c的相反数即可,超时 3.枚举a+b,只需要在c+d中找到a+b的相反数即可,TMD超时! 4.循环只能优化到这个程度了,再优化就得用哈希表直接调用了. 这个题的哈希表也是新的知识,由于本题a+b的值可能很大很大,所以以前的哈希表检索不好用了(因为数组开不了那么大),多亏高神的Blog,学了一招,哈哈,下面是搬运的内容: 对于一个无法用数

UVA 1152 4 Values whose Sum is 0

题意: 四个集合,要求每个集合中选出一个数字,四个数字相加为0,问四个数字相加为零的次数有几次 分析: 先把A+B的和存到sum数组里,然后再从中找-(c+b)出现的个数.求长度为n的有序数组a中的数k的个数num:num=upper_bound(a,a+n,k)-lower_bound(a,a+n,k); 代码: #include <iostream>#include <cstdio>#include <cstring>#include <algorithm&g

(白书训练计划)UVa 1152 4 Values whose Sum is 0(中途相遇法。。)

题目地址:UVa 1152 先枚举A集合与B集合的和,存起来,然后再枚举C集合与D集合的和,看与存起来的值有多少个是互为相反数的.水题.找存起来的值时可以用二分找. 代码如下: #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <stdlib.h> #include <math.h> #include <ctyp

K - 4 Values whose Sum is 0(中途相遇法)

K - 4 Values whose Sum is 0 Crawling in process... Crawling failed Time Limit:9000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Practice UVA 1152 Appoint description: System Crawler (2015-03-12) Description The SUM problem can

POJ 2785 4 Values whose Sum is 0(折半枚举)

4 Values whose Sum is 0 Time Limit: 15000MS   Memory Limit: 228000K Total Submissions: 17088   Accepted: 4998 Case Time Limit: 5000MS Description The SUM problem can be formulated as follows: given four lists A, B, C, D of integer values, compute how

POJ 2785 4 Values whose Sum is 0

4 Values whose Sum is 0 Time Limit: 15000MS   Memory Limit: 228000K Total Submissions: 22691   Accepted: 6869 Case Time Limit: 5000MS Description The SUM problem can be formulated as follows: given four lists A, B, C, D of integer values, compute how

4 Values whose Sum is 0(二分)

4 Values whose Sum is 0 Time Limit: 15000MS   Memory Limit: 228000K Total Submissions: 21370   Accepted: 6428 Case Time Limit: 5000MS Description The SUM problem can be formulated as follows: given four lists A, B, C, D of integer values, compute how

[poj2785]4 Values whose Sum is 0(hash或二分)

4 Values whose Sum is 0 Time Limit: 15000MS Memory Limit: 228000K Total Submissions: 19322 Accepted: 5778 Case Time Limit: 5000MS Description The SUM problem can be formulated as follows: given four lists A, B, C, D of integer values, compute how man

POJ 2785 4 Values whose Sum is 0 (对半分解 二分搜索)

4 Values whose Sum is 0 Time Limit: 15000MS   Memory Limit: 228000K Total Submissions: 17658   Accepted: 5187 Case Time Limit: 5000MS Description The SUM problem can be formulated as follows: given four lists A, B, C, D of integer values, compute how