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.

Examples
input
2
1
1
output
YES
3
3
input
3
1
1
1
output
NO
input
4
1
2
2
3
output
YES
题目大意:输入一个整数n,代表n个糖果盒子,接下来n个数,代表每个糖果盒子中的糖果数;看是否可以添加4-n个糖果盒子,组成4个糖果盒子(糖果盒中的糖果数记为a<=b<=c<=d),
使得(a+b+c+d)/4=(b+c)/2=d-a。如果不可以,输出NO;否则,输出YES以及添加的糖果盒子中的糖果数。
方法及证明:
分类谈论。
(a+b+c+d)/4=(b+c)/2=d-a
得①d=3a;②b+c=4a.
将糖果数保存进box[]中,并升序排序。
(1)当n==0,肯定存在,输出YES以及1,1,3,3;
(2)当n==1时,也肯定存在,输出YES以及box[0],box[0]*3,box[0]*3;
(3)当n==2时,如果box[0]和box[1]分别在a和b位置不满足条件,那么他们在任何位置也不满足条件,下面给出证明:
  由①得d=3*box[0]>0(满足条件)
由②得c=4*box[0]-box[1]
如果要不满足条件,那么只能是4*box[0]-box[1]<=0,得box[1]>=4*box[0]
  Ⅰ当box[0]和box[1]分别在a和c位置时,b=4*box[0]-box[1]<=0,不满足条件;
  Ⅱ当box[0]和box[1]分别在a和d位置时,box[1]=3*a=3*box[0],因为box[1]>=4*box[0],所以,3*box[0]>=4*box[0],矛盾;
  Ⅲ当box[0]和box[1]分别在b和c位置时,a=(box[0]+box[1])/4>=(5/4)*box[0]>box[0]=b,不满足升序条件;
  Ⅳ当box[0]和box[1]分别在b和d位置时,a=d/3=box[1]/3>=(4/3)*box[0]>box[0]=b,不满足升序条件。
证毕。
  所以,只要满足4*box[0]-box[1]>0,就一定存在;否则一定不存在。
(4)当n==3时,根据①②分别讨论box[0]box[1]box[2]在b,c,d或a,c,d(或a,b,d这2种类似)或a,b,c位置的情形,如果你上面的证明看懂了,那么这个对你来说就是小case了。
(5)当n==4时,看满不满足(a+b+c+d)/4=(b+c)/2=d-a。
代码如下:
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
const int N=1e6;
int main()
{
int n;
int box[];
cin>>n;
int sum=;
for(int i=;i<n;i++)
{
cin>>box[i];
sum+=box[i];
}
sort(box,box+n);
if(n==)
{
float ave=sum/4.0;
float med=(box[]+box[])/2.0;
float range=box[]-box[];
if(ave==med&&med==range)cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
else if(n==)
{
if(box[]%==&&(box[]+box[])%==&&box[]/==(box[]+box[])/)
{
cout<<"YES"<<endl;
cout<<box[]/<<endl;
}
else if(box[]==box[]*&&box[]*>box[])
{
cout<<"YES"<<endl;
cout<<box[]*-box[]<<endl;
}
else if((box[]+box[])%==&&box[]==(box[]+box[])/)
{
cout<<"YES"<<endl;
cout<<box[]*<<endl;
}
else
cout<<"NO"<<endl;
}
else if(n==)
{
int c=box[]*-box[];
int d=box[]*;
if(c<=)cout<<"NO"<<endl;
else
{
cout<<"YES"<<endl;
cout<<c<<endl;
cout<<d<<endl;
}
}
else if(n==)
{
cout<<"YES"<<endl;
cout<<box[]<<endl;
cout<<box[]*<<endl;
cout<<box[]*<<endl;
}
else if(n==)
{
cout<<"YES"<<endl;
cout<<<<endl;
cout<<<<endl;
cout<<<<endl;
cout<<<<endl;
}
return ;
}
  
 

Codeforces 488B - Candy Boxes的更多相关文章

  1. Brute Force - B. Candy Boxes ( Codeforces Round #278 (Div. 2)

    B. Candy Boxes Problem's Link:   http://codeforces.com/contest/488/problem/B Mean: T题目意思很简单,不解释. ana ...

  2. Codeforces Round #229 (Div. 2) C. Inna and Candy Boxes 树状数组s

    C. Inna and Candy Boxes   Inna loves sweets very much. She has n closed present boxes lines up in a ...

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

    哎,最近弱爆了,,,不过这题还是不错滴~~ 要考虑完整各种情况 8795058                 2014-11-22 06:52:58     njczy2010     B - Ca ...

  4. codeforces 390C Inna and Candy Boxes

    这个题目看似不是很好下手,不过很容易发现每次询问的时候总是会问到第r个盒子是否有糖果: 这样的话就很好办事了: 维护两个数组: 一个sum数组:累加和: 一个in数组:如果i位是1的话,in[i]=i ...

  5. Educational Codeforces Round 31- D. Boxes And Balls

    D. Boxes And Balls time limit per test2 seconds memory limit per test256 megabytes 题目链接:http://codef ...

  6. codeforces A. Candy Bags 解题报告

    题目链接:http://codeforces.com/contest/334/problem/A 题意:有n个人,将1-n袋(第 i  袋共有 i  颗糖果,1<= i  <=n)所有的糖 ...

  7. [Codeforces 1053C] Putting Boxes Together

    Link: Codeforces 1053C 传送门 Solution: 先推出一个结论: 最后必有一个点不动且其为权值上最中间的一个点 证明用反证证出如果不在中间的点必有一段能用代价少的替代多的 这 ...

  8. codeforces 334A - Candy Bags

    忘了是偶数了,在纸上画奇数画了半天... #include<cstdio> #include<cstring> #include<cstdlib> #include ...

  9. cf C. Inna and Candy Boxes

    题意:给你一个长度为n的只含有1和0的字符串,w个询问,每次询问输入l,r:在[l,r]中在l+k-1.l+2*k-1.......r的位置都必须为1,如果不为1的,变成1,记为一次操作,其它的地方的 ...

随机推荐

  1. Spring Boot(十二):spring boot如何测试打包部署

    Spring Boot(十二):spring boot如何测试打包部署 一.开发阶段 1,单元测试 在开发阶段的时候最重要的是单元测试了,springboot对单元测试的支持已经很完善了. (1)在p ...

  2. centos/rhel 7 几个最重要变化(systemd,firewalld,networkmanager,文件系统)

    详细参考:https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/7/html/System_Administra ...

  3. rabbitmq heartbeat missing with heartbeat = N seconds原因总结

    一直以来,在我们大规模使用rabbitmq的服务端应用中,都没有出现rabbitmq心跳超时而造成的的影响,反倒是在rabbitmq-cpp客户端出现过很多次该问题,一直以为客户端lib实现的问题(不 ...

  4. 跟阿铭学Linux习题答案

    第一章:走进Linux 1.简述它的发展历史,列举几种代表性的发行版 Linux之前是Unix,由于Unix收费昂贵,so,Richard Stallman 发起了开发自由软件的运动,并成立了自由软件 ...

  5. vue2.0之echarts使用

    1.首先下载echart依赖 npm install echarts --save备注:npm 安装报错时使用cnpm 2.全局注册 在main.js里引入echart并在vue中注册echart / ...

  6. 尚硅谷面试第一季-15Mysql什么时候建索引

    课堂重点: MySQL的官方定义: 索引的优势: 索引的劣势: 那些情况下需要建立索引: 那些情况下不要建立索引: 何为过滤性:例如在数据库字段里,手机号/身份证号这些字段是过滤性好的字段,而性别则是 ...

  7. error C4996: Function call with parameters that may be unsafe – this call relies on the caller to ch

    在加入QCustomplot时有如题的错误 1>c:\program files (x86)\microsoft visual studio11.0\vc\include\xutility(21 ...

  8. ODAC(V9.5.15) 学习笔记(十一)TOraEncryptor、TOraPackage和TOraAlerter

    TOraEncryptor 名称 类型 说明 DataHeader TCREncDataHeader 一些附加信息放入加密数据中,包括: ehNone 无附加信息 ehTag   GUID和随机生成的 ...

  9. Spring-Cache 注解 @Cacheable,@CachePut , @CacheEvict

    1.自动生成key @Bean public KeyGenerator keyGenerator() { return new KeyGenerator() { @Override public Ob ...

  10. R语言 apply,sapply,lapply,tapply,vapply, mapply的用法

    apply() apply(m,dimcode,f,fargs) m 是一个矩阵. dimcode是维度编号,取1则为对行应用函数,取2则为对列运用函数. f是函数 fargs是f的可选参数集 > ...