UVA 10125 - Sumsets
链结:戳我。
AC了。这道题我参考了这里:狠狠地戳我。我觉得他的解释非常的棒:
找到a + b的和的所有情况。
然后看d - c在不在里面。
最后算法是O(n^2log(n))
1 /* 2 Author: frankdj 3 State: AC 4 */ 5 #include <iostream> 6 #include <cstring> 7 #include <algorithm> 8 using namespace std; 9 10 struct Node{ 11 long long value; 12 int x; 13 int y; 14 }; 15 16 const int kMaxNumEle = 1001; 17 const int kMaxHashSize = 100000; 18 19 Node sum[kMaxNumEle*kMaxNumEle]; 20 int elements[kMaxNumEle], num_element, num_sum; 21 int head[kMaxHashSize], next[kMaxNumEle*kMaxNumEle]; 22 23 void InitLookupTable() { 24 memset(head, 0, sizeof(head)); 25 memset(next, 0, sizeof(next)); 26 } 27 28 int Hash(long long key) { 29 return (int)((key & 0x7fffffff) % kMaxHashSize); 30 } 31 32 void Insert(int index) { 33 int hash = Hash(sum[index].value); 34 next[index] = head[hash]; 35 head[hash] = index; 36 } 37 38 bool IsDifferent(int i, int j, int index) { 39 int x = sum[index].x; 40 int y = sum[index].y; 41 if (i == x || i == y || j == x || j == y) 42 return false; 43 else 44 return true; 45 } 46 47 bool Find(long long number, int i, int j) { 48 int hash = Hash(number); 49 int index = head[hash]; 50 while (index != 0) { 51 if (sum[index].value == number) 52 if (IsDifferent(i, j, index)) 53 return i; 54 else 55 index = next[index]; 56 else 57 index = next[index]; 58 } 59 return false; 60 } 61 62 int Search() { 63 for (int i = num_element; i >= 1; i--){ 64 for (int j = 1; j != i && j <= num_element; j++){ 65 if (Find(elements[i] - elements[j], i, j)) 66 return i; 67 } 68 } 69 return 0; 70 } 71 72 73 int main(int argc, char *argv[]){ 74 #ifndef ONLINE_JUDGE 75 freopen("input.txt", "r", stdin); 76 #endif 77 78 while (cin >> num_element && num_element != 0) { 79 memset(sum, 0, sizeof(sum)); 80 memset(elements, 0, sizeof(elements)); 81 82 for (int i = 1; i <= num_element; i++) 83 cin >> elements[i]; 84 85 sort(elements+1, elements+num_element+1); 86 87 num_sum = 1; 88 for (int i = 1; i <= num_element; i++){ 89 for (int j = i+1; j <= num_element; j++){ 90 sum[num_sum].value = elements[i] + elements[j]; 91 sum[num_sum].x = i; 92 sum[num_sum].y = j; 93 Insert(num_sum); 94 num_sum++; 95 } 96 } 97 num_sum--; 98 99 int ans = Search(); 100 if (ans != 0) { 101 cout << elements[ans] << endl; 102 } 103 else { 104 cout << "no solution" << endl; 105 } 106 } 107 return 0; 108 }
转载于:https://www.cnblogs.com/frankdj412/archive/2013/02/19/2916398.html
UVA 10125 - Sumsets相关推荐
- 【HASH】【UVA 10125】 Sumset
传送门 Description 给定一个整数集合S,求一个最大的d,满足a+b+c=d,其中a,b,c,d∈S Input 多组数据,每组数据包括: 第一行一个整数n,代表元素个数 下面n行每行一个整 ...
- 提取了下刘汝佳推荐的题号...
今天闲来没事上uva oj提取了下刘汝佳推荐的acm题号,原始数据如下: Volume 0. Getting Started 10055 - Hashmat the Brave Warrior ...
- 初学者acm的练习题指南
上机练习题参考题 忘了在哪找的啦~~希望对大家有帮助呦 <!--[if !supportLists]-->1. <!--[endif]-->Programming Bas ...
- [搜索]UVa 129 困难的串
题意:将一个包含两个相邻的重复子串的子串,称为"容易的串",其他为"困难的串". 输入正整数n和l,输出由前l个字符组成的,字典序第n小的困难的串. 输入样例: ...
- uva 401.Palindromes
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
- Uva 3767 Dynamic len(set(a[L:R])) 树套树
Dynamic len(set(a[L:R])) Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 https://uva.onlinejudge.org/in ...
- UVA 11752 超级幂
UVA 11752 超级幂 Z - The Super Powers Time Limit:1000MS Memory Limit:0KB 64bit IO Format:%lld & ...
- UVa 11174 - Stand in a Line
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...
- UVa 10112 - Myacm Triangles
UVa第一卷最后一题. 求内部不含点并且面积最大的三角形. 暴力. 代码如下: 1 #include<iostream> 2 #include<cstdio> 3 #inclu ...
- UVa 10180 - Rope Crisis in Ropeland!
题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=41&pa ...
最新文章
- Linux_文件系统、磁盘分区_RHEL7
- 直接用Win32 API创建对话框Demo
- 产品经理必备的14大效率工具
- 化工原理 蒸馏(上)
- 性能测试常用的软件有哪些,常用的正经CPU测试软件有哪些
- (笔记)java环境变量设置
- 从0-1背包问题学习回溯法、分支界限法、动态规划
- 使用Hadoop自带的例子pi计算圆周率
- phoenix创建索引报错“ Mutable secondary indexes must have the hbase.regionserver.wal.codec property”
- java堆排序解决topk问题,详解堆排序解决TopK问题
- bzoj 1615: [Usaco2008 Mar]The Loathesome Hay Baler麻烦的干草打包机
- 逻辑运算符 用法解释
- c语言如何引用一维数组,C语言一维数组的定义和引用
- 计算机图形学---纹理及纹理隐射
- 韩寒式的幽默-屌丝回忆录
- Mediator模式(仲裁者设计模式)
- 阿里巴巴编码规范认证
- 科学计算机后盖换电池,图吧小白教程 篇二十二:手把手教你给手机换电池(拆机)...
- Github:Semantic-Segmentation-Suite分割网络集锦--使用小结
- 2019年,SEO关键词KPI考核指标有哪些?