UPC Contest 1748 Little Sub and Triples

题目描述

Little Sub has learned a new word ’Triple’, which usually means a group with three elements in it.
Now he comes up with an interesting problem for you.
Define
Given an positive integer sequence A and some queries. In each query, you have to tell if there exists
a triple T = (x, y, z) such that:
1.x, y, z are unique integers.
2.l≤x, y, z≤r.
3.Ax≤Ay≤Az .
4.F(Ax, Ay, Az) > 1.

输入

The first line contains two positive integers n, q(1 ≤ n, q ≤ 200000).
The second line contains n positive integers, indicating the integer sequence.
The next q lines describe each query by giving two integer l, r(1≤l≤r≤n).
All given integers will not exceed 231-1.

输出

For each query, print YES if it exists any legal triple or NO otherwise.

样例输入

复制样例数据

4 2
1 2 3 4
1 3
2 4

样例输出

NO
YES

思路:

           因为斐波那契数在达到第47个时就超出了1e9了。斐波那契数(1,2,3,5,8,13....)实际上就是满足不能构成三角形的一个连续边序列。所以在判断[l,r]的长度大于47就保证可以构成三角形。如果小于47的话,排序遍历一遍即可。

AC代码

#include<iostream>
#include<algorithm>
#include<cstdio>
using namespace std;
long long a[240000],b[240000];
int main()
{
    int n,q,r,l;
    scanf("%d%d",&n,&q);
    for(int i=1;i<=n;i++)
        scanf("%d",&a[i]);
    for(int i=1;i<=q;i++)
    {
        int j,flag=0,k=0;
        scanf("%d%d",&l,&r);
        if(r-l+1>47)
            printf("YES\n");
        else
        {
            for(j=l;j<=r;j++)
                b[k++]=a[j];
            sort(b,b+k);
            for(j=2;j<k;j++)
            {
                if(b[j]<b[j-1]+b[j-2])
                {
                    printf("YES\n");
                    break;
                }
            }
            if(j==k)
                printf("NO\n");
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/ytuyzh/article/details/89817778
今日推荐