【例题 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 ...
随机推荐
- 85.explicit作用
#include <iostream> using namespace std; class myclass { public: int num; public: explicit myc ...
- Linux常用系统管理软件
1.BleachBit是一款开源的系统清理工具,它可以释放磁盘空间,保护您的隐私,清除缓存,删除cookies.internet历史.临时文件.日志和丢弃的垃圾文件等,支持清除应用的残留数据,切碎文件 ...
- Python环境搭建—安利Python小白的Python安装详细教程
人生苦短,我用Python.众所周知,Python目前越来越火,学习Python的小伙伴也越来越多.最近看到群里的小伙伴经常碰到不会安装Python或者不知道去哪下载Python安装包等系列问题,为了 ...
- 【RHEL7/CentOS7基本配置】
目录 @ 相比于6.x的版本,Rhel7/CentOS7增加或改进了以下7大特性. 1.身份管理 kerberos的跨平台信任机制:kerberos将完全兼容微软活动目录,实现完全使用活动目录进行认证 ...
- jquery正则匹配URL地址
JQuery代码: var regexp = /((http|ftp|https|file):\/\/([\w\-]+\.)+[\w\-]+(\/[\w\u4e00-\u9fa5\-\.\/?\@\% ...
- 控制div固定在页面的某个位置 ,用js感觉很麻烦 CSS更好一些
CSS代码:<style type="text/css"> html,body { width:100%; height:100%; margin:0px; paddi ...
- PMP杂谈--制约因素与如果条件
制约因素和如果条件都是项目范围说明书的内容.它们是项目范围基准的一部分,是定义活动.估算活动持续时间.制定进度计划.估算成本.制定预算.识别风险和规划採购等多个过程的输入. 差别:制约因素是确定 ...
- 基于TI Davinci架构的多核/双核开发高速扫盲(以OMAP L138为例),dm8168多核开发參考以及达芬奇系列资料user guide整理
基于TI Davinci架构的双核嵌入式应用处理器OMAPL138开发入门 原文转自http://blog.csdn.net/wangpengqi/article/details/8115614 感谢 ...
- 54.nodejs nodemailer不兼容
转自:https://blog.csdn.net/q36835109/article/details/53067917 注:由于本人使用最新版本的nodemailer不兼容,所以目前使用的是0.7.1 ...
- 【转】BAT取当前日期的前一天
============================取前一天的日期 OK============================@echo off ::前一天的日期,格式化输出echo ...