Triangle & Little Sub and Triples 三角形与思维

两个类似的题目放到一起。

2019中山大学程序设计竞赛(重现赛)-1002题

Triangle
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 7179 Accepted Submission(s): 856

Problem Description
After Xiaoteng took a math class, he learned a lot of different shapes, but Xiaoteng’s favorite triangle is because he likes stable shapes, just like his style.

After the Xiaoxun knew it, he wanted to give a triangle as a gift to Xiaoteng. He originally planned to do one, but he was too tired. So he decided to bring some wooden sticks to Xiaoteng and let Xiaoteng make a triangle himself.

One day, Xiaoxun brought n sticks to Xiaoteng. Xiaoteng wanted to try to use three of them to form a triangle, but the number is too much, Xiaoteng stunned, so he can only ask for your help.

Input
There are mutiple test cases.

Each case starts with a line containing a integer n(1≤n≤5×106) which represent the number of sticks and this is followed by n positive intergers(smaller than 231−1) separated by spaces.

Output
YES or NO for each case mean Xiaoteng can or can’t use three of sticks to form a triangle.

Sample Input
4
1 2 3 4

Sample Output
YES

给出n个长度,问这些长度是否能构成三角形。

2019年杭州师范大学第十二届程序设计竞赛

问题 B: Little Sub and Triples
时间限制: 1 Sec 内存限制: 256 MB
提交: 842 解决: 155
[提交] [状态] [命题人: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

给出n个长度,进行q次询问,对每次询问求区间内是否可构成三角形。

思路与方法

这两个题目都是只询问是否可以构成三角形,n个边长无法构成三角形的最坏情况为斐波那契数列,该数列满足对任意l,r,x(l<r<x): a [ l ] + a [ r ] &lt; = a [ r − 1 ] + a [ r ] &lt; a [ r + 1 ] &lt; = a [ x ] a[l]+a[r]&lt;=a[r-1]+a[r]&lt;a[r+1]&lt;=a[x] a[l]+a[r]<=a[r1]+a[r]<a[r+1]<=a[x],故任意三条边无法构成三角形。在斐波那契数列的基础上,任意插入一条边,必然能与斐波那契数列中与之相邻的两条边构成三角形。
所以只要输入的边的个数多于int范围内的斐波那契数列的项数,必然存在三边构成三角形,而斐波那契数列的项数在int范围内有46项,只要边的个数超过50,直接输出yes,对于小于50的情况可以先排序再遍历一次进行判断,此时复杂度已经大大降低。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值