二分-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 ...
随机推荐
- 关于elementui的table组件单元格的内容自定义写法
------------恢复内容开始------------ 记录老哥的写法 columns是表格的配置文件 在表格渲染的时候通过renderTableCell传入表格的row以及配置文件中的rend ...
- pycharm中新建Vue项目时没有vue.js的解决办法
可能很多小伙伴在使用pycharm 1,新建vue项目的时候并没有发现vue.js的名字, 2,新建.vue文件(即单文件组件)的时候没有 下面就来帮助大家一下,仅供参考 如图: 1.首先我们打开设置 ...
- 论AMD内核如何使用Android Studio虚拟机
其实之前和老师求证过AMD内核要用虚拟机的话应求助第三方模拟器(不然速度太慢)或直接使用真机测试,当时拿着虚拟机的报错简单搜索了下只翻到了一个用谷歌第三方工具的(检索时关键词不对的锅),觉得麻烦就没去 ...
- Git的基本使用 -- 文件的添加、撤销、对比、删除
显示当前工作区.暂存区.仓库的状态 git status 当工作区的所有文件都提交到仓库,并和仓库保持一致时 有修改的文件时,会显示有改动的文件,并提示如何提交这些修改 添加到暂存区,还未提交到仓库时 ...
- iMacros 入门教程-基础函数介绍(4)
imacros的TRAY函数用法 这个函数的功能就是隐藏或显示,当执行imacros文件的时候,出现在特定标签的imacros图标 TRAY HIDE 就是隐藏图标 TRAY SHOW 就是显示图标 ...
- springboot专用的注解
这些是springboot特有的,常见的条件依赖注解有: @ConditionalOnBean,仅在当前上下文中存在某个bean时,才会实例化这个Bean. @ConditionalOnClass,某 ...
- Linux DMA访问的一致性
DMA访问的一致性 DMA对内存是直接访问的,而CPU对内存的访问有时会通过cache.不管是CPU还是DMA访问内存,都需要确保cache的一致性.本文只分析从DMA的角度,对内存的访问如何确保ca ...
- 为spring cloud config实现刷新动态掉的坑
正常搭建配置中心,网上教程多,这里不讨论,只记坑也是为了后来者少花时间在这里,由于是当时研究了好久才写的文章,所以只能提供问题的原因,当然会给出印证的思路,闲话不多说进入正题! 版本spring bo ...
- Win10查看屏保的所在位置
路径 C:\Users\Hlzy\AppData\Local\Packages\Microsoft.Windows.ContentDeliveryManager_cw5n1h2txyewy\Local ...
- ES的性能优化
ES的性能优化 es在数据量很大的情况下(数十亿级别)如何提高查询效率? 在es里,不要期待着随手调一个参数,就可以万能的应对所有的性能慢的场景.也许有的场景是你换个参数,或者调整一下语法,就可以搞定 ...