8-3 4Values Whose Sum is Zero 和为0的四个值
给定四个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的四个值的更多相关文章
- UVA 1152 4 Values Whose Sum is Zero 和为0的4个值 (中途相遇)
摘要:中途相遇.对比map,快排+二分查找,Hash效率. n是4000的级别,直接O(n^4)肯定超,所以中途相遇法,O(n^2)的时间枚举其中两个的和,O(n^2)的时间枚举其他两个的和的相反数, ...
- sum(case when ct.tradeTotal >= 0 then 1 else 0 end)的意思
String hql = "select new com.ks.admin.report.dto.ReportMonthWithDrawalDto(" + "count( ...
- 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 ...
- UVa 10891 - Game of Sum 动态规划,博弈 难度: 0
题目 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...
- hive对有null值的列进行avg,sum,count等操作时会不会过滤null值
在hive中,我们经常会遇到对某列进行count.sum.avg等操作计算记录数.求和.求平均值等,但这列经常会出现有null值的情况,那这些操作会不会过滤掉null能呢? 下面我们简单测试下: wi ...
- mysql 语句中 sum函数求和 null 变 0
https://blog.csdn.net/Z_passionate/article/details/83821039
- iOS开发零碎知识点
记录一些常用和不常用的iOS知识点,防止遗忘丢失.(来源为收集自己项目中用到的或者整理看到博客中的知识点),如有错误,欢迎大家批评指正:如有好的知识点,也欢迎大家联系我,添加上去.谢谢! 一.调用代码 ...
- LeetCode.942-DI字符串匹配(DI String Match)
这是悦乐书的第361次更新,第388篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第223题(顺位题号是942).给定仅包含I(增加)或D(减少)的字符串S,令N = S ...
- [LeetCode] Path Sum III 二叉树的路径和之三
You are given a binary tree in which each node contains an integer value. Find the number of paths t ...
随机推荐
- 2017 清北济南考前刷题Day 4 morning
考场思路: 倒着算就是 可以对一个数-1 可以合并两个数 可以证明只有0和0才能执行合并操作 然后模拟 #include<cstdio> #include<iostream> ...
- [ONTAK2015]Bajtman i Okrągły Robin
bzoj 4276: [ONTAK2015]Bajtman i Okrągły Robin Time Limit: 40 Sec Memory Limit: 256 MB Description 有 ...
- Android端抓取日志
一.背景: ADT-Bundlee for Windows 是由GoogleAndroid官方提供的集成式IDE,已经包含了Eclipse,你无需再去下载Eclipse,并且里面已集成了插件,它解决大 ...
- mysql的force index
MSQL中使用order by 有个坑,会默认走order by 后面的索引.而不走where条件里应该走的索引.大家在使用时要绕过此坑. 如下语句因为order by 走了settle_id这个主键 ...
- xgboost与sklearn的接口
xgb使用sklearn接口(推荐) XGBClassifier from xgboost.sklearn import XGBClassifier clf = XGBClassifier( sile ...
- 十大opengl教程
正文: 1. http://nehe.gamedev.net/ 这个是我觉得全世界最着名的OpenGL教程,并且有网友将其中48个教程翻译成了中文http://www.owlei.com/Dancin ...
- golang的json序列化
json就是简单的数据交换格式,语法类似javascript的对象和列表,是最常见的后端和运行在网页上的js之间的通信格式. encoding: 编码json数据需要使用到Marshal()函数. f ...
- DirectFB简介以及移植[一]【转】
转自:https://blog.csdn.net/wavemcu/article/details/39251805 ****************************************** ...
- aarch64_n1
NFStest-2.1.5-0.fc26.noarch.rpm 2017-02-17 01:19 531K fedora Mirroring Project NLopt-2.4.2-11.fc26.a ...
- asp.net操作word 配置在IIS上出现的问题
异常: 检索 COM 类工厂中 CLSID 为 {000209FF-0000-0000-C000-000000000046} 的组件失败,原因是出现以下错误: 80070005 拒绝访问. (异常来自 ...