uva 10730 - Antiarithmetic?

点击打开链接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;
}
时间: 2024-07-28 13:11:07

uva 10730 - Antiarithmetic?的相关文章

算法:uva 10730

题目大意: 给n个数组成的序列,他们是0~n-1, 问序列中是否有按顺序的3个数,是等差数 列. 思路: 用一个数组记录每个数在序列中的位置,然后枚举3个数中最小的一个,再枚 举等差数列的增量,最后看着三个数的位置是不是连续的即可. n最大为1W,但这个算法的复杂 度看起来好像是O(n^2),怎么速度还挺快的呢?粗略的分析一下复杂度吧. 第一层for循环是 0...n,重点是看第二层for循环 :  for(int j=1;  i+2*j < n; ++j) 把 i+2*j < n 移项改变一

UVa 11129:An antiarithmetic permutation

[链接] http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=2070 [原题] A permutation of n+1 is a bijective function of the initial n+1 natural numbers: 0, 1, ... n. A permutation p is cal

uva 11129 - An antiarithmetic permutation

点击打开链接uva 11129 题目意思: 给定一个初始序列为0 1 2 3 .....n-1;要求找到一个序列能够满足所有的子序列都不能够形成等差序列 解题思路: 1思路:递归+分治 2分析:网上那些神牛是这么说的:把这个序列按照奇偶位置分开成两个序列,然后再对两个序列进行奇偶位置分开......最后得到的数组就是所要的ans,为什么这样可以呢,因为在奇数位置内和偶数位置内等差为2,而两个序列之间为1,所以这样肯定不会构成等差序列. 代码: #include <algorithm> #inc

UVa 10602

链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=1543 类型:贪心 原题: Company Macrohard has released it's new version of editor Nottoobad, which can understand a few voice commands.

UVa 10392 Factoring Large Numbers:素因子分解

10392 - Factoring Large Numbers Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=100&page=show_problem&problem=1333 One of the central ideas behind much cryptography is that factoring

UVa 10182 Bee Maja:规律&amp;amp;O(1)算法

10182 - Bee Maja Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1123 Maja is a bee. She lives in a bee hive with thousands of other bees. This bee hive c

算法题之UVA 763

Fibinary Numbers The standard interpretation of the binary number 1010 is 8 + 2 = 10. An alternate way to view the sequence ``1010'' is to use Fibonacci numbers as bases instead of powers of two. For this problem, the terms of the Fibonacci sequence

算法题:UVa 11461 Square Numbers (简单数学)

11461 - Square Numbers Time limit: 1.000 seconds http://uva.onlinejudge.org/index.php? option=com_onlinejudge&Itemid=8&category=467&page=show_problem&problem=24 56 A square number is an integer number whose square root is also an integer.

UVa 10183 How Many Fibs? (统计斐波那契数个数&amp;amp;高精度)

10183 - How Many Fibs? Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=115&page=show_problem&problem=1124 Recall the definition of the Fibonacci numbers:    f1 := 1    f2 := 2    fn :