题目:传送门

题目描述

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. 【BZOJ】1050: [HAOI2006]旅行comf(暴力+并查集)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1050 表示被暴力吓到了orz 我竟然想不到...我竟然还想到分数规划,,但是不可做...然后又想到最 ...

  2. c#用run32dll打开系统dll(如系统图片查看器,并置最顶层)

    [DllImport("user32.dll", EntryPoint = "SetWindowPos",CharSet = CharSet.Auto)] st ...

  3. MyBatis 3.2.6插入时候获取自增主键方法

    MyBatis 3.2.6插入时候获取自增主键方法有二 以MySQL5.5为例: 方法1: <insert id="insert" parameterType="P ...

  4. freemarker0

    assign  用于为该模板页面 创建或替换一个顶层变量 或创建或替换多个顶层变量 列子如下 <#assign name=value [in namespacehash]>,指定一个名为n ...

  5. VC++ 设置桌面壁纸

    Windows Shell API提供了接口IActiveDesktop来完成墙纸的设置. //IActiveDesktop 接口方法表 (详情参见MSDN) AddDesktopItem AddDe ...

  6. java后台高德经纬度转地理位置信息

    import java.util.LinkedList;import java.util.List; import org.apache.http.HttpResponse;import org.ap ...

  7. WCF:并发处理

    当多个线程同时访问相同的资源的时候就会产生并发,WCF缺省情况下会保护并发访问.对并发访问需要恰当处理,控制不好不仅会大大降低WCF服务的吞吐量和性能,而且还有可能会导致WCF服务的死锁.一.WCF并 ...

  8. PHP mysql基本语句指令

    /*选择数据库 use test; */ /* 显示所有的数据库 show databases; */ /*删除表/数据库 drop database test1; delete from user1 ...

  9. LeetCode 笔记系列三 3Sum

    题目:Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all ...

  10. 实现返回top功能

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...