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
output
input
output
input
output
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个数。
代码如下:
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; }
相关文章推荐
- codeforces 258B Little Elephant and Elections 数位DP+DFS
- CodeForces 534F Simplified Nonogram (记忆化搜索)
- Codeforces 6225B KMP
- Codeforces 115A. Party
- CodeForces - 729D(思路)
- CodeForces 158 A.Next Round(水~)
- CodeForces-884D:Boxes And Balls(合并石子)
- codeforces 166?
- Codeforces 492E Vanya and Field(拓展欧几里得)
- Codeforces 653C Bear and Up-Down 【模拟】
- Codeforces 707D.Persistent Bookcase(离线算法,dfs树,好题!)
- CodeForces 776D The Door Problem【并查集】
- CodeForces 22b_Bargaining Table_dp(暴力)
- codeforces 82A Double Cola
- [代码]codeforces 274B Zero Tree
- 【贪心】Codeforces 353E Antichain
- codeforces 598C C. Nearest vectors(极角排序)
- Codeforces- 124B-Permutations (深搜找全排列以及取出一个数的每一位)
- Codeforces 583C GCD Table【贪心】
- CodeForces - 822C Hacker, pack your bags!(区间端点处理技巧+思维)