给定四个n元素集合 ABCD   要求分别从中取一个元素 abcd   使得他们的合为0   问有多少中取法

map果然炸了

#include<bits/stdc++.h>
using namespace std;
#define N 100000000
int a[N];
int b[N];
int c[N];
int d[N];
map<int,int>mp; int main()
{
int cas;cin>>cas; while(cas--)
{ mp.clear();
int n;cin>>n; for(int i=;i<=n;i++)
scanf("%d%d%d%d",&a[i],&b[i],&c[i],&d[i]) for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
mp[ a[i]+b[j] ]++; int cnt=;
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
{
int t=-c[i]-d[j];
cnt+=mp[t];
}
printf("%d\n",cnt);
}
return ;
}

用hash  但是不知道怎么放入哈希表里  因为每个元素最大值为2 28  总的为2 29  数组根本开不下

然后可以直接从小到大存入数组。。。

并且活用 upper-bound 和low bound !!!   值得学习!

#include<bits/stdc++.h>
using namespace std;
#define N 10000
int a[N];
int b[N],c[N],d[N]; int sum[N*N]; int main()
{
int cas;
cin>>cas;
while(cas--)
{
int n;cin>>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++)
sum[cnt++]=a[i]+b[j]; sort(sum,sum+cnt); long long ans=;
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
ans+=upper_bound(sum,sum+cnt,-d[j]-c[i])-lower_bound(sum,sum+cnt,-c[i]-d[j]);
printf("%ld\n",ans);
if(cas)printf("\n");
}
}

8-3 4Values Whose Sum is Zero 和为0的四个值的更多相关文章

  1. UVA 1152 4 Values Whose Sum is Zero 和为0的4个值 (中途相遇)

    摘要:中途相遇.对比map,快排+二分查找,Hash效率. n是4000的级别,直接O(n^4)肯定超,所以中途相遇法,O(n^2)的时间枚举其中两个的和,O(n^2)的时间枚举其他两个的和的相反数, ...

  2. sum(case when ct.tradeTotal >= 0 then 1 else 0 end)的意思

    String hql = "select new com.ks.admin.report.dto.ReportMonthWithDrawalDto(" + "count( ...

  3. UVA1152 4Values whose Sum is 0

    Description The SUM problem can be formulated as follows: given four lists A, B, C, D of integer val ...

  4. UVa 10891 - Game of Sum 动态规划,博弈 难度: 0

    题目 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...

  5. hive对有null值的列进行avg,sum,count等操作时会不会过滤null值

    在hive中,我们经常会遇到对某列进行count.sum.avg等操作计算记录数.求和.求平均值等,但这列经常会出现有null值的情况,那这些操作会不会过滤掉null能呢? 下面我们简单测试下: wi ...

  6. mysql 语句中 sum函数求和 null 变 0

    https://blog.csdn.net/Z_passionate/article/details/83821039

  7. iOS开发零碎知识点

    记录一些常用和不常用的iOS知识点,防止遗忘丢失.(来源为收集自己项目中用到的或者整理看到博客中的知识点),如有错误,欢迎大家批评指正:如有好的知识点,也欢迎大家联系我,添加上去.谢谢! 一.调用代码 ...

  8. LeetCode.942-DI字符串匹配(DI String Match)

    这是悦乐书的第361次更新,第388篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第223题(顺位题号是942).给定仅包含I(增加)或D(减少)的字符串S,令N = S ...

  9. [LeetCode] Path Sum III 二叉树的路径和之三

    You are given a binary tree in which each node contains an integer value. Find the number of paths t ...

随机推荐

  1. JS中的异步与回调

    问题的引出:在js中使用异步调用时,有可能会出现在异步的回调函数中设置调用之外的变量值,但在异步调用完成后去使用变量,却发现这些变量值并没有被成功设置的情况.如: google map中的地理编码,地 ...

  2. [转载]strtok函数和strtok_r函数

    1.一个应用实例 网络上一个比较经典的例子是将字符串切分,存入结构体中.如,现有结构体 typedef struct person{     char name[25];     char sex[1 ...

  3. HDU 1010 Tempter of the Bone (广搜+减枝)

    题目链接 Problem Description The doggie found a bone in an ancient maze, which fascinated him a lot. How ...

  4. Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A

    问题: 当我们打开数据库,即use dbname时,要预读数据库信息,当使用-A参数时,就不预读数据库信息. 解决方法:mysql -hhostname -uusername -ppassword - ...

  5. UNIX环境高级编程 第8章 进程控制

    本章是UNIX系统中进程控制原语,包括进程创建.执行新程序.进程终止,另外还会对进程的属性加以说明,包括进程ID.实际/有效用户ID. 进程标识 每个进程某一时刻在系统中都是独一无二的,它们之间是用一 ...

  6. JavaScript常用数组方法

    JavaScript数组方法有以下这些: forEach() 方法对数组的每一个元素执行一次提供的函数. map() 方法创建一个新数组,其结果是该数组都执行一次函数,原函数保持不变. filter( ...

  7. DRM/KMS 基本组件介绍

    Each DRM device provides access to manage which monitors and displays are currently used and what fr ...

  8. Linux中断处理驱动程序编写【转】

    转自:http://blog.163.com/baosongliang@126/blog/static/1949357020132585316912/ 本章节我们一起来探讨一下Linux中的中断 中断 ...

  9. 25 个常用的 Linux iptables 规则【转】

    转自 25 个常用的 Linux iptables 规则 - 文章 - 伯乐在线http://blog.jobbole.com/108468/ # 1. 删除所有现有规则 iptables -F # ...

  10. FastDFS集群部署

    之前介绍过关于FastDFS单机部署,详见博文:FastDFS+Nginx(单点部署)事例 下面来玩下FastDFS集群部署,实现高可用(HA) 服务器规划: 跟踪服务器1[主机](Tracker S ...