POJ 2785 4 Values whose Sum is 0(想法题)
| Time Limit: 15000MS | Memory Limit: 228000K | |
| Total Submissions: 20334 | Accepted: 6100 | |
| Case Time Limit: 5000MS | ||
Description
Input
Output
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 45
Sample Output
5
Hint
思路
题意:给定四个长度为n的数组A, B, C, D。 从每个数组中取一个数, 这样得到四个数, 并且这四个数的之和为0. 求这样组合的个数。
题解:直接算出组合数的话,复杂度太高,分成两堆来求,算出 A[i] + B[i] 的值,然后在A[i] + B[i]中找 等于 -C[i] - D[i] 的个数
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 4005;
int cd[maxn*maxn];
int main()
{
int N;
while (~scanf("%d",&N))
{
int a[maxn],b[maxn],c[maxn],d[maxn];
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++) cd[i*N+j] = c[i] + d[j];
sort(cd,cd + N*N);
int res = 0;
for (int i = 0;i < N;i++)
{
for (int j = 0;j < N;j++)
{
int tmp = -a[i] - b[j];
int pos1 = lower_bound(cd,cd + N*N,tmp) - cd;
int pos2 = upper_bound(cd,cd + N*N,tmp) - cd;
res += pos2 - pos1;
}
}
printf("%d\n",res);
}
return 0;
}
POJ 2785 4 Values whose Sum is 0(想法题)的更多相关文章
- POJ 2785 4 Values whose Sum is 0
4 Values whose Sum is 0 Time Limit: 15000MS Memory Limit: 228000K Total Submissions: 13069 Accep ...
- POJ - 2785 4 Values whose Sum is 0 二分
4 Values whose Sum is 0 Time Limit: 15000MS Memory Limit: 228000K Total Submissions: 25615 Accep ...
- POJ 2785 4 Values whose Sum is 0(折半枚举+二分)
4 Values whose Sum is 0 Time Limit: 15000MS Memory Limit: 228000K Total Submissions: 25675 Accep ...
- POJ 2785 4 Values whose Sum is 0(暴力枚举的优化策略)
题目链接: https://cn.vjudge.net/problem/POJ-2785 The SUM problem can be formulated as follows: given fou ...
- POJ 2785 4 Values whose Sum is 0(哈希表)
[题目链接] http://poj.org/problem?id=2785 [题目大意] 给出四个数组,从每个数组中选出一个数,使得四个数相加为0,求方案数 [题解] 将a+b存入哈希表,反查-c-d ...
- POJ 2785 4 Values whose Sum is 0 Hash!
http://poj.org/problem?id=2785 题目大意: 给你四个数组a,b,c,d求满足a+b+c+d=0的个数 其中a,b,c,d可能高达2^28 思路: 嗯,没错,和上次的 HD ...
- poj 2785 4 Values whose Sum is 0(折半枚举(双向搜索))
Description The SUM problem can be formulated . In the following, we assume that all lists have the ...
- [POJ] 2785 4 Values whose Sum is 0(双向搜索)
题目地址:http://poj.org/problem?id=2785 #include<cstdio> #include<iostream> #include<stri ...
- POJ 2785 4 Values whose Sum is 0 (二分)题解
思路: 如果用朴素的方法算O(n^4)超时,这里用折半二分.把数组分成两块,分别计算前后两个的和,然后枚举第一个再二分查找第二个中是否有满足和为0的数. 注意和有重复 #include<iost ...
随机推荐
- "Timeout"在测试框架里是如何被实现的
今天组里的小伙伴问了我一个问题:“我这里有一个底层驱动的接口,我想在测试它的时候加上超时限制,时间一过就fail掉它,执行后面的测试用例.怎么办到呢?”.我问:“它自己没有超时响应的机制么? 超时抛e ...
- HTML5+JS 《五子飞》游戏实现(三)页面和棋盘棋子
前面两节,我们已经对<五子飞>有个初步的认识,对走棋路线也有了基本的了解,现在里沃特继续跟大家分享HTML页面,另外把棋盘棋子也画出来. 演示地址:http://www.lyout.com ...
- 一例完整的websocket实现群聊demo
前言 业余我都会花一些时间在tcp.http和websocket等领域的学习,现在觉得有点收获,所以把一个基于websocket的群聊功能的例子提供给大家玩玩.当然这是一个很完整的例子,包括webso ...
- JSON返回DateTime/Date('123123123')/解决办法
Date.prototype.format = function (format) //author: meizz { var o = { "M+& ...
- Mysql 慢查询和慢查询日志分析
众所周知,大访问量的情况下,可添加节点或改变架构可有效的缓解数据库压力,不过一切的原点,都是从单台mysql开始的.下面总结一些使用过或者研究过的经验,从配置以及调节索引的方面入手,对mysql进行一 ...
- Linq的TakeWhile误用
where(func<>)返回符合条件的元素 与此相对的skipwhile跳过符合条件的,返回剩下的元素 容易误用的takewhile,与where 不一样,只有当所有元素满足条件时,才返 ...
- springMvc接受日期类型参数处理
这个问题,也即是springMvc如何进行参数类型的转换 以把client传过来一个String类型,转换为日期类型为例: 1.controller /** * 接收日期类型参数 * 注意: * sp ...
- AngularJS 日期转换字符串
日期转换成字符串的办法有很多种,其中最简单的方法是 使用AngularJS的filter来实现. $filter('date')(date, 'yyyyMM'): $filter('date')(da ...
- ps打造逼真印章效果
新建500*500px--->路径工具 最后通过 通道扣取选取,填充白色,即可
- 函数也是对象,本片介绍函数的属性、方法、Function()狗仔函数。
1.arguments.length表示实参的个数. 2.arguments.callee.length表示形参个数. function test(a,b,c,d,e,f){ alert(argume ...