UVA10125 POJ2549 Sumsets【暴力+二分】
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
问题链接:UVA10125 POJ2549 Sumsets
问题简述:(略)
问题分析:用暴力枚举来解决这个问题。如果同时枚举a、b、c和d,则算法复杂度为O(n4),有点可怕。所以,需要进行必要的降维。先把所有数据排序,然后从大到小枚举d,再枚举c,这时可以算出d-c,再枚举a+b跟d-c进行匹配。a+b=d-c等价于a+b+c=d。
需要注意,那些数有可能是负数。
这个题也可以采用set数据结构来表示,再进行枚举处理。但是,用数组会比较快。
程序说明:枚举过程中,尽量用循环变量缩小枚举数量。
参考链接:(略)
题记:(略)
AC的C++语言程序如下:
/* UVA10125 POJ2549 Sumsets */#include <iostream>
#include <algorithm>
#include <cstdio>using namespace std;const int N = 1000;
int s[N];int main()
{int n, a, b, c, d;while(~scanf("%d", &n) && n) {for(int i = 0; i < n; i++) scanf("%d", &s[i]);sort(s, s + n);int flag = 0, t1, t2;for(d = n - 1; d > 0; d--) {for(c = n - 1; c > 0; c--) {if(s[c] != s[d]) t1 = s[d] - s[c];for(a = 0, b = c - 1; a < b;) {t2 = s[a] + s[b];if(t2 == t1) {flag = 1;break;}else if(t2 < t1)a++;elseb--;}if(flag) break;}if(flag) break;}if(flag)printf("%d\n", s[d]);elseprintf("no solution\n");}return 0;
}
UVA10125 POJ2549 Sumsets【暴力+二分】相关推荐
- Codeforces Round #593 (Div. 2) D. Alice and the Doll 暴力 + 二分
传送门 文章目录 题意: 思路: 题意: 思路: 还以为这个题有什么高深的算法,结果就是个暴力. 由于n∗mn*mn∗m达到了1e101e101e10的级别,所以直接暴力肯定是不行的,考虑有很多空格, ...
- Codeforces Round #635 (Div. 2) D. Xenia and Colorful Gems 暴力 + 二分
传送门 文章目录 题意: 思路: 题意: 给你三个数组a,b,ca,b,ca,b,c,让你从每个数组中选择一个数x,y,zx,y,zx,y,z,使得(x−y)2+(x−z)2+(y−z)2(x-y)^ ...
- [Letcode]第[34]题[JAVA][在排序数组中查找元素的第一个和最后一个位置][暴力][二分]
[问题描述][中等] [解答思路] 1. 线性扫描(不符合题意) 时间复杂度:O(N) 空间复杂度:O(1) class Solution {public int[] searchRange(int[ ...
- Competitive Programming 3题解
题目一览: Competitive Programming 3: The New Lower Bound of Programming Contests(1) Competitive Programm ...
- AOAPC I: Beginning Algorithm Contests 题解
AOAPC I: Beginning Algorithm Contests 题解 AOAPC I: Beginning Algorithm Contests (Rujia Liu) - Virtual ...
- ICPC程序设计题解书籍系列之三:秋田拓哉:《挑战程序设计竞赛》(第2版)
白书<挑战程序设计竞赛>(第2版)题目一览 白书:秋田拓哉:<挑战程序设计竞赛>(第2版) 第1章 蓄势待发--准备篇(例题) POJ1852 UVa10714 ZOJ2376 ...
- 20181021模拟赛(暴力+暴力+优先队列二分)
NOIP2016 提高组模拟赛 IzumiKonata 题目名Tetrix Tree Copier 输入文件名tetrix.in tree.in copier.in 输出文件名tetrix.out t ...
- Gym - 101911B Glider(前缀和+二分)
传送门:点我 A plane is flying at a constant height of hh meters above the ground surface. Let's consider ...
- POJ - 1904 King's Quest 强连通tanjar思想
传送门 题意:每个儿子都喜欢许多女孩,问每个渣男儿子最多有几个可以选择备胎.他爹也挺渣.. 猛地一看,哇二分匹配,但是想了想输入输出都能卡时间的题目,循环暴力二分匹配必T无疑,在做强连通模块的题所以想 ...
最新文章
- 串的顺序存储结构c语言版,数据结构C语言版串的定长顺序存储表示和实现.doc
- 企业数字化转型本质上是“人”的转型和“组织”的转型
- visual c 语言参考手册pdf,《Microsoft-Visual-C-6-0语言参考手册(三)》.pdf
- php如何使用代码清除bom,使用php清除bom示例
- JVM调优:常见垃圾回收器组合
- BZOJ1095: [ZJOI2007]Hide 捉迷藏(动态点分治)
- 文本处理3剑客 sed命令
- solaris系统中的’format’程序
- C++学有余力的大一同学的学习拓展
- STM32/TMS320F2812+W5500硬软件调试总结
- Flutter之Visibility和Opacity使用说明
- HSQLDB:一款基于 Java 的嵌入式关系型数据库
- MySQL操作语句(快速办公)
- “程序员猝死”引发的思考
- 【C++】C++PrimerPlus(第6版)中文版 第9章 内存模型和名称空间 编程练习 参考答案
- 电脑死机什么导致,带你一次性了解所有导致电脑死机蓝屏原因!
- 小白如何进入IT行业及如何选择培训机构
- 什么是Android逆向?如何学习安卓逆向?Android逆向自学笔记入门到实战
- 前端需要了解的5G网络知识
- 嵌入式软硬件开发区别