uva 10730 - Antiarithmetic?
来源:互联网 发布:名侦探柯南主线知乎 编辑:程序博客网 时间:2024/06/11 18:43
点击打开链接uva 10730
思路:枚举等差中项
分析:
1 给定一个n个数的序列判断是否有等差子序列
2 很明显我们如果要判断是否有等差子序列的话,只要去判断是否有长度为3的等差子序列
3 对于n<=10000,那么我们怎么去判断呢,由于我们要找的是是否有三个的等差序列,那么我们可以枚举每一个数作为等差中项,然后去判断
4 假设现在我们枚举num[i]作为等差中项,那么第一个数在0~i-1之间,第二个数是在i+1~n-1之间,这个时候如果单存利用for循环去找时间复杂度不能接受,那么我们想到能减少多少复杂度呢?
5 我们知道set内部利用的是红黑数,所以说set的查找是logN的比O(n)快,所以呢我们利用set来找第二个数那么这样降低了时间复杂度。
代码:
#include<set>#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespace std;int const MAXN = 10010;int n , num[MAXN];set<int>s;bool solve(){ //枚举每一个数作为等差中项 for(int i = 0 ; i < n ; i++){ //左边 s.erase(num[i]); for(int j = 0 ; j < i ; j++){ //右边 int value = 2*num[i]-num[j]; if(s.find(value) != s.end()) return true; } } return false;}int main(){ char str[10]; while(scanf("%s" , str)){ if(!strcmp(str , "0")) break; sscanf(str , "%d:" , &n); s.clear(); for(int i = 0 ; i < n ; i++){ scanf("%d" , &num[i]); s.insert(num[i]); } printf("%s\n" , solve() ? "no" : "yes"); } return 0;}
- uva 10730 - Antiarithmetic?
- UVA 10730 - Antiarithmetic?
- uva 10730 - Antiarithmetic?
- UVA 10730 Antiarithmetic?
- uva 10730 - Antiarithmetic?(枚举)
- UVa 10730 Antiarithmetic? (想法题)
- uva - 10730 - Antiarithmetic?(暴力枚举)
- UVA 10730 - Antiarithmetic?(技巧枚举)
- UVa 11129 - An antiarithmetic permutation
- UVa 11129 - An antiarithmetic permutation
- uva 11129 - An antiarithmetic permutation
- UVA 11129 - An antiarithmetic permutation
- UVA 11129 An antiarithmetic permutation
- uva 11129 - An antiarithmetic permutation
- uva 11129 An antiarithmetic permutation
- uva 11129 - An antiarithmetic permutation
- UVa 11129 - An antiarithmetic permutation
- An antiarithmetic permutation +uva+分治
- Jsp : tag 文件操作数据库 (用户注册程序)
- CodeForces Round #179 (296B) - Yaroslav and Two Strings
- 用U盘装win 7操作系统 [李园7舍_404]
- 1.4.3---Arithmetic Progressions
- NeHe OpenGL教程(中英文版附带VC++源码)中英文系列
- uva 10730 - Antiarithmetic?
- 你不懂我,我不怪你 余秋雨
- 视频通话最新谍报
- Arctic Network
- 移植mjpg
- Qt中使用OpenCV显示图片时,Mat结构转换为QImage结构的问题
- V4L2驱动编写篇——介绍
- Arctic Network
- 一个字符串查找的函数