题目:传送门

题目描述

You are given an integer array s[] and are asked to count how many positions a, b, c and d satisfy the condition: s[a] + s[b] + s[c] == s[d].
Note that a, b, c, and d do not need to be distinct.

输入

 The first line of input contains an integer T, indicates the cases.

Each of the next T blocks contains an integer n first (0< n ≤1000 ), the length of the array s[], following with n integers representing s[] (0≤ si≤10^6 ).

输出

 Output T lines each contains the answer required. You'd better use 'long long' instead of 'int'.

示例输入

4
4
0 0 0 0
2
1 3
4
1 10 100 111
1
3

示例输出

Case #1: 256
Case #2: 1
Case #3: 6
Case #4: 0 这题题意很简单,但是我比赛的时候没有做出来,当时看见几乎所有人都A了痛苦啊,就是一道简单哈希。

题意:给定一个序列,查找所有满足s[a]+s[b]+s[c]=s[d]的abcd的组数,注意,不要求abcd都不同。

分析:n<=1000,很明显O(n^3)的算法是不可能过的。式子可以转化为s[a]+s[b]=s[d]-s[c],然后复杂度就变成了O(n^2),先枚举ab,再枚举cd即可,用数组离散化。注意,因为s[a]+s[b]肯定是>=0,所以可以剪枝掉所有的s[d]-s[c]<0的情况。

#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string.h>
typedef long long ll;
using namespace std;
int n,a[],h[];
ll sum;
int main()
{
int T;
scanf("%d",&T);
for(int z=;z<=T;z++)
{
scanf("%d",&n);
sum=;
memset(h,,sizeof(h));
for(int i=;i<n;i++)
scanf("%d",&a[i]);
for(int i=;i<n;i++)
{
for(int j=;j<n;j++)
if(a[i]-a[j]>=)
h[a[i]-a[j]]++;
}
for(int i=;i<n;i++)
{
for(int j=;j<n;j++)
sum+=h[a[i]+a[j]];
}
printf("Case #%d: %lld\n",z,sum);
}
return ;
}
 

SDUT3141:Count(哈希)好题的更多相关文章

  1. HUD1686-Oulipo-kmp模板题/哈希模板题

    The French author Georges Perec (1936–1982) once wrote a book, La disparition, without the letter 'e ...

  2. D. Count the Arrays 计数题

    D. Count the Arrays 也是一个计数题. 题目大意: 要求构造一个满足题意的数列. \(n\) 代表数列的长度 数列元素的范围 \([1,m]\) 数列必须有且仅有一对相同的数 存在一 ...

  3. codevs哈希水题

    1230 多重hash练习一下,不用也可以 // // main.cpp // codeves1230 // // Created by Candy on 9/29/16. // Copyright ...

  4. Count the Sheep 思维题

    Altough Skipping the class is happy, the new term still can drive luras anxious which is of course b ...

  5. Codeforces Round #258 (Div. 2) D. Count Good Substrings 水题

    D. Count Good Substrings 题目连接: http://codeforces.com/contest/451/problem/D Description We call a str ...

  6. 【hdoj】哈希表题hdoj1425

    hdoj1425 github链接 #include<cstdio> #include<cstring> using namespace std; const int offs ...

  7. Codeforces 1188B - Count Pairs(思维题)

    Codeforces 题面传送门 & 洛谷题面传送门 虽说是一个 D1B,但还是想了我足足 20min,所以还是写篇题解罢( 首先注意到这个式子里涉及两个参数,如果我们选择固定一个并动态维护另 ...

  8. leetcode每日刷题计划-简单篇day4

    腰酸腿疼肝数模 被教育说代码风格像是小学生而且有点冗余 QAQ之前面试官好像也说过orz努力改努力改 今天把前两天跳过的vector给简单看了一下补上了 Num 14 最长公共前缀 Longest C ...

  9. TTTTTTTTTTTTTTTTTT hdu 1800 字符串哈希 裸题

    题意:意思是有若干个飞行员,需要在扫帚上练习飞行,每个飞行员具有不同的等级,且等级高的飞行员可以当等级低的飞行员的老师,且每个飞行员至多有且只有一个老师和学生.具有老师和学生关系的飞行员可以在同一把扫 ...

  10. 树hash/树哈希 刷题记录

    不同hash姿势: 树的括号序列最小表示法  s[i] 如果i为叶子节点:() 如果i的子节点为j1~jn:(s[j1]...s[jn]),注意s[j]要按照字典序排列

随机推荐

  1. 服务器的svnserver修改密码

    VisualSVN Server是一个集成的svn服务端工具,是一款svn服务端不可多得的好工具.可以先安装好VisualSVN Server后,运行VisualSVN Server Manger,然 ...

  2. Spring MVC属于SpringFrameWork的后续产品

    Spring MVC属于SpringFrameWork的后续产品,已经融合在Spring Web Flow里面.Spring MVC 分离了控制器.模型对象.分派器以及处理程序对象的角色,这种分离让它 ...

  3. sdut 2158:Hello World!(第一届山东省省赛原题,水题,穷举)

    Hello World! Time Limit: 1000MS Memory limit: 65536K 题目描述 We know that Ivan gives Saya three problem ...

  4. hdu 1425:sort(排序,经典题。快排模板)

    sort Time Limit : 6000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submissi ...

  5. 数据库已有时间索引,想再添加ID索引

    将时间索引的代码复制进去后,将编辑框的变量改为m_QueryID. 准备先以时间索引查找出大概数据,再直接使用ID索引精确查找. 于是想直接精确查ID为105的数据信息. 出现错误: 发现错误原因是忘 ...

  6. GO项目目录

    |--bin |--pkg |--src 其中,bin存放编译后的可执行文件:pkg存放编译后的包文件:src存放项目源文件.一般,bin和pkg目录可以不创建,go命令会自动创建(如 go inst ...

  7. Django学习笔记第八篇--实战练习四--为你的视图函数自定义装饰器

    零.背景: 对于登录后面所有视图函数,都需要验证登录信息,一般而言就是验证cookie里面的一些信息.所以你可以这么写函数: def personinfo(request): ": retu ...

  8. Memcached 之 .NET(C#)实例分析

    一:Memcached的安装 step1. 下载memcache(http://jehiah.cz/projects/memcached-win32)的windows稳定版(这里我下载了memcach ...

  9. 【BZOJ4155】[Ipsc2015]Humble Captains 最小割+DP

    [BZOJ4155][Ipsc2015]Humble Captains Description 每天下午放学时都有n个zky冲出教室去搞基.搞基的zky们分成两队,编号为1的zky是1号队的首领,编号 ...

  10. vue ios自带拼音全键输入法模糊查询兼容性问题

    ios的自带拼音全键会在输入框中输入拼音,直接在输入框用@keyup="autoInput()"的话,在监听输入事件的时候安卓显示正常, ios就会出现输入显示数据不灵敏 解决办法 ...