A. Omkar and Bad Story-cf#724

A. Omkar and Bad Story

题目来源:Codeforces Round #724(Div.2) https://codeforces.com/contest/1536/problem/A

 

time limit per test:2 seconds

memory limit per test:256 megabytes

 

Omkar has received a message from Anton saying "Your story for problem A is confusing. Just make a formal statement." Because of this, Omkar gives you an array a=[a1,a2,…,an] of n distinct integers. An array b=[b1,b2,…,bk] is called nice if for any two distinct elements bi,bj of b, |bi−bj| appears in b at least once. In addition, all elements in bb must be distinct. Can you add several (maybe, 0) integers to a to create a nice array b of size at most 300? If a is already nice, you don't have to add any elements.

For example, array [3,6,9] is nice, as |6−3|=|9−6|=3, which appears in the array, and |9−3|=6, which appears in the array, while array [4,2,0,6,9] is not nice, as |9−4|=5 is not present in the array.

For integers xx and yy, |x−y|=x−y if x>y and |x−y|=y−x otherwise.

Input

Each test contains multiple test cases. The first line contains tt (1≤t≤50), the number of test cases. Description of the test cases follows.

The first line of each test case contains a single integer nn (2≤n≤100) — the length of the array a.

The second line of each test case contains n distinct integers a1,a2,⋯,an (−100≤ai≤100) — the elements of the array a.

Output

For each test case, output one line containing YES if Omkar can create a nice array bb by adding elements to aa and NO otherwise. The case of each letter does not matter, so yEs and nO will also be accepted.

If the first line is YES, output a second line containing a single integer kk (n≤k≤300).

Then output one line containing k distinct integers b1,b2,⋯,bk (−109≤bi≤109), the elements of the nice array b. b1,b2,⋯,bk can be in any order. For each aiai in a, aiai must appear at least once in b.

It can be proved that if Omkar can create such an array b, then he can also do so in a way that satisfies the above constraints.

If multiple solutions exist, you can print any.

Example

input

4
3
3 0 9
2
3 4
5
-7 3 13 -2 8
4
4 8 12 6

output

yes
4
6 0 3 9
yEs
5
5 3 1 2 4
NO
Yes
6
8 12 6 2 4 10

Note

For the first case, you can add integers to a to receive the array b=[6,0,3,9]. Note that |6−3|=|9−6|=|3−0|=3 and 3 is in b, |6−0|=|9−3|=6 and 6 is in bb, and |9−0|=9 is in b, so b is nice.

For the second case, you can add integers to a to receive the array b=[5,3,1,2,4]. We have that |2−1|=|3−2|=|4−3|=|5−4|=1 is in b, |3−1|=|4−2|=|5−3|=2 is in b, |4−1|=|5−2|=3 is in b, and |5−1|=4 is in b, so b is nice.

For the fourth case, you can add integers to a to receive the array b=[8,12,6,2,4,10]. We have that |4−2|=|6−4|=|8−6|=|10−8|=|12−10|=2 is in b, |6−2|=|8−4|=|10−6|=|12−8|=4 is in b, |8−2|=|10−4|=|12−6|=6 is in bb, |10−2|=|12−4|=8 is in b, and |12−2|=10 is in b, so b is nice.

It can be proven that for all other test cases it is impossible to create a nice array b.

题意

给定一个数组(数组大小不超过100个),对数组内的元素(-100<=元素值<=100)进行作差,若任意两个元素的差的绝对值在此数组内,则此数组为nice数组

题目要求在规定范围内对数组进行修改,当两个元素的差的绝对值不存在于数组内时,将其添加到数组内,结果要求输出修改后能完全表达的nice数组大小,并输出数组元素(可以不排序)。

当存在能完全表达的nice数组时,输出YES,并输出修改后的数组大小及数组元素;当数组内元素个数超过300个,但仍然不是nice数组,则输出NO。

思路

题目给的数据量不大,可以直接暴力写。

思路很简单。遍历数组,两两元素相减,若差值的绝对值不在数组内,就加进去,继续遍历。

因为题目不考虑数组内元素的大小顺序,加进去的数不用过多考虑。

利用check函数判断这个数是不是在数组内。

按照题目意思,只要存在nice数组就可以输出结果了。

输出的"YES""NO"没有固定大小写。

AC代码

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
const int N = 300+5;
int a[N];
//a数组大小为n,判断x是否存在于a数组内
int check(int a[],int n,int x){
    for(int i=0;i<n;i++){
        if(a[i] == x) return 1;
    }
    return 0;
}
int main()
{
    ios::sync_with_stdio(false);
    int t,n;
    cin>>t;
    while(t--){
        cin>>n;
        int cnt = 0;
        for(int i=0;i<n;i++) {
            cin>>a[i];
            cnt++;  //记录数组大小
        }
        int f = 1;  //标记
        for(int i=0;i<cnt;i++){
            for(int j=0;j<cnt;j++){
                if(i != j && a[i] != a[j]){
                    if( !check(a,cnt,abs(a[i]-a[j])) ){
                        a[cnt++] = abs(a[i]-a[j]); 
                        if(cnt > 300){  //数组大小超过300,修改标记
                            f=0;
                            break;
                        }
                    }
                    if(f == 0) break;
                }
                if(f == 0) break;
            }
            if(f == 0) break;
        }
        if(f == 0) cout<<"NO"<<'\n';    //标记数组大小超过300,输出NO
        else {
            cout<<"YES"<<'\n';
            cout<<cnt<<'\n';
            for(int i=0;i<cnt;i++) cout<<a[i]<<" ";
            cout<<'\n'; 
        }       
    }
    return 0;
}

可以尝试用动态数组vector写。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值