问题 B: Little Sub and Triples

题目链接:http://icpc.upc.edu.cn/problem.php?cid=1748&pid=1 (后有原题)

【分析】拿到这道题目很容易推导出 a+b>c 这个式子来,只是推导到这里,就去做题是万万不可行的,为什么思考不在深入一步呢

 看这一段:

                 1.x, y, z are unique integers.
                 2.l≤x, y, z≤r.
                 3.Ax≤Ay≤Az .
                 4.F(Ax, Ay, Az) > 1.

  意思也就是说使 c>=b>=a 且 a+b>c  , 其实这个条件可以进行聚像成 a  b  c 能够组成三角型。题目给定 l r 区间,从中判断是否存    在 a b c 能够构成三角形。

  想到这里可能还是没有思路。这时或许有种灵感,每三个三个的考虑可以吗,然后依次往后尺取。然后这时候大脑中可能会想倘      若只考虑连续的了,那不连续的呢?不连续的也可能会构成三角形啊。我们就有可能放弃之前的那种考虑连续三个点的思路,自      己把自己给否定了。

  其实我们可以这样想,上面我们质疑的问题是不连续怎么办,其核心是我们心里知道题目中给的序列可能不是单调的。所以我们      为何不直接把 l   r  区间排一遍序呢,然后在对每一组连续的三个点进行判断不就好了。

  可是题目中给的数据区间太大,每一次询问都要进行排序的话即使是递归排序、快速排序也会超时。所以我们继续深入考虑。

   对于一个小区间来说,最坏的情况莫过于 num[i]=num[i-1]+nun[i-2] , 这其实是斐波那契数列~ 很神奇。我们把斐波那契数列打表       发现当打到第47个的时候就已经超过int类型能够承受的最大数值,而题目中说  All given integers will not exceed 231-1. 所以我       们可以得出一个结论,当区间长度超过47的时候我们可以直接输出 YES ,剩下的不超过47的我们就需要进行暴力了。

   

#include<cstdio>
#include <iostream>
#include<vector>
#include<algorithm>
using namespace std;
typedef long long ll;
int n,q;
int num[200005];
vector<int>v;
int main()
{
    
    scanf("%d%d",&n,&q);
    for(int i=0;i<n;i++) scanf("%d",&num[i]);
    while(q--)
    {
        int f=0;
        int l,r;
        scanf("%d%d",&l,&r);
        if((r-l+1)>47) f=1;
        else
        {
            v.clear();
            for(int j=l-1;j<=r-1;j++) v.push_back(num[j]);
            sort(v.begin(),v.end());
            for(int j=2;j<v.size();j++)
            {
                if(v[j]<v[j-1]+v[j-2]) {f=1;break;}
            }
        }
        if(f) printf( "YES\n" );
        else printf("NO\n");
    }
    return 0;
}

时间限制: 1 Sec  内存限制: 256 MB
提交: 785  解决: 138
[提交] [状态] [命题人:admin]

题目描述

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

[提交][状态]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值