【例题 8-3 UVA - 1152】4 Values whose Sum is 0
【链接】 我是链接,点我呀:)
【题意】
在这里输入题意
【题解】
显然中间相遇。
自己写了个hash处理一下冲突就可以了。
【代码】
/*
1.Shoud it use long long ?
2.Have you ever test several sample(at least therr) yourself?
3.Can you promise that the solution is right? At least,the main ideal
4.use the puts("") or putchar() or printf and such things?
5.init the used array or any value?
6.use error MAX_VALUE?
7.use scanf instead of cin/cout?
8.whatch out the detail input require
*/
/*
一定在这里写完思路再敲代码!!!
*/
#include <bits/stdc++.h>
const int MOD = 2000007;
using namespace std;
const int N = 4e3;
struct node{
int x,num;
node *nex;
node(){
x = 0;num = 0;nex=NULL;
}
node(int xx){
x = xx;num = 1;nex=NULL;
}
};
int n,a[4][N+10];
node *Hash[MOD+10];
void inc(int temp){
int x = abs(temp)%MOD;
node *p = Hash[x];
while (p->nex!=NULL){
p = p->nex;
if(p->x==temp){
p->num++;
return;
}
}
p->nex = new node(temp);
}
int getnum(int temp){
int x= abs(temp)%MOD;
node *p = Hash[x];
while (p->nex!=NULL){
p = p->nex;
if(p->x==temp) return p->num;
}
return 0;
}
int main(){
#ifdef LOCAL_DEFINE
freopen("rush_in.txt", "r", stdin);
#endif
int T,kase = 0;
scanf("%d",&T);
while (T--){
for (int i = 0;i < MOD;i++) Hash[i] = new node();
if (kase>0) puts("");
kase++;
scanf("%d",&n);
for (int i = 1;i <= n;i++)
for (int j = 0;j < 4;j++)
scanf("%d",&a[j][i]);
for (int i = 1;i <= n;i++)
for (int j = 1;j <= n;j++)
inc(-a[0][i]-a[1][j]);
long long ans = 0;
for (int i = 1;i <= n;i++)
for (int j = 1;j <= n;j++)
ans += getnum(a[2][i]+a[3][j]);
printf("%lld\n",ans);
}
return 0;
}
【例题 8-3 UVA - 1152】4 Values whose Sum is 0的更多相关文章
- UVA 1152 4 Values whose Sum is 0 (枚举+中途相遇法)(+Java版)(Java手撕快排+二分)
4 Values whose Sum is 0 题目链接:https://cn.vjudge.net/problem/UVA-1152 ——每天在线,欢迎留言谈论. 题目大意: 给定4个n(1< ...
- UVa 1152 -4 Values whose Sum is 0—[哈希表实现]
The SUM problem can be formulated as follows: given four lists A, B, C, D of integer values, compute ...
- UVa 1152 4 Values whose Sum is 0
题意:给出n,四个集合a,b,c,d每个集合分别有n个数,分别从a,b,c,d中选取一个数相加,问使得a+b+c+d=0的选法有多少种 看的紫书,先试着用hash写了一下, 是用hash[]记录下来a ...
- UVA - 1152 4 Values whose Sum is 0问题分解,二分查找
题目:点击打开题目链接 思路:暴力循环显然会超时,根据紫书提示,采取问题分解的方法,分成A+B与C+D,然后采取二分查找,复杂度降为O(n2logn) AC代码: #include <bits/ ...
- UVA - 1152 --- 4 Values whose Sum is 0(二分)
问题分析 首先枚举a和b, 把所有a+b记录下来放在一个有序数组,然后枚举c和d, 在有序数组中查一查-c-d共有多少个.注意这里不可以直接用二分算法的那个模板,因为那个模板只能查找是否有某个数,一旦 ...
- UVA - 1152 4 Values whose Sum is 0(中途相遇法)
题意:从四个集合各选一个数,使和等于0,问有多少种选法. 分析:求出来所有ai + bi,在里面找所有等于ci + di的个数. #pragma comment(linker, "/STAC ...
- uva 1152 4 values whose sum is zero ——yhx
The SUM problem can be formulated as follows: given four lists A;B;C;D of integer values, computehow ...
- UVA 1152 4 Values Whose Sum is Zero 和为0的4个值 (中途相遇)
摘要:中途相遇.对比map,快排+二分查找,Hash效率. n是4000的级别,直接O(n^4)肯定超,所以中途相遇法,O(n^2)的时间枚举其中两个的和,O(n^2)的时间枚举其他两个的和的相反数, ...
- K - 4 Values whose Sum is 0(中途相遇法)
K - 4 Values whose Sum is 0 Crawling in process... Crawling failed Time Limit:9000MS Memory Limi ...
- POJ 2785 4 Values whose Sum is 0(想法题)
传送门 4 Values whose Sum is 0 Time Limit: 15000MS Memory Limit: 228000K Total Submissions: 20334 A ...
随机推荐
- uestc 94(区间更新)
题意:有一个字符串全部由'('和')'组成.然后有三种操作,query a b输出区间[a,b]字符串的括号序列是否合法.reverse a b把区间[a,b]字符串里全部'('替换成')',而且把全 ...
- RequestMapping、Responsebody、RequestBody
预备知识:@RequestMappingRequestMapping是一个用来处理请求地址映射的注解,可用于类或方法上.用于类上,表示类中的所有响应请求的方法都是以该地址作为父路径.@RequestM ...
- HIVE JOIN_1
HIVE JOIN 概述 Hive join的实现包含了: Common (Reduce-side) Join Broadcast (Map-side) Join Bucket Map Join So ...
- SQL中union union all 和in的查询效率问题
UNION用的比较多union all是直接连接,取到得是所有值,记录可能有重复 union 是取唯一值,记录没有重复 1.UNION 的语法如下: [SQL 语句 1] UNION [SQL 语句 ...
- Java:JDBC操作
内容:供程序员调用的接口与类,集成在java.sql和javax.sql包中,如:DriverManager类Connection接口Statement接口ResultSet接口 1.Class.fo ...
- 运输层协议——UDP
UDP概述: UDP只是做了运输层协议能做的最少工作,仅做了复用/分解,少量的差错检验. UDP是无连接的. UDP优点: 关于何时.发送什么数据的应用层控制更为精细:TCP在拥堵时会遏制发送方的发送 ...
- Windows IP 设置脚本
前言: 有时候,总要在不同地方工作,就会总要切换 IP,内网比较严重,内网大多数都是静态 IP 所以呢,老是手动去设置 IP.子网掩码.网关等甚是繁琐,同时还得记住 IP,所以呢,用脚本来记录 IP, ...
- Python中import和from的一些事。。。
摘自python学习手册, 用于记录. 客户端可以执行import或from语句.如果模块还没有加载,这两个语句会去搜索.编译以及执行模块文件程序.主要差别在于,import会读取整个模块,所以必须进 ...
- Android NDK课程录制完毕上线
近期依据公司安排,录制了Android NDK开发的内容,总体课程能够在www.iotekclass.com上看到.兴许会慢慢的用blog把这些内容展示出来. 此课程大纲例如以下: 第一章 NDK简单 ...
- 手动脱FSG壳实战
作者:Fly2015 对于FSG壳.之前没有接触过是第一次接触.这次拿来脱壳的程序仍然是吾爱破解论坛破解培训的作业3的程序.对于这个壳折腾了一会儿,后来还是被搞定了. 1.查壳 首先对该程序(吾爱破解 ...