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 }
时间: 2024-10-24 05:52:44

Codeforces Round #278 (Div. 2) B. Candy Boxes [brute force+constructive algorithms]的相关文章

codeforces水题100道 第二十题 Codeforces Round #191 (Div. 2) A. Flipping Game (brute force)

题目链接:http://www.codeforces.com/problemset/problem/327/A题意:你现在有n张牌,这些派一面是0,另一面是1.编号从1到n,你需要翻转[i,j]区间的牌一次,使得看到的牌是1的数量最大.C++代码: #include <iostream> using namespace std; const int maxn = 110; int n, a[maxn], sum[maxn]; int flip(int L, int R) { int a1 =

codeforces水题100道 第十三题 Codeforces Round #166 (Div. 2) A. Beautiful Year (brute force)

题目链接:http://www.codeforces.com/problemset/problem/271/A题意:给你一个四位数,求比这个数大的最小的满足四个位的数字不同的四位数.C++代码: #include <iostream> #include <algorithm> using namespace std; bool chk(int x) { int a[4]; for (int i = 0; i < 4; i ++) { a[i] = x % 10; x /= 1

Codeforces Round #278 (Div. 2) b

/**  *  * @brief Codeforces Round #278 (Div. 2) b  * @file b.c  * @author mianma  * @created 2014/11/24 17:52  * @edited  2014/11/18 17:52  * @type brute  *   * @note   *          declare k >= 0;  *              then   *                  x1 = k  *   

Codeforces Round #278 (Div. 2) d

/**  * @brief Codeforces Round #278 (Div. 2) d  * @file d.c  * @author 面码  * @created 2014/11/26 10:07  * @edited  2014/11/26 10:07  * @type dp   * @note  *      自己的TL了,看了别人代码写的  *      该代码主要是在dp的基础上使用stl来提速  *      dp需辅助提速,但内存又不能爆掉是该题目的卡点 = =  */ #i

Codeforces Round #278 (Div. 2) c

/**  * @brief Codeforces Round #278 (Div. 2) c  * @file c.c  * @author 面码  * @created 2014/11/25 14:15  * @edited  2014/11/25 14:15  * @type brute  *  */ #include <stdio.h> #define max(a, b)  ((a) > (b) ? (a) : (b)) #define min(a, b)  ((a) > (

Codeforces Round #278 (Div. 1)

A A monster is attacking the Cyberland! Master Yang, a braver, is going to beat the monster. Yang and the monster each have 3 attributes: hitpoints (HP), offensive power (ATK) and defensive power (DEF). During the battle, every second the monster's H

CodeForces Round #278 (Div.2) (待续)

A 这么简单的题直接贴代码好了. 1 #include <cstdio> 2 #include <cmath> 3 using namespace std; 4 5 bool islucky(int a) 6 { 7 a = abs(a); 8 while(a) 9 { 10 if(a % 10 == 8) return true; 11 a /= 10; 12 } 13 return false; 14 } 15 16 int main(void) 17 { 18 int a,

Codeforces Round #278 (Div. 2)

A直接暴力好了. 1 #include<bits/stdc++.h> 2 typedef long long ll; 3 using namespace std; 4 5 int pan(ll x) 6 { 7 while (x) 8 { 9 if (x%10==8) return 1; 10 x/=10; 11 } 12 return 0; 13 } 14 15 int main() 16 { 17 ll n; 18 cin>>n; 19 for (int i=1;i<=1

Codeforces Round #279 (Div. 2) ABCD

Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems # Name     A Team Olympiad standard input/output 1 s, 256 MB  x2377 B Queue standard input/output 2 s, 256 MB  x1250 C Hacking Cypher standard input/output 1 s, 256 MB  x740 D Chocolate standard input/