poj 2785 4 Values whose Sum is 0(折半枚举(双向搜索))
2024-08-20 11:39:35
Description
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 = . 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 ). We then have n lines containing four integer values (with absolute value as large as ) 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
- -
- -
- -
- - -
- -
- - -
Sample Output
Hint
Sample Explanation: Indeed, the sum of the five following quadruplets is zero: (-, -, , ), (, , -, -), (-, , , -),(-, , -, ), (-, -, , ).
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <set>
using namespace std;
#define ll long long
#define N 4006
int n;
int mp[N][N];
int A[N],B[N],C[N],D[N];
int CD[N*N];
void solve(){
for(int i=;i<n;i++){
for(int j=;j<n;j++){
CD[i*n+j]=C[i]+D[j];
}
}
sort(CD,CD+n*n);
ll ans=;
for(int i=;i<n;i++){
for(int j=;j<n;j++){
int cd = -(A[i]+B[j]);
ans+=upper_bound(CD,CD+n*n,cd)-lower_bound(CD,CD+n*n,cd);
}
}
printf("%I64d\n",ans);
}
int main()
{
while(scanf("%d",&n)==){
for(int i=;i<n;i++){
for(int j=;j<;j++){
scanf("%d",&mp[i][j]);
}
}
for(int i=;i<n;i++){
A[i] = mp[i][];
B[i] = mp[i][];
C[i] = mp[i][];
D[i] = mp[i][];
}
solve();
}
return ;
}
最新文章
- 移动web基本知识
- Quartz2D 编程指南(四)位图与图像遮罩、CoreGraphics 绘制 Layer
- 详解Android中AsyncTask的使用
- 在SharePoint 2013中显示“以其他用户身份登录”
- Java-马士兵设计模式学习笔记-代理模式-动态代理 调用Proxy.newProxyInstance()
- HDU 1847 (博弈 找规律) Good Luck in CET-4 Everybody!
- AO创建IFeature的两种方法
- mybatis中为sql中传值#{}和${}的区别
- c# TCP/IP编程
- Ubuntu中PyCharm中字体设置
- WPF仿微软事件和属性窗体,效果更炫!
- asp.net mvc 实现记忆返回的功能
- HDOJ(HDU) 2143 box(简单的多次判断-用的卫条件)
- matrix矩阵求逆 与解方程模板 留做备用 (有bug,待补充)
- C++から広がり
- 响应的系统设置的事件——重写onConfigurationChanged响应系统设置更改
- Linux下一些命令
- MVC 前端页面ViewData参数名不区分大小写
- 微观:心流,宏观:ikigai
- Linux 基本使用