例题 8-3 UVA - 1152 4 Values whose Sum is 0(和为0的4个值)(二分枚举)
来源:互联网 发布:淘宝网店申请多少钱 编辑:程序博客网 时间:2024/05/29 04:13
题意:
给你4个n 元素的集合ABCD,要求分别从中选取一个元素abcd使得a + b + c + d = 0问有多少种选法!
思路:
先把A数组和B数组所有的组合情况都记录到tmp数组,排序,然后枚举C数组和D数组,取相反数,然后二分枚举tmp数组中值,
取上界upper_bound 和取下界 lower_bound 做差即可!
#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int maxn = 5000 + 5;int a[maxn],b[maxn],c[maxn],d[maxn];int tmp[maxn*maxn];int main(){ int T,cnt2 = 0; scanf("%d",&T); while(T--){ int n,cnt=0; scanf("%d",&n); for (int i = 0; i < n; ++i){ scanf("%d%d%d%d",&a[i],&b[i],&c[i],&d[i]); } for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) tmp[cnt++] = a[i] + b[j]; sort(tmp,tmp+cnt); int ans = 0; for (int i = 0; i < n; ++i){ for (int j = 0; j < n; ++j){ int k = -c[i]-d[j]; int pos1 = upper_bound(tmp,tmp+cnt,k) - tmp; int pos2 = lower_bound(tmp,tmp+cnt,k) - tmp; ans += pos1-pos2; } } if (cnt2++)printf("\n"); printf("%d\n",ans); } return 0;}
0 0
- 例题 8-3 UVA - 1152 4 Values whose Sum is 0(和为0的4个值)(二分枚举)
- 例题8-3 和为0的4个值(4 Values Whose Sum is Zero, ACM/ICPC SWERC 2005, UVa 1152)
- 和为0的4个值(4 Values Whose sum is Zero UVa 1152)
- [UVa 1152] 和为0的四个值(4 Values whose Sum is 0)
- UVA1152:4 Values whose Sum is 0(和为0的4个值)
- UVa #1152 4 Values whose Sum is 0 (例题8-3)
- UVA 1152 4 Values whose Sum is 0 (二分)
- UVA 1152 --4 Values whose Sum is 0(枚举--中途相遇法)
- Uva1152 4 Values whose Sum is 0 【中途相遇+二分】【例题8-3】
- poj 2785 4 Values whose Sum is 0 (二分+枚举)
- POJ - 2785 4 Values whose Sum is 0(二分枚举)
- POJ2785 -- 4 Values whose Sum is 0(折半枚举)
- 4 Values whose Sum is 0 (折半枚举)
- POJ2785 4 Values whose Sum is 0(折半枚举+二分)
- poj 2785 4 Values whose Sum is 0(二分)
- poj 2785 4 Values whose Sum is 0 (二分)
- POJ2785:4 Values whose Sum is 0(二分+暴力)
- (二分)4 Values whose Sum is 0
- java 死锁及解决
- 字符编码简介:ASCII,Unicode,UTF-8,GB2312及Unicode和UTF-8如何转化
- c 语言模块接口函数在main之前注册
- JUC源码分析14-集合-HashMap
- Dplus的玩法
- 例题 8-3 UVA - 1152 4 Values whose Sum is 0(和为0的4个值)(二分枚举)
- 使用MD5对明文密码进行加密
- 公司后台商品添加操作流程
- java 常用的几种排序方式
- 【JQ】jquery如何判断一个节点下面没有子节点方法整理
- android默认系统日期、时间、时区更改
- 图像处理-线性滤波-2 图像微分(1、2阶导数和拉普拉斯算子)
- treemap
- 快排代码示例