10125 - Sumsets
Time limit: 3.000 seconds
http://uva.onlinejudge.org/index.php?option=onlinejudge&page=show_problem&problem=1066
Given S, a set of integers, find the largest d such that a + b + c = d where a, b, c, and d are distinct elements of S.
Input
Several S, each consisting of a line containing an integer 1 <= n <= 1000 indicating the number of elements in S, followed by the elements of S, one per line. Each element of S is a distinct integer between -536870912 and +536870911 inclusive. The last line
of input contains 0.
Output
For each S, a single line containing d, or a single line containing "no solution".
Sample Input
5
2
3
5
7
12
5
2
16
64
256
1024
0
Output for Sample Input
12
no solution
思路见代码注释。
复杂度:O(n^2 log n)
/*0.072s*/
#include<bits/stdc++.h>
using namespace std;
struct node
{
int a, b, sum; ///注意已经保证了a<b;
bool operator < (const node& a) const
{
return sum < a.sum;
}
} sum[499505], tmpsum;
int a[1005], tmp[4], n, c, ans, low, upp;
inline bool notequal()///判断这4个数是否互不相同
{
int i,j;
for (i = 0; i < 3; ++i)
for (j = i + 1; j < 4; ++j)
if (tmp[i] == tmp[j]) return false;
return true;
}
bool solve()
{
int i,j,k;
for (i = n - 1; i >= 0; --i)///从S中最大的枚举d
for (j = 0; j < n; ++j)///枚举c
{
if (j == i) continue;
tmp[0] = a[i], tmp[1] = a[j];///a[i]是d,a[j]是c
tmpsum = (node){a[i], a[j], a[i] - a[j]};///d-c
low = lower_bound(sum, sum + c, tmpsum) - sum, upp = upper_bound(sum, sum + c, tmpsum) - sum;///二分找d-c
for (k = low; k < upp; ++k)
{
tmp[2] = sum[k].a, tmp[3] = sum[k].b;///a和b
if (notequal())
{
ans = a[i];
return true;
}
}
}
return false;
}
int main()
{
int i,j;
while (scanf("%d", &n), n)
{
for (i = 0; i < n; ++i) scanf("%d", &a[i]);
if (n < 4)
{
puts("no solution");
continue;
}
sort(a, a + n);
c = 0;
for (i = 0; i < n - 1; ++i)
for (j = i + 1; j < n; ++j)
sum[c++] = (node){a[i], a[j], a[i] + a[j]};///枚举S中任意两个元素之和,并排序
sort(sum, sum + c);
if (solve()) printf("%d\n", ans);
else puts("no solution");
}
return 0;
}
一个更快的算法:(调整法)
/*0.019s*/
#include<bits/stdc++.h>
using namespace std;
int num[1005];
int answer, a, b, c, d, n, i;
bool find()
{
for (a = n - 1; a >= 0; --a)
{
for (b = n - 1; b > 1; --b)
{
if (a == b) continue;
answer = num[a] - num[b];
for (c = 0, d = b - 1; c < d;)
{
if (num[c] + num[d] == answer) return true;
else if (num[c] + num[d] < answer) ++c;
else --d;
}
}
}
return false;
}
int main()
{
while (scanf("%d", &n), n)
{
for (i = 0; i < n; ++i) scanf("%d", &num[i]);
sort(num, num + n);
if (find()) printf("%d\n", num[a]);
else puts("no solution");
}
return 0;
}
分享到:
相关推荐
折半查找(二分查找)折半查找(二分查找)折半查找(二分查找)折半查找(二分查找)折半查找(二分查找)折半查找(二分查找)
C语言折半查找、二分查找
折半查找(又称二分查找)是一种高效的查找算法,适用于已排序的数组或列表。其基本思想是将待查找的元素与数组中间元素进行比较,如果相等则返回该元素的下标;如果待查找元素小于中间元素,则在数组左半部分继续...
C++数据结构折半查找法二分查找法,算法设计新颖,有利于数据结构初学者的学习!
完整的实现了查找功能,并有被查数的前一个数和后一个数,能够更好的检验查找的正确性
折半查找,也称为二分查找(Binary Search)或对数查找(Logarithmic Search),是一种在有序数组中查找某一特定元素的搜索算法。搜索过程从数组的中间元素开始,如果中间元素正好是要查找的元素,则搜索过程结束;...
三种查找的方法,一点点东西,很简单,里面包括伪代码和解析。
折半(二分)查找算法C语言源程序,首先借助于快速排序算法对数组进行从小到大的排序,然后折半进行查找,直到找到相应数字为止。
该文档详细讲述了折半查找的优点和缺点,以及适用的情况,也写了一维数组和二维数组使用折半查找的代码,,,,,,,,
用分治法思想实现二分查找,Java语言描述。
这里本人自己写的是折半查找算法(又称二分查找)的c++代码的实现, 用的是递归的方法和非递归的方法, 里面的代码已经编译通过,并且优化好, 有需要的朋友可以下载借鉴一下
二分查找 C语言语言源代码 用递归写的 C语言入门经典代码 值得收藏
C#实现 二分查找 折半查找 visual studio 2012开发环境 具有图形化界面
折半查找算法,折半查找算法,折半查找算法
数据结构习题---折半查找代码 void BinInsert(int A[],int &n,int item) { int j,low=1,high=n,mid; while(low) { /* 利用折半查找法查找合适位置*/ mid=(low+high)/2; /* 计算当前查找部分的中间位置*/ if...
折半查找(二).
二分查找,也称为折半查找,是一种在有序数据集中查找特定元素的算法。它的高效性使得二分查找在多种应用场景中成为首选的搜索算法。下面我们将从原理、应用、性能分析及扩展变种等方面进行详细探讨。 二分查找的...
使用折半查找,输入一个整数,查找是否在数组中,如在给出下标,否则-1
数据结构折半查找,用于C语言版的数据结构。
折半查找是一种数据结构算法 非常有用 我们用C语言实现了查找 简单有效