UVA - 10125 Sumsets (预处理 + 二分)
来源:互联网 发布:python datatime 编辑:程序博客网 时间:2024/06/03 22:13
大体题意:
给你一个最大长度为1000的数组,让你从中找出四个不同的元素 a,b,c,d,满足 a + b + c == d
如果存在 就让d 尽可能的大,否则输出no solution
思路:
把等式变换一下 a + b == d - c;
那么我们可以先两层循环,求出所有的a + b 来,然后再两层循环枚举 d 和c,二分 a+b,看看满不满足!
这样复杂度n*n log n级别的!
注意:
因为题目要求是 四个不同元素,因此 不仅要存下 a+b 来,还要存下a和b 的位置,可以放到结构体里!
lower_bound 结构体即可!
详细见代码:
#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int inf = 0x3f3f3f3f;const int maxn = 1000 + 10;struct Node{int val;int a,b;bool operator < (const Node& rhs) const {return val < rhs.val;}bool operator < (int kk) const {return val < kk;}}p[maxn*maxn];int x[maxn];int main(){int n;while(scanf("%d",&n) == 1 && n){int cnt = 0;for (int i = 0 ; i < n; ++i){scanf("%d",&x[i]);}for (int i = 0; i < n; ++i){for (int j = 0; j < n; ++j){if (i == j)continue;p[cnt].a = i;p[cnt].b = j;p[cnt++].val = x[i] + x[j];}}sort(p,p+cnt);int ans = -inf;for (int s = 0; s < n; ++s){for (int c = 0; c < n; ++c){if (s == c)continue;int m = lower_bound(p,p+cnt,x[s] - x[c]) - p;if (m == cnt)continue;while(m < cnt && p[m].val == x[s]-x[c] &&(p[m].a == s || p[m].a == c || p[m].b == s || p[m].b == c))++m;if (m != cnt && p[m].val == x[s] - x[c])ans = max(ans,x[s]);}}if (ans == -inf)printf("no solution\n");else printf("%d\n",ans);}return 0;}
0 0
- UVA - 10125 Sumsets (预处理 + 二分)
- UVA - 10125 Sumsets(二分查找)
- UVa 10125 Sumsets (折半枚举&二分查找)
- UVA 10125 Sumsets 折半枚举 + 二分
- uva 10125 Sumsets(搜索)
- 10125 - Sumsets(折半枚举+二分)
- uva 10125 - Sumsets(技巧性枚举)
- UVA 10125 - Sumsets(POJ 2549) hash
- UVA - 10125 - Sumsets (暴力枚举 + STL)
- UVa 10125 - Sumsets
- uva 10125 - Sumsets
- uva 10125 Sumsets
- UVa 10125 - Sumsets
- uva 10125 - Sumsets
- UVA 10125 Sumsets
- UVA 10125 - Sumsets
- UVa 10125-Sumsets
- UVa:10125 Sumsets
- 11 ContextMenu 上下文菜单按钮
- 【LightOJ】1331 - Agent J(数论)
- 如何查看SQL2000数据库版本
- 最新的mysql5.7.14下载及安装指南
- jquery心形点赞关注效果
- UVA - 10125 Sumsets (预处理 + 二分)
- 工厂模式学习笔记
- TEA和XxTEA跨平台加密算法
- CSDN如何转载别人的博客
- halcon 特征参数学习
- Windows系统启动流程
- iOS 代码搜身的技巧
- iOS开发系列--音频播放、录音、视频播放、拍照、视频录制
- tmux快捷键