Codeforces Round #278 (Div. 2)---B. Candy Boxes (暴力+构造)

本文探讨了在Cyberland中保留4个特殊糖果盒子的传统,并提出了当某些盒子丢失后,如何通过数学方法找到缺失盒子内糖果数量的问题。文章详细介绍了输入输出格式、数据限制以及解题思路,并提供了AC代码示例。

time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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, their median and their range are all equal. By definition, for a set {x1, x2, x3, x4} (x1 ≤ x2 ≤ x3 ≤ x4arithmetic mean is median is  and range is x4 - x1The 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 of 4 numbers meeting the condition because their mean, median and range are all equal to 2.

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

Now Jeff wants to know: is there a possible way to find the number of candies of the 4 - 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 integers ai, denoting the number of candies in the i-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 inequality 1 ≤ b ≤ 106. It is guaranteed that if there exists a positive integer solution, you can always find such b'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 lowest n first positions.

Sample test(s)
input
2
1
1
output
YES
3
3
input
3
1
1
1
output
NO
input
4
1
2
2
3
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 output b in any order.





题目大意:给你一组数x1, x2 ,x3, x4,问是否满足(x1 + x2 + x3 + x4)/ 4.0  ==  (x2 + x3)/2.0  == x4 - x1.若给出的数少于4,则问你是否可以构造出满足条件的数组,四个数顺序任意。 


解题思路:分别考虑n的取值,特殊判定n == 0 和n == 1时候的情况,其他两种情况暴力枚举剩下的数,在判断是否存在即可。





AC代码:

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
#define INF 0x7fffffff

int a[5], b[5];

int main()
{
    #ifdef sxk
        freopen("in.txt","r",stdin);
    #endif
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        for(int i=0; i<n; i++)
            scanf("%d", &a[i]);
        sort(a, a+n);
        if(n == 0){                  //特判
            puts("YES");
            printf("%d\n", 1);
            printf("%d\n", 1);
            printf("%d\n", 3);
            printf("%d\n", 3);
        }
        else if(n == 1){              //特判
            puts("YES");
            printf("%d\n", a[0]);
            printf("%d\n", 3*a[0]);
            printf("%d\n", 3*a[0]);
        }
        else if(n == 2){                   
            for(int i=1; i<3000; i++){         //暴力枚举剩下的两个数
                for(int j=i; j<3000; j++){
                    b[0] = a[0]; b[1] = a[1];
                    b[2] = i;
                    b[3] = j;
                    sort(b, b+4);
                    int x = (b[0]+b[1]+b[2]+b[3]);
                    int xx = (b[1]+b[2]);
                    int xxx = b[3] - b[0];
                    if(x/4.0 == xx/2.0 && x/4.0 == xxx){
                        puts("YES");
                        printf("%d\n", i);
                        printf("%d\n", j);
                        return 0;
                    }
                }
            }
            puts("NO");
        }
        else if(n == 3){
            for(int i=1; i<10000; i++){                //暴力枚举剩下的一个数
                b[0] = a[0]; b[1] = a[1]; b[2] = a[2];
                b[3] = i;
                sort(b, b+4);
                int x = (b[0]+b[1]+b[2]+b[3]);
                int xx = (b[1]+b[2]);
                int xxx = b[3] - b[0];
                if(x/4.0 == xx/2.0 && x/4.0 == xxx){
                    puts("YES");
                    printf("%d\n", i);
                    return 0;
                }
            }
            puts("NO");
        }
        else{                                      //n == 4时,直接判断是否满足情况即可
            sort(a, a+4);
            int x = (a[0]+a[1]+a[2]+a[3]);
                int xx = (a[1]+a[2]);
                int xxx = a[3] - a[0];
                if(x/4.0 == xx/2.0 && x/4.0 == xxx){
                    puts("YES");
                    return 0;
                }
                else puts("NO");
        }
    }
    return 0;
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值