Codeforces Round #278 (Div. 2) B. Candy Boxes [brute force+constructive algorithms]

哎,最近弱爆了,,,不过这题还是不错滴~~ 要考虑完整各种情况

8795058                 2014-11-22 06:52:58     njczy2010                     B - Candy Boxes                          GNU C++     Accepted 31 ms 4 KB
8795016                 2014-11-22 06:48:15     njczy2010                     B - Candy Boxes                          GNU C++     Wrong answer on test 13                 30 ms                 0 KB    
8795000                 2014-11-22 06:44:39     njczy2010                     B - Candy Boxes                          GNU C++     Wrong answer on test 5                 15 ms                 0 KB

 

 

B. 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 all 2.

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.


主要就是根据那三个等式,化简,得两个方程:

x4=3*x1;

x2+x3=4*x1;

 

  1 #include<iostream>
  2 #include<cstring>
  3 #include<cstdlib>
  4 #include<cstdio>
  5 #include<algorithm>
  6 #include<cmath>
  7 #include<queue>
  8 #include<map>
  9 #include<set>
 10 #include<string>
 11 //#include<pair>
 12 
 13 #define N 10005
 14 #define M 1005
 15 #define mod 1000000007
 16 //#define p 10000007
 17 #define mod2 1000000000
 18 #define ll long long
 19 #define LL long long
 20 #define eps 1e-9
 21 #define maxi(a,b) (a)>(b)? (a) : (b)
 22 #define mini(a,b) (a)<(b)? (a) : (b)
 23 
 24 using namespace std;
 25 
 26 int n;
 27 int a[10];
 28 int b[10];
 29 int flag;
 30 
 31 void ini()
 32 {
 33     flag=0;
 34     for(int i=1;i<=n;i++){
 35         scanf("%d",&a[i]);
 36     }
 37     sort(a+1,a+1+n);
 38 }
 39 
 40 void solve0()
 41 {
 42     flag=1;
 43     b[1]=1;b[2]=1;b[3]=3;b[4]=3;
 44 }
 45 
 46 void solve1()
 47 {
 48     flag=1;
 49     b[1]=a[1];b[2]=3*a[1];b[3]=3*a[1];
 50 }
 51 
 52 void solve2()
 53 {
 54     if(a[2]%3==0){
 55         if(a[2]/3==a[1]){
 56             flag=1;
 57             b[1]=a[1];b[2]=a[2];
 58         }
 59         else if(a[2]/3<a[1]){
 60             flag=1;b[1]=a[2]/3;b[2]=a[2]+b[1]-a[1];
 61         }
 62         else{
 63             return;
 64         }
 65     }
 66     else{
 67         if(a[1]*3<=a[2]){
 68             return;
 69         }
 70         else{
 71             flag=1;
 72             b[2]=a[1]*3;
 73             b[1]=b[2]+a[1]-a[2];
 74         }
 75     }
 76 }
 77 
 78 void solve3()
 79 {
 80     if(a[3]%3!=0){
 81         if(a[1]*3<=a[3]){
 82             return;
 83         }
 84         else{
 85             if(a[1]*4==a[2]+a[3]){
 86                 flag=1;b[1]=a[1]*3;
 87             }
 88             else return;
 89         }
 90     }
 91     else{
 92         if(a[3]/3==a[1]){
 93             flag=1;
 94             b[1]=a[1]+a[3]-a[2];
 95         }
 96         else if(a[3]/3<a[1]){
 97             b[1]=a[3]/3;
 98             if(b[1]+a[3]==a[1]+a[2]){
 99                 flag=1;
100             }
101             else return;
102         }
103         else{
104             return;
105         }
106     }
107 }
108 
109 void solve4()
110 {
111     if(a[4]==3*a[1] && a[2]+a[3]==4*a[1]){
112         flag=1;
113     }
114 }
115 
116 void solve()
117 {
118     if(n==4){
119         solve4();
120     }
121     else if(n==0){
122         solve0();
123     }
124     else if(n==1){
125         solve1();
126     }
127     else if(n==2){
128         solve2();
129     }
130     else if(n==3){
131         solve3();
132     }
133 }
134 
135 void out()
136 {
137     int i;
138     if(flag==1){
139         printf("YES\n");
140         for(i=1;i<=4-n;i++){
141             printf("%d\n",b[i]);
142         }
143     }
144     else{
145         printf("NO\n");
146     }
147 }
148 
149 int main()
150 {
151     //freopen("data.in","r",stdin);
152     //freopen("data.out","w",stdout);
153     //scanf("%d",&T);
154     //for(int ccnt=1;ccnt<=T;ccnt++)
155    // while(T--)
156     while(scanf("%d",&n)!=EOF)
157     {
158        // if(n==0 && m==0 ) break;
159         //printf("Case %d: ",ccnt);
160         ini();
161         solve();
162         out();
163     }
164 
165     return 0;
166 }

 

 

posted on 2014-11-22 11:58  njczy2010  阅读(266)  评论(0编辑  收藏  举报