Codeforces 488B Candy Boxes【思维+分类讨论】
来源:互联网 发布:淘宝上的蛋白粉真假 编辑:程序博客网 时间:2024/05/08 16:11
There is an old tradition of keeping 4 boxes of candies in the house in Cyberland. The numbers of candies arespecial if theirarithmetic mean, theirmedian and theirrange are all equal. By definition, for a set{x1, x2, x3, x4} (x1 ≤ x2 ≤ x3 ≤ x4)arithmetic mean is ,median is andrange isx4 - 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 to2.
Jeff has 4 special boxes of candies. However, something bad has happened! Some of the boxes could have been lost and now there are onlyn (0 ≤ n ≤ 4) boxes remaining. Thei-th remaining box containsai candies.
Now Jeff wants to know: is there a possible way to find the number of candies of the4 - n missing boxes, meeting the condition above (the mean, median and range are equal)?
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 thei-th box (1 ≤ ai ≤ 500).
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 integerb, denoting the number of candies in a missing box.
All your numbers b must satisfy inequality1 ≤ b ≤ 106. It is guaranteed that if there exists a positive integer solution, you can always find suchb'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 lowestn first positions.
211
YES33
3111
NO
41223
YES
For the first sample, the numbers of candies in 4 boxes can be1, 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.
题目大意:
给你n个数,如果能找到4-n个数,使得这四个数满足:x1<=x2<=x3<=x4&&(x1+x2+x3+x4)/4==(x2+x3)/2==x4-x1;输出YES,并且输出其他4-n个数,否则输出NO。
思路:
1、首先有:
(x1+x2+x3+x4)/4==(x2+x3)/2------------------->能够推出x2+x3==x1+x4;
(x2+x3)/2==x4-x1-------------->能够推出x2+x3==2x4-2x1
将上述两个式子联立,能够得出:x4=x1*3的结论。
2、接下来我们分类讨论:
①n==0,输出任意可行解即可。
②n==1,输出a【1】,a【1】*3,a【1】*3即可(其实也是任意可行解)。
③n==2,首先输入没有保证递增性,那么我们要将这两个数排序,然后设定a【4】=a【1】*3,那么a【3】的值我们可以通过枚举来判定,因为结果输出保证了是1e6内的数据,那么我们枚举一层for,如果有可行解输出即可,如果没有可行解输出NO。
④n==3,直接枚举最后一个数,枚举出来之后,将这四个数排序,并且判断是否是一个可行解,如果是,输出即可。
⑤n==4,直接判断是否是一个可行解即可。
3、细节参考代码、
Ac代码:
#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;int a[15];int ans[15];int main(){ int n; while(~scanf("%d",&n)) { for(int i=1; i<=n; i++) { scanf("%d",&a[i]); } sort(a+1,a+n+1); if(n==0) { printf("YES\n1\n1\n3\n3\n"); } if(n==4) { if((a[1]+a[2]+a[3]+a[4])/4==(a[2]+a[3])/2&&(a[2]+a[3])/2==a[4]-a[1]) { printf("YES\n"); } else printf("NO\n"); } if(n==1) { printf("YES\n"); printf("%d\n%d\n%d\n",a[1],a[1]*3,a[1]*3); } if(n==2) { a[4]=a[1]*3; int flag=0; for(int i=1; i<=1000000; i++) { a[3]=i; if((a[1]+a[2]+a[3]+a[4])/4==(a[2]+a[3])/2&&(a[2]+a[3])/2==a[4]-a[1]) { flag=1; printf("YES\n"); break; } } if(flag==0)printf("NO\n"); else { printf("%d\n%d\n",a[3],a[4]); } } if(n==3) { int flag=0; for(int i=1; i<=1000000; i++) { a[4]=i; int tmp[5]; tmp[1]=a[1]; tmp[2]=a[2]; tmp[3]=a[3]; tmp[4]=a[4]; sort(tmp+1,tmp+5); if(tmp[1]*3!=tmp[4])continue; if((tmp[1]+tmp[2]+tmp[3]+tmp[4])/4==(tmp[2]+tmp[3])/2&&(tmp[2]+tmp[3])/2==tmp[4]-tmp[1]) { flag=1; printf("YES\n%d\n",i); break; } } if(flag==0)printf("NO\n"); } }}
- Codeforces 488B Candy Boxes【思维+分类讨论】
- codeforces 488B Candy Boxes
- Codeforces 488B - Candy Boxes (构造)
- Codeforces 488 B. Candy Boxes(构造)
- Codeforces 488B Candy Boxes(水)
- Codeforces 560B Gerald is into Art【思维、分类讨论】
- Codeforces Round #278 (Div. 2)B—— Candy Boxes
- Codeforces Round #278 (Div. 2) B - Candy Boxes
- Codeforces Round #278 (Div. 2) B. Candy Boxes
- Codeforces Round #278 (Div. 2)B. Candy Boxes
- codeforces 789B(分类讨论)
- Codeforces 617D Polyline【思维+分类讨论】
- CodeForces 44B- A - Cola- 分类讨论
- Codeforces Problem 709B Checkpoints(分类讨论)
- CodeForces 141B Hopscotch(分类讨论)
- Codeforces Round #278 (Div. 2)---B. Candy Boxes (暴力+构造)
- Codeforces Round #278 (Div. 2) B. Candy Boxes (模拟,数学)
- CodeForces 390C Inna and Candy Boxes
- 谷歌地图高清卫星地图
- httpServlet,GenericServlet,Servlet源码分析
- Cortex—A8处理器编程模型
- 2016.09.28 list.c
- c++中static变量在main函数之前执行
- Codeforces 488B Candy Boxes【思维+分类讨论】
- Android NDk开发系列(疑难杂症篇)
- 串口通信模块3:串口通信编程基础(读写、关闭)
- LVS介绍
- BZOJ3503: [Cqoi2014]和谐矩阵 解题报告
- X264的时耗分析
- 伙伴系统之避免碎片--Linux内存管理(十六)
- 百度离线地图金字塔瓦片发布
- C++ 文件操作