您的位置:首页 > 其它

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

2014-11-24 09:26 555 查看
Candy Boxes

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 ≤ x4) arithmetic
mean is

, median is

and range 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 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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: