题目大意:
找到几何中的4个数字使他们能够组成 a+b+c=d , 得到最大的d值

我们很容易想到a+b = d-c

那么将所有a+b的值存入hash表中,然后查找能否在表中找到这样的d-c的值即可

因为4个数字都不能相同,那么我们同时要在hash表中记录相加两个数的下标,然后查找的时候还要进行下标判断

这里用二分查找也可以,但是能用hash还是hash快地多了

这里第一次写到对负数进行hash,还是傻傻地 val%MOD , 但是负数得到的模值为负,作为hash的下标会RE,所以RE了一发,还是看别人的题解才找到这个错误,要谨记

遇到负数要先取绝对值后再hash

 #include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <climits>
using namespace std;
#define N 1010
#define MOD 1000007
#define base 31 struct HashNode{
int val , next;
int x , y;
}_hash[MOD]; int head[MOD+] , k , a[N]; void insertHash(int key , int x , int y)
{
int pos = ((key<)?-key:key)%MOD;
_hash[k].val = key , _hash[k].next = head[pos] , _hash[k].x=x , _hash[k].y=y;
head[pos] = k++;
} bool searchHash(int key , int a , int b)
{
int pos = ((key<)?-key:key)%MOD;
for(int i=head[pos] ; ~i ; i=_hash[i].next){
if(key == _hash[i].val){
if(a==_hash[i].x||a==_hash[i].y||b==_hash[i].x||b==_hash[i].y) continue;
return true;
}
}
return false;
} bool cmp(int a , int b)
{
return a>b;
} int main()
{
#ifndef ONLINE_JUDGE
freopen("a.in" , "r" , stdin);
#endif // ONLINE_JUDGE
int n;
while(scanf("%d" , &n) , n)
{
memset(head , - , sizeof(head));
k=;
for(int i= ; i<n ; i++) scanf("%d" , &a[i]);
sort(a , a+n , cmp);
for(int i= ; i<n ; i++){
for(int j=i+ ; j<n ; j++){
int v = a[i]+a[j];
insertHash(v , i , j);
}
}
int ret = INT_MIN;
for(int i= ; i<n ; i++){
for(int j= ; j<n ; j++){
if(i==j) continue;
int v = a[i]-a[j];
if(searchHash(v , i , j)){
ret = max(ret , a[i]);
break;
}
}
if(ret!=INT_MIN) break;
}
if(ret==INT_MIN) printf("no solution\n");
else printf("%d\n" , ret);
}
return ;
}

POJ 2549 Sumsets hash值及下标的更多相关文章

  1. UVA 10125 - Sumsets(POJ 2549) hash

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...

  2. POJ 2549 二分+HASH

    题目链接:http://poj.org/problem?id=2002 题意:给定一个n个数字组成的序列,然后求出4个数使得a+b+c=d,求d的最大值.其中a,b,c,d要求是给定序列的数,并且不能 ...

  3. POJ 2549 Sumsets(折半枚举+二分)

    Sumsets Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11946   Accepted: 3299 Descript ...

  4. POJ 2549 Sumsets

    Sumsets Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10593   Accepted: 2890 Descript ...

  5. [poj] 2549 Sumsets || 双向bfs

    原题 在集合里找到a+b+c=d的最大的d. 显然枚举a,b,c不行,所以将式子移项为a+b=d-c,然后双向bfs,meet int the middle. #include<cstdio&g ...

  6. POJ 2549:Subsets(哈希表)

    [题目链接] http://poj.org/problem?id=2549 [题目大意] 给出一个数集,从中选择四个元素,使得a+b+c=d,最小化d [题解] 我们对a+b建立Hash_table, ...

  7. POJ 1200 字符串HASH

    题目链接:http://poj.org/problem?id=1200 题意:给定一个字符串,字符串只有NC个不同的字符,问这个字符串所有长度为N的子串有多少个不相同. 思路:字符串HASH,因为只有 ...

  8. IOS9.0中hash值的bug与解决方案

    事件起因 事情是这样的:产品上线发布,突然出现了问题.运营Gg过来反应,当场给露珠演示,运营同事的手机是iphone,bug确实是存在的.奇怪的是露珠用了其他iphone手机(借别人的,露珠的是吊死安 ...

  9. Js获取后台集合List的值和下标的方法

    Js获取后台集合List的值和下标的方法 转载自:http://blog.csdn.net/XiaoKanZheShiJie/article/details/47280449 首先用的是struts2 ...

随机推荐

  1. ASP.NET MVC3 局部页面@RENDERBODY @RENDERPAGE@RENDERSECTION使用方法详细说明

    转载自:http://blog.163.com/wenchangqing_live/blog/static/173722309201211299817278/ asp.net mvc3局部页面使用方法 ...

  2. OpenJudge 7624 山区建小学

    在openjudge似乎无法凭题号搜到题...? 总时间限制:  1000ms  内存限制:  65536kB 描述 政府在某山区修建了一条道路,恰好穿越总共m个村庄的每个村庄一次,没有回路或交叉,任 ...

  3. 我对Padding Oracle Attack的分析和思考

    道哥的<白帽子讲web安全>有一章提到Padding Oracle Attack的攻击方式,据说这货在2011年的Pwnie Rewards上还被评为"最具价值的服务器漏洞&qu ...

  4. 2层Xml读取类

    配置文件 <?xml> <root> <parent name="C"> <child name="C1">Sp ...

  5. 关于updateElement接口

    1.bool UpdateGameElement(const struct_game_element& ele, gs_dbs_user_info_op_req& db_req, :: ...

  6. hdu 1048 The Hardest Problem Ever

    import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(Strin ...

  7. WINDOWS渗透与提权总结(2)

    vbs 下载者: 01 1: 02   03 echo Set sGet = createObject("ADODB.Stream") >>c:\windows\cft ...

  8. CentOS 7 firewalld使用简介

    1.firewalld简介 firewalld是centos7的一大特性,最大的好处有两个:支持动态更新,不用重启服务:第二个就是加入了防火墙的“zone”概念   firewalld有图形界面和工具 ...

  9. Spell checker(暴力)

    Spell checker Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 20188   Accepted: 7404 De ...

  10. TASKKILL命令使用大全

    Mr.Savin Mr.Savin 2009-08-07 183315TASKKILL [S system [U username [P [password]] { [FI filter] [PID ...