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 ...
随机推荐
- 翻译qmake文档(四) Building Common Project Types
翻译qmake文档 目录 本章原英文文档:http://qt-project.org/doc/qt-5/qmake-common-projects.html 构建常见的项目类型 本章描述 ...
- sql基本命令
--------------------------------------------------------SQL基本命令开始----------------------------------- ...
- struct socket 结构详解
Socket数据结构网络协议CC++ 用户使用socket系统调用编写应用程序时,通过一个数字来表示一个socket,所有的操作都在该数字上进行,这个数字称为套接字描述符.在系统调用 的实现函 ...
- SVN 修改log信息报错的解决方案
要实现允许修改log这个功能,只需要在hooks目录下增加一个名为:pre-revprop-change.bat的文件,重启svn即可.该文件内容为:------------------------- ...
- javascript 函数声明与函数表达式的区别
先看一段代码 var f = function g() { return 1; }; if (false) { f = function g(){ return 2; }; } alert(g()); ...
- 【CSS3】 线性渐变
参考地址:http://www.w3cplus.com/css3/new-css3-linear-gradient.html background-image: linear-gradient(to ...
- MVC认知路【点点滴滴支离破碎】【一】----新建数据库
1.App_Data文件夹创建[SQL Server Compact Local Database *]数据库 2.添加链接字符串<add name="MovieDBContext&q ...
- RabbitMQ集群、镜像部署配置
1 RABBITMQ简介及安装 RabbitMQ是一个开源的AMQP实现,服务器端用Erlang语言编写,支持多种客户端,如:Python.Ruby..NET.Java.JMS.C.PHP.Act ...
- 通过Keepalived实现Redis Failover自动故障切换功能
通过Keepalived实现Redis Failover自动故障切换功能[实践分享] 参考资料: http://patrick-tang.blogspot.com/2012/06/redis-keep ...
- IntelliJ idea的使用
1.快捷键 2.插件集成 附录:参考资料