二分-G - 4 Values whose Sum is 0
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 .Input
The first line of the input file contains the size of the lists n (this value can be as large as 4000). We then have n lines containing four integer values (with absolute value as large as 2 28 ) that belong respectively to A, B, C and D .Output
For each input file, your program has to write the number quadruplets whose sum is zero.Sample Input
6
-45 22 42 -16
-41 -27 56 30
-36 53 -37 77
-36 30 -75 -46
26 -38 -10 62
-32 -54 -6 45Sample Output
5Hint
Sample Explanation: Indeed, the sum of the five following quadruplets is zero: (-45, -27, 42, 30), (26, 30, -10, -46), (-32, 22, 56, -46),(-32, 30, -75, 77), (-32, -54, 56, 30).
#include<iostream>
#include<algorithm>
using namespace std; const int maxn = ;
const int maxm = *;
int a[maxn],b[maxn],c[maxn],d[maxn];
int ab[maxm],cd[maxm]; int main(){
int n;
scanf("%d",&n);
for(int i=;i<n;i++)
scanf("%d %d %d %d",a+i,b+i,c+i,d+i); int cnt = ;
for(int i=;i<n;i++)//前两列整数求和
for(int j=;j<n;j++)
ab[cnt++] = a[i] + b[j];
cnt = ;
for(int i=;i<n;i++)//后两列整数求和
for(int j=;j<n;j++)
cd[cnt++] = c[i] + d[j]; sort(ab,ab+n*n);
sort(cd,cd+n*n); int p = n*n-;
cnt = ;
for(int i=;i<n*n;i++){
for(;p>=;p--)
if(ab[i]+cd[p]<=) break;
if(p<) break;
for(int j=p;j>=;j--){
if(ab[i]+cd[j]==) cnt++;
if(ab[i]+cd[j]<) break;
}
}
printf("%d",cnt);
}
面对四列整数,一一组合将会造成极其庞大的数据,可将其中两两组合,组合以后再相加寻找和为0的情况。
二分-G - 4 Values whose Sum is 0的更多相关文章
- [poj2785]4 Values whose Sum is 0(hash或二分)
4 Values whose Sum is 0 Time Limit: 15000MS Memory Limit: 228000K Total Submissions: 19322 Accepted: ...
- POJ - 2785 4 Values whose Sum is 0 二分
4 Values whose Sum is 0 Time Limit: 15000MS Memory Limit: 228000K Total Submissions: 25615 Accep ...
- 4 Values whose Sum is 0(二分)
4 Values whose Sum is 0 Time Limit: 15000MS Memory Limit: 228000K Total Submissions: 21370 Accep ...
- POJ 2785:4 Values whose Sum is 0 二分
4 Values whose Sum is 0 Time Limit: 15000MS Memory Limit: 228000K Total Submissions: 18221 Accep ...
- 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< ...
- POJ 2785 4 Values whose Sum is 0(折半枚举+二分)
4 Values whose Sum is 0 Time Limit: 15000MS Memory Limit: 228000K Total Submissions: 25675 Accep ...
- UVA1152-4 Values whose Sum is 0(分块)
Problem UVA1152-4 Values whose Sum is 0 Accept: 794 Submit: 10087Time Limit: 9000 mSec Problem Desc ...
- POJ:2785-4 Values whose Sum is 0(双向搜索)
4 Values whose Sum is 0 Time Limit: 15000MS Memory Limit: 228000K Total Submissions: 26974 Accepted: ...
- 4 Values whose Sum is 0 POJ - 2785
4 Values whose Sum is 0 Time Limit: 15000MS Memory Limit: 228000K Total Submissions: 29243 Accep ...
随机推荐
- 第一天,初学Markdown
Markdown学习 二级标题 三级标题 字体 hello,world hello,world hello,world hello,world 引用 飞冲 分割线 图片 超链接 跳转到安徽科技学院 列 ...
- 在vue中继续使用layer.js来做弹出层---切图网
layer.js是一个方便的弹出层插件,切图网专注于PSD2HTML等前端切图多年,后转向Vue开发.在vue开发过程中引入layer.js的时候遇到了麻烦.原因是layer.js不支持import导 ...
- (一)LoadRunner安装
1.下载LR,双击exe安装程序,选择LoadRunner完整安装程序,如下图: 2.点击下一步 3.选择我同意,下一步 4.输入姓名和组织(可以不输入),下一步 5.点击浏览选择要安装的目录,建议使 ...
- 桌面粉笔功能.有window ink功能区开启的快捷键
功能区开启的快捷键 方法1: win+W唤出工作区,可以直接点击,但是没有快捷键.prtsc是直接截取屏幕(国际通用)然后在画图打开或直接粘贴于某处都可以. 方法2:快捷键是 Windows 徽标键 ...
- 使用elementui树形控件写项目小结
使用tree pagination serch table 实现功能 项目难点主要解析后台传递的代码,线性转树形,这儿加上一个大神的解析 https://blog.csdn.net/dandanzmc ...
- Go并发模式代码示例
演讲稿:Go Concurrency Patterns Youtube视频 作者:Rob Pike 练习题目:谷歌搜索:一个虚拟框架 谷歌搜索1.0 PPT从43页开始:https://talks.g ...
- Vim 全选命令
ggVG稍微解释一下上面的命令gg 让光标移到首行,在vim才有效,vi中无效V 是进入Visual(可视)模式G 光标移到最后一行选中内容以后就可以其他的操作了,比如:d 删除选中内容y ...
- windows系统安装部署python3.5和python2.7双解释器并存
前提材料准备: 下载对应版本的安装包:下载地址:https://www.python.org/downloads/windows/ python3.8.x安装包下载: python2.7.x安装包下载 ...
- 连接数据库的工具JdbcUtil
public class JdbcUtil { private static String driver=null;//驱动 private static String url=null;//连接地址 ...
- kali linux中mariadb加上密码
kali自带mysql.2019.4 中带得是:MariaDB.据说跟Mysql差不多.简单用了一下发现root用户可以不要密码进入Mysql! 这极不习惯,不输入密码感觉好像少了点什么.这肯定是权限 ...