Divide and conquer:4 Values whose Sum is 0(POJ 2785)
题目大意:给定四个集合,要你每个集合选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)的更多相关文章
- 4 Values whose Sum is 0 POJ - 2785
4 Values whose Sum is 0 Time Limit: 15000MS Memory Limit: 228000K Total Submissions: 29243 Accep ...
- 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 ...
- 4 Values whose Sum is 0 POJ - 2785(二分应用)
题意:输入一个数字n,代表有n行a,b,c,d,求a+b+c+d=0有多少组情况. 思路:先求出前两个数字的所有情况,装在一个数组里面,再去求后两个数字的时候二分查找第一个大于等于这个数的位置和第一个 ...
- POJ 2785 4 Values whose Sum is 0(想法题)
传送门 4 Values whose Sum is 0 Time Limit: 15000MS Memory Limit: 228000K Total Submissions: 20334 A ...
- POJ 2785 4 Values whose Sum is 0
4 Values whose Sum is 0 Time Limit: 15000MS Memory Limit: 228000K Total Submissions: 13069 Accep ...
- 哈希-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: ...
- [poj2785]4 Values whose Sum is 0(hash或二分)
4 Values whose Sum is 0 Time Limit: 15000MS Memory Limit: 228000K Total Submissions: 19322 Accepted: ...
- K - 4 Values whose Sum is 0(中途相遇法)
K - 4 Values whose Sum is 0 Crawling in process... Crawling failed Time Limit:9000MS Memory Limi ...
- 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< ...
随机推荐
- linux:vi 替换命令
转自:http://www.cnblogs.com/afant/archive/2009/03/11/1408745.html :s/^.*$/\L&/100 ##将100行内的小写转换成大写 ...
- AngularJS API之copy深拷贝
angular提供了一个可以复制对象的api--copy(source,destination),它会对source对象执行深拷贝. 使用时需要注意下面几点: 如果只有一个参数(没有指定拷贝的对象), ...
- phpmyadmin中访问时出现2002 无法登录 MySQL 服务器
phpmyadmin中访问时出现2002 无法登录 MySQL 服务器! 解决方法如下: 修改phpmyadmin目录中libraries文件夹下的config.default.php文件 $cfg[ ...
- Spring配置bean文件的底层实现方式
首先 bean文件如下: <beans> <bean id="date" class="java.util.Date"></bea ...
- cocoapods pod install 安装报错 is not used in any concrete target
低版本的cocoa pods在编写Podfile文件时这样写就可以了 platform :ios, '8.0'pod 'AFNetworking' 高版本的cocoa pods在编写Podfile文件 ...
- php curl 库使用
支持http basic认证: curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); curl_setopt($ch, CURLOPT_USERPWD ...
- Asp.Net网页头部动态加载标题、描述、关键字、css和js文件的方法
首先head标签上添加runat="server". protected void Page_Load(object sender, EventArgs e) { Page.Tit ...
- CSS3实现背景颜色渐变 摘抄
一. Webkit浏览器 (1) 第一种写法: background:-webkit-gradient(linear ,10% 10%,100% 100%, color-stop(0.14,rgb(2 ...
- 关于man和help的区别
help 是内部命令的帮助,比如cdman 是外部命令的帮助,比如ls
- git 教程(11)--从远程库克隆
上次我们讲了先有本地库,后有远程库的时候,如何关联远程库. 现在,假设我们从零开发,那么最好的方式是先创建远程库,然后,从远程库克隆. 首先,登陆GitHub,创建一个新的仓库,名字叫gitskill ...