找四个数的和为0

  题目大意:给定四个集合,要你每个集合选4个数字,组成和为0

  这题是3977的简单版,只要和是0就可以了

  

 #include <iostream>
#include <algorithm>
#include <functional>
#define MAX 4001 using namespace std; typedef long long LL_INT;
static LL_INT list[][MAX], set_sum1[MAX*MAX]; LL_INT *Binary_Lower_Bound(int &, LL_INT);
LL_INT *Binary_Upper_Bound(int &, LL_INT);
LL_INT solve(int &,int &); int main(void)
{
int list_contian, sum_comb;
LL_INT ans; while (~scanf("%d", &list_contian))
{
for (int i = ; i < list_contian; i++)
scanf("%lld%lld%lld%lld", &list[][i], &list[][i], &list[][i], &list[][i]); sum_comb = ;
for (int i = ; i < list_contian; i++)//把第一张和第二张表的总数枚举出来
for (int j = ; j < list_contian; j++)
set_sum1[sum_comb++] = list[][i] + list[][j]; sort(set_sum1, set_sum1 + sum_comb);
ans = solve(list_contian, sum_comb);
printf("%lld\n", ans);
}
return EXIT_SUCCESS;
} LL_INT solve(int &list_contain, int &sum_comb)
{
LL_INT tmp_sum, ans = ;
int pos1, pos2; for (int i = ; i < list_contain; i++)
{
for (int j = ; j < list_contain; j++)
{
tmp_sum = -list[][i] - list[][j];
pos1 = Binary_Upper_Bound(sum_comb, tmp_sum) - set_sum1;
pos2 = Binary_Lower_Bound(sum_comb, tmp_sum) - set_sum1;
ans += pos1 - pos2;
}
}
return ans;
} LL_INT *Binary_Lower_Bound(int &sum_comb, LL_INT val)
{
int lb = , mid, count1 = sum_comb, count2;
while (count1 > )
{
count2 = count1 >> ;
mid = lb + (count1 >> );
if (set_sum1[mid] < val)
{
lb = ++mid;
count1 -= count2 + ;
}
else count1 = count2;
}
return &set_sum1[lb];
} LL_INT *Binary_Upper_Bound(int &sum_comb, LL_INT val)
{
int lb = , mid, count1 = sum_comb, count2;
while (count1 > )
{
count2 = count1 >> ;
mid = lb + (count1 >> );
if (set_sum1[mid] <= val)
{
lb = ++mid;
count1 -= count2 + ;
}
else count1 = count2;
}
return &set_sum1[lb];
}

  

Divide and conquer:4 Values whose Sum is 0(POJ 2785)的更多相关文章

  1. 4 Values whose Sum is 0 POJ - 2785

    4 Values whose Sum is 0 Time Limit: 15000MS   Memory Limit: 228000K Total Submissions: 29243   Accep ...

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

    题目链接 Description The SUM problem can be formulated as follows: given four lists A, B, C, D of intege ...

  3. 4 Values whose Sum is 0 POJ - 2785(二分应用)

    题意:输入一个数字n,代表有n行a,b,c,d,求a+b+c+d=0有多少组情况. 思路:先求出前两个数字的所有情况,装在一个数组里面,再去求后两个数字的时候二分查找第一个大于等于这个数的位置和第一个 ...

  4. POJ 2785 4 Values whose Sum is 0(想法题)

    传送门 4 Values whose Sum is 0 Time Limit: 15000MS   Memory Limit: 228000K Total Submissions: 20334   A ...

  5. POJ 2785 4 Values whose Sum is 0

    4 Values whose Sum is 0 Time Limit: 15000MS   Memory Limit: 228000K Total Submissions: 13069   Accep ...

  6. 哈希-4 Values whose Sum is 0 分类: POJ 哈希 2015-08-07 09:51 3人阅读 评论(0) 收藏

    4 Values whose Sum is 0 Time Limit: 15000MS Memory Limit: 228000K Total Submissions: 17875 Accepted: ...

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

    4 Values whose Sum is 0 Time Limit: 15000MS Memory Limit: 228000K Total Submissions: 19322 Accepted: ...

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

    K - 4 Values whose Sum is 0 Crawling in process... Crawling failed Time Limit:9000MS     Memory Limi ...

  9. UVA 1152 4 Values whose Sum is 0 (枚举+中途相遇法)(+Java版)(Java手撕快排+二分)

    4 Values whose Sum is 0 题目链接:https://cn.vjudge.net/problem/UVA-1152 ——每天在线,欢迎留言谈论. 题目大意: 给定4个n(1< ...

随机推荐

  1. Linux关于vm虚拟机复制后无法启动网卡

    1.一个月前由于自己一直在开发PHP站点,所以把Linux抛出去很长时间没有碰,最近几天把Linux的一些捡起来, 但在我设置vm虚拟机由于在家里做的实验未做完,复制到U盘想到公司接着做没成像,系统是 ...

  2. PostgreSQL表空间、数据库、模式、表、用户/角色之间的关系

    看PostgreSQL9的官方文档,我越看越迷糊,这表空间,数据库,模式,表,用户,角色之间的关系怎么在PostgreSQL里这么混乱呢?经过中午的一个小实验,我逐渐理清了个中来龙去脉.下面我来还原我 ...

  3. Memcached原理分析

    Memcached的内存管理方式 Memcached采用了名为Slab Allocation的机制分配,管理内存. Slab Allocation的原理相当简单.将分配的内存分割成各种尺寸的块(chu ...

  4. 2013区域赛长沙赛区现场赛 K - Pocket Cube

    K - Pocket Cube Time Limit:10000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Su ...

  5. Mac Pro Office Word 2011 个性化设置

    操作系统:Mac Pro OS X 10.11.5 1.常用的几个操作: (1).视图 -> 功能区 (2).视图 -> 打印版式 (3).视图 -> 大纲 (4).视图 -> ...

  6. SQL按指定文字顺序进行排序(中文或数字等)

    在有些情况下我们需要按指定顺序输出数据,比如选择了ID in(3,1,2,5,4)我们希望按这个3,1,2,5,4的顺序输出,这样只使用order by ID是无法实现的, 但是我们可以使用order ...

  7. Redis学习笔记三:多机数据库的实现

    1.复制 执行slaveof命令或者设置slaveof选项,让一个服务器去复制另外一个服务器. 旧版复制功能的实现(Redis 2.8 之前的版本) 复制功能分为同步和命令传播两个操作. 同步(syn ...

  8. fontconfig编译出错

    编译fontconfig编译时出错: 出错configure: error: You must have freetype installed; see http://www.freetype.org ...

  9. HDU 5047 Sawtooth(大数模拟)上海赛区网赛1006

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5047 解题报告:问一个“M”型可以把一个矩形的平面最多分割成多少块. 输入是有n个“M",现 ...

  10. Python验证Url地址的正则表达式

    如下是django中做url验证的正则表达式: regex = re.compile( r'^(?:http|ftp)s?://' # http:// or https:// r'(?:(?:[A-Z ...