您的位置:首页 > 其它

codeforces 488B Candy Boxes

2014-11-29 11:57 537 查看
题目链接

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 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.

题意:4个数,x1<=x2<=x3<=x4, 且(x1+x2+x3+x4)/2 ==(x2+x3)/2==x4-x1。

已知n<=4个数,现在要构造4-n个数,让着4个数满足上述条件。

题解:由四个数的关系我们可以解得:3*x1==x4,x1+x4==x2+x3。

由于n<=4可以直接分类讨论即可。

但是我的做法是枚举x1,贪心构造满足条件的4个数。

代码如下:

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<string>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<stdlib.h>
#include<vector>
#define inff 0x3fffffff
#define nn 6100
#define mod 1000000007
typedef long long LL;
const LL inf64=inff*(LL)inff;
using namespace std;
int n;
int a[10];
bool use[10];
vector<int>ve;
int main()
{
int i,j;
while(scanf("%d",&n)!=EOF)
{
int mx=-inff;
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
mx=max(mx,a[i]);
}
if(n==0)
{
puts("YES");
puts("1");
puts("1");
puts("3");
puts("3");
continue;
}
int ix;
for(i=1;i<=mx;i++)
{
ve.clear();
ix=0;
memset(use,false,sizeof(use));
for(j=1;j<=n;j++)
{
if(a[j]==i)
{
ix++;
use[j]=true;
break;
}
}
if(j>n)
ve.push_back(i);
for(j=1;j<=n;j++)
{
if(use[j])
continue;
if(a[j]==3*i)
{
use[j]=true;
ix++;
break;
}
}
if(j>n)
ve.push_back(3*i);
if(n-ix>2)
continue;
if(n-ix==2)
{
int sum=0;
for(j=1;j<=n;j++)
{
if(use[j])
continue;
if(a[j]<=3*i&&a[j]>=i)
{
sum+=a[j];
}
else
break;
}
if(j<=n)
continue;
if(sum==4*i)
{
break;
}
}
if(n-ix==1)
{
for(j=1;j<=n;j++)
{
if(use[j])
continue;
if(a[j]<=3*i&&a[j]>=i)
{
ve.push_back(4*i-a[j]);
}
else
break;
}
if(j<=n)
continue;
else
break;
}
if(n-ix==0)
{
ve.push_back(i+1);
ve.push_back(3*i-1);
break;
}
}
if(i<=mx)
{
puts("YES");
sort(ve.begin(),ve.end());
int lv=ve.size();
for(i=0;i<lv;i++)
{
printf("%d\n",ve[i]);
}
}
else
puts("NO");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: