折半枚举+3SUM--poj2549
来源:互联网 发布:万能视频修复软件 编辑:程序博客网 时间:2024/05/21 09:18
3SUM
Suppose the input array is . 3SUM can be solved in time by inserting each number into a hash table, and then for each index and, checking whether the hash table contains the integer.
Alternatively, the algorithm below first sorts the input array and then tests all possible pairs in a careful order that avoids the need tobinary search for the pairs in the sorted list, again achieving time, as follows.[1]
sort(S); for i=0 to n-3 do a = S[i]; k = i+1; l = n-1; while (k<l) do b = S[k]; c = S[l]; if (a+b+c == 0) then output a, b, c; exit; else if (a+b+c > 0) then l = l - 1; else k = k + 1; end end end
下面是代码:
#include<iostream>#include<cstdio>#include<algorithm>using namespace std;int N;int a[1010];void solve(){ bool flag=false; int tmp; for(int i=N; i>=1; i--) { tmp=a[i]; for(int j=1; j<=N-2; j++) { if(i==j) continue; int s=a[j]; int k=j+1; int l=N; while(k<l) { if(s+a[k]+a[l]==tmp&&k!=i&&l!=i) { flag=true; break; } else if(s+a[k]+a[l]>tmp) l-=1; else k+=1; } if(flag) break; } if(flag) break; } if(flag) cout<<tmp<<endl; else cout<<"no solution"<<endl;}int main(){ while(cin>>N,N) { for(int i=1; i<=N; i++) scanf("%d",&a[i]); if(N<=3) { cout<<"no solution"<<endl; continue; } sort(a+1,a+N+1); solve(); } return 0;}
- 折半枚举+3SUM--poj2549
- POJ2549 Sumsets 折半枚举
- poj2549 Sumsets(折半枚举)
- poj2549折半枚举
- poj2549(折半枚举)
- poj2549 3sum/二分查找
- poj 2785 折半枚举(与poj2549的区别)
- POJ2549-Sumsets【折半搜索】
- poj2549 枚举+二分
- poj2549
- poj2549
- poj2549
- poj 2785--4 Values whose Sum is 0(折半枚举)
- POJ_2785_4 values whose sum is 0_折半枚举
- POJ 2785 4 VALUES WHOSE SUM IS 0 【折半枚举】
- [折半枚举] poj 2785 4 Values whose Sum is 0
- POJ 2785 4 Values whose Sum is 0(折半枚举)
- POJ 2785 4 Values whose Sum is 0 折半枚举
- C++产生随机数
- 【线段树】单点更新2
- 详解惠普webOS官方教程 如何下载安装SDK和PDK(1)
- DM8168板第一次有串口输出数据
- 高斯滤波
- 折半枚举+3SUM--poj2549
- maven编译打包jar名称里含svn版本号
- 不要逼我结对编程
- OCP-1Z0-053-V13.02-325题
- Qt绘制简易时钟
- 做程序员要遵循的几个基本原则!
- Access 修改窗口大小(亲自实践)
- MongoDB的备份恢复和导入导出
- 软件的层次结构之Swing