poj 2549 Sumsets (枚举+二分)
2016-03-31 21:06
429 查看
Sumsets
Description
![](http://poj.org/images/2549_1.jpg)
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
Sample Output
Source
Waterloo local 2001.06.02
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int n,data[1005];
int main ()
{
while (scanf("%d",&n),n)
{
int ans,i;
bool flag=true;
for (i=0;i<n;i++)
scanf("%d",&data[i]);
sort(data,data+n);
int sz = unique(data,data+n)-data; //去重
n=sz;
for (int d=n-1;d>=0 && flag;d--) //枚举d
for (i=0;i<=n-4 && flag;i++) //注意因为有负数存在,需要扫完全部
{
int head=i+1,tail=n-1; //从首尾两个方向扫描
while (head<tail)
{
int tmp=data[i]+data[head]+data[tail];
if (tmp==data[d])
{
if (i==d || head==d || tail==d) break;
flag=false;
ans=tmp;
break;
}
else if (tmp>data[d]) tail--;
else head++;
}
}
if (flag)
printf("no solution\n");
else
printf("%d\n",ans);
}
return 0;
}
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 10026 | Accepted: 2743 |
![](http://poj.org/images/2549_1.jpg)
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
Sample Output
12 no solution
Source
Waterloo local 2001.06.02
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int n,data[1005];
int main ()
{
while (scanf("%d",&n),n)
{
int ans,i;
bool flag=true;
for (i=0;i<n;i++)
scanf("%d",&data[i]);
sort(data,data+n);
int sz = unique(data,data+n)-data; //去重
n=sz;
for (int d=n-1;d>=0 && flag;d--) //枚举d
for (i=0;i<=n-4 && flag;i++) //注意因为有负数存在,需要扫完全部
{
int head=i+1,tail=n-1; //从首尾两个方向扫描
while (head<tail)
{
int tmp=data[i]+data[head]+data[tail];
if (tmp==data[d])
{
if (i==d || head==d || tail==d) break;
flag=false;
ans=tmp;
break;
}
else if (tmp>data[d]) tail--;
else head++;
}
}
if (flag)
printf("no solution\n");
else
printf("%d\n",ans);
}
return 0;
}
相关文章推荐
- 8.6 tempfile--临时文件和目录处理
- 【坑】使用PrintWrite返回JSON的时候中文乱码的解决办法
- 指定CRC32反构数据
- 8.6 tempfile--临时文件和目录处理
- day25
- HDU破百,ACM起航
- 集成友盟推送android
- 从第一个字符串中删除在第二个字符串中出现过的所有字符
- JavaScript异步编程的Promise模式
- 启发式算法-A*算法
- jquery部分笔记
- C - 跨平台宏定义
- (转)虚拟IP原理
- HDU 1061 (51Nod 1004 )n^n的末位数字
- Android 文本输入框EditText属性和方法说明(转载)
- Android 使用Toolbar+DrawerLayout快速实现仿“知乎APP”侧滑导航效果
- 锋利的jQuery第2版学习笔记1~3章
- Android 使用Toolbar+DrawerLayout快速实现仿“知乎APP”侧滑导航效果
- ROS turtlebot mapping
- (OK) 调试cBPM—CentOS7—gdb—gdbserver—问题的解决—2—段错误