Codeforces Round #278 (Div. 2) B. Candy Boxes

来源:互联网 发布:扒皮吧软件 编辑:程序博客网 时间:2024/05/20 02:22

B. Candy Boxes


There is an old tradition of keeping 4 boxes of candies in the house in Cyberland. The numbers of candies are special if their arithmetic mean, theirmedian and their range are all equal. By definition, for a set {x1, x2, x3, x4} (x1 ≤ x2 ≤ x3 ≤ x4)arithmetic mean is ,median is andrange is x4 - x1.The arithmetic mean and median are not necessary integer. It is well-known that if those three numbers are same, boxes will create a "debugging field" and codes in the field will have no bugs.

For example, 1, 1, 3, 3 is the example of4 numbers meeting the condition because their mean, median and range are all equal to2.

Jeff has 4 special boxes of candies. However, something bad has happened! Some of the boxes could have been lost and now there are onlyn (0 ≤ n ≤ 4) boxes remaining. Thei-th remaining box contains ai candies.

Now Jeff wants to know: is there a possible way to find the number of candies of the4 - n missing boxes, meeting the condition above (the mean, median and range are equal)?

Input

The first line of input contains an only integer n (0 ≤ n ≤ 4).

The next n lines contain integersai, denoting the number of candies in thei-th box (1 ≤ ai ≤ 500).

Output

In the first output line, print "YES" if a solution exists, or print "NO" if there is no solution.

If a solution exists, you should output 4 - n more lines, each line containing an integer b, denoting the number of candies in a missing box.

All your numbers b must satisfy inequality1 ≤ b ≤ 106. It is guaranteed that if there exists a positive integer solution, you can always find suchb's meeting the condition. If there are multiple answers, you are allowed to print any of them.

Given numbers ai may follow in any order in the input, not necessary in non-decreasing.

ai may have stood at any positions in the original set, not necessary on lowestn first positions.

Sample test(s)
Input
211
Output
YES33
Input
3111
Output
NO
Input
41223
Output
YES
Note

For the first sample, the numbers of candies in 4 boxes can be 1, 1, 3, 3. The arithmetic mean, the median and the range of them are all2.

For the second sample, it's impossible to find the missing number of candies.

In the third example no box has been lost and numbers satisfy the condition.

You may outputb in any order.(可以以任意顺序输出b。)


题意:一个数列由4个数组成,从小到大分别为x1,x2,x3,x4,如果他们的平均数,中位数,范围(即最大数减最小数)这三个数都相等,则称这个数列是一个“特殊”的数列。他会给你其中n个数(a1,……an,n<=4),让你任意补全这个数列,问是否可以构成“特殊”数列。可以输出“YES”并且输出你添加的数,不可以输出“NO”。


思路:当n=0,1,时,一定能构成“特殊”数列。因为(sum)/4=(x2+x3)/2=x4-x1(sum代表x1+x2+x3+x4),整理出:

一号式子:x4=3*x1;(由此可知最大数一定不会超过三倍的最小数)

二号式子:x1+x4=x2+x3=sum/2;

分类讨论:

当n=0时,输出任意一个正确答案,如:1 1 3 3.

当n=1时,得到一个数a1,令x1=a1,所以有一号式子得x4=3*a1,二号式子可得(x1<= x2,x3 <=x4且x2+x3=x1+x4)x2=x1,x3=x4;即输出a1,a1,3*a1,3*a1;

当n=2时,得到两个数a1,a2,用a1表示最小值,a2表示最大值,如果a2>3*a1则“NO”(由一号式子知)。如果a2<=3*a1,则一定可以构成a1(最小值),3*a1(最大值),4*a1-a2(中间值),a2(中间值),构成的“特殊”数列。

当n=3时,将a1,a2,a3排序后得(a1<=a2<=a3),依旧判断最大值是否大于最小值的三倍,如果a3>3*a1,则“NO”。

         如果a3=3*a1,则直接得到中间值4*a1-a2。

         如果a3<3*a1,则选择构造最大值或者构造最小值,判断是否成立,若都不成立则输出“NO”。(最大值是三倍的最小值,最小值是最大值的三分之一。)

当n=4时,直接判断是否成立:if(sum==2*(x2+x3)&&sum==4*(x4-x1)),(这里转化为乘法判断可以避免出现小数问题)。


代码:

#include<iostream>#include<cstdio>#include<algorithm>#include<cstring>#include<stdio.h>using namespace std;int a[10];int main(){    int  n;    cin>>n;    int sum=0,ma,mi;//用ma代表已知数列的最大值,mi代表已知数列的最小值。    for(int i=1;i<=n;i++){        cin>>a[i];        if(i==1){            ma=mi=a[1];        }        else{            if(a[i]>ma)                ma=a[i];            if(a[i]<mi)                mi=a[i];        }        sum+=a[i];    }    sort(a+1,a+1+n);    if(ma>mi*3&&n!=0){//如果最大值大于最小值的3倍,一定没有答案;        printf("NO\n");    }    else if(n==4){        if(4*a[4]-4*a[1]==sum&&sum==2*a[3]+2*a[2])            printf("YES\n");        else            printf("NO\n");    }    else if(n==0){        printf("YES\n1\n1\n3\n3\n");    }    else if(n==1){        printf("YES\n");        printf("%d\n%d\n%d\n",a[1],3*a[1],3*a[1]);    }    else if(ma==3*mi){//因为2,3都要判断最大值是否为最小值的三倍,所以直接提前一次判断了。        printf("YES\n");        if(n==2){            printf("%d\n%d\n",(mi+ma)/2,(mi+ma)/2);        }        else if(n==3){            a[4]=ma+mi-a[2];                printf("%d\n",a[4]);        }    }    else if(n==2){        printf("YES\n");        a[4]=3*a[1];        a[3]=4*a[1]-a[2];        cout<<a[3]<<endl<<a[4]<<endl;    }    else if(n==3){        int pd=1;        a[4]=3*a[1];        sum+=a[4];        if(4*a[4]-4*a[1]==sum&&sum==2*a[3]+2*a[2])            {printf("YES\n%d\n",a[4]);pd=0;}        if(ma%3==0){            sum-=a[4];            a[4]=ma/3;            sum+=a[4];            sort(a+1,a+5);            if(4*a[4]-4*a[1]==sum&&sum==2*a[3]+2*a[2])                {printf("YES\n%d\n",a[1]);pd=0;}        }        if(pd)            printf("NO\n");    }    return 0;}


0 0