Replacing Elements

You have an array a1,a2,…,an. All ai are positive integers.

In one step you can choose three distinct indices i, j, and k (i≠j; i≠k; j≠k) and assign the sum of aj and ak to ai, i. e. make ai=aj+ak.

Can you make all ai lower or equal to d using the operation above any number of times (possibly, zero)?

Input
The first line contains a single integer t (1≤t≤2000) — the number of test cases.

The first line of each test case contains two integers n and d (3≤n≤100; 1≤d≤100) — the number of elements in the array a and the value d.

The second line contains n integers a1,a2,…,an (1≤ai≤100) — the array a.

Output
For each test case, print YES, if it’s possible to make all elements ai less or equal than d using the operation above. Otherwise, print NO.

You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).

Example

input
3
5 3
2 3 2 5 4
3 4
2 4 4
5 4
2 1 5 3 6
output
NO
YES
YES

Note
In the first test case, we can prove that we can’t make all ai≤3.

In the second test case, all ai are already less or equal than d=4.

In the third test case, we can, for example, choose i=5, j=1, k=2 and make a5=a1+a2=2+1=3. Array a will become [2,1,5,3,3].

After that we can make a3=a5+a2=3+1=4. Array will become [2,1,4,3,3] and all elements are less or equal than d=4.

题目还是很好理解的,任选数组中的两个作和替换较大的数,可以先对一串数字进行排序,排序方式从小到大和从大到小都可,sort函数默认排序从小到大。
一旦数组中最大的数即a[n-1]是一个小于或等于d的数,直接输出YES即可,否则运用数组中最小的两个数加和替换最大的数。

(是很简单的题,谨以此篇文章纪念自己会使用sort函数。)

/*by BFU zq2021/1/16 14:13:51*/
#include<bits/stdc++.h>
using namespace std;int main()
{int t,n,d,a[103];cin>>t;while(t--){cin>>n>>d;for (int i=0;i<n;i++){cin>>a[i];}sort(a,a+n);if (a[n-1] > d){int m = a[0] + a[1];a[n-1] = m;if (a[n-1] > d){cout<<"No"<<endl;}else{cout<<"Yes"<<endl;}}else{cout<<"Yes"<<endl;}}return 0;
}

Replacing Elements (CodeForces - 1473A)相关推荐

  1. Replacing Elements

    Replacing Elements CodeForces - 1473A You have an array a1,a2,-,an. All ai are positive integers. In ...

  2. Swap Adjacent Elements CodeForces - 920C

    You have an array a consisting of n integers. Each integer from 1 to n appears exactly once in this ...

  3. 【CF比赛】Educational Codeforces Round 102 (Rated for Div. 2)

    题目来源 Educational Codeforces Round 102 (Rated for Div. 2) A. Replacing Elements 只要判断最大值是否小于等于d,或者第一个值 ...

  4. Codeforces Round #640 (Div. 4)(ABCDE)

    Sum of Round Numbers CodeForces - 1352A 思路:按照题意模拟即可. 代码如下: #include<bits/stdc++.h> #define ll ...

  5. Angular component的一个例子

    官网:https://angular.io/guide/architecture-components Before a view is displayed, Angular evaluates th ...

  6. python设置堆大小_Python中的堆问题

    Heap in python 堆(英语:Heap)是计算机科学中一类特殊的数据结构的统称.堆通常是一个可以被看做一棵树的数组对象.在队列中,调度程序反复提取队列中第一个作业并运行,因为实际情况中某些时 ...

  7. 《泛型编程与stl》

    以下是STL六大组件(componments): adapters  配接器 用来修饰其他组件.包括iterator adapters.function  adapters.container ada ...

  8. Codeforces Gym 100203G G - Good elements 暴力

    G - Good elements Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest ...

  9. Codeforces 926E - Merge Equal Elements(栈 + 模拟)

    You are given a sequence of positive integers a 1, a 2, -, a n. While possible, you perform the foll ...

最新文章

  1. Python:爬虫框架Scrapy的安装与基本使用
  2. 非常美妙的图片,呵呵
  3. Ajax_Apache访问资源文件的权限配置、资源存放路径配置、配置虚拟主机、动态网站静态网站区别...
  4. K8S集群安装KubeSphere失败记录
  5. asp与php对比,个人看法 zblogasp和zblogphp的对比
  6. java开源springboot项目_使用Spring Boot的10多个免费开源项目
  7. 详解浏览器 428 状态码 428 Precondition Required
  8. 中国水痘带状疱疹感染治疗药物市场趋势报告、技术动态创新及市场预测
  9. 有一些无声话语,只有寻梦的人彼此听得见
  10. Django基础--4
  11. 虚拟光驱 安装深度linux,今天试装了深度精简系统Deepin-LiteXP-SP3 6.2 小盘
  12. 《算法笔记》胡凡 例题/练习 答案
  13. 开发APP软件需要哪些编程语言和开发环境
  14. Daemontools和Supervisor管理linux常驻进程
  15. python安装教程
  16. 使用python根据模板批量生成docx文档
  17. 铁威马教程之如何轻松同步TNAS和云盘数据
  18. Timer 控件中的Elapsed事件与tick事件的区别
  19. word文档中如何添加目录
  20. 海康威视多监控集成到同一页面

热门文章

  1. AtoZ CSS快速提示:对文本使用OpenType
  2. 网易游戏AI Lab 招聘CV日常实习生
  3. trainning 2017-11-21
  4. π型滤波频率计算_滑动平均滤波的截止频率与平均点数计算
  5. 8、C语言printf函数与scanf函数
  6. Go语言黑帽子学习1
  7. 墨者安全调查:2018年DDoS威胁与黑灰产业调查报告
  8. 基于深度学习的图像篡改识别
  9. 面向对象:继承(经典类新式类继承原理、属性查找)、派生
  10. ssl证书过期怎么解决?