CF #278 (Div. 2) B.(暴力枚举+推导公式+数学构造)
来源:互联网 发布:矩阵防御下载破解 编辑:程序博客网 时间:2024/05/16 13:52
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)?
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).
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.
211
YES33
3111
NO
41223
YES
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.
解题思路:
题目大意就是要构造四个数,要求这四个数的 平均数 == 中位数 == 极差。给你n个数,问你能否构造出这样的序列。
首先推导公式: (a + b + c + d) / 4 == (b + c) / 2 == d - a ------------->>>>>> d : a = 3 : 1 && a + d == b + c -------->>>>>>>> 序列应该满足(x , y , 4 * x - y , 3 * x )。
由于题目给出a[ i ] 在[ 1 , 500 ]之间,所以对于x 在[ 1 , 500 ]内暴力枚举,相应的y在[ x , 3 * x ]内枚举,每次得出相应符合条件的四个数。然后判断给出的n个数是否和当前满足条件的数组进行匹配,如果发现匹配数正好等于n的话,说明可以构造出满足条件的序列。
完整代码:
#include <functional>#include <algorithm>#include <iostream>#include <fstream>#include <sstream>#include <iomanip>#include <numeric>#include <cstring>#include <climits>#include <cassert>#include <complex>#include <cstdio>#include <string>#include <vector>#include <bitset>#include <queue>#include <stack>#include <cmath>#include <ctime>#include <list>#include <set>#include <map>using namespace std;#pragma comment(linker, "/STACK:102400000,102400000")typedef long long LL;typedef double DB;typedef unsigned uint;typedef unsigned long long uLL;/** Constant List .. **/ //{const int MOD = int(1e9)+7;const int INF = 0x3f3f3f3f;const LL INFF = 0x3f3f3f3f3f3f3f3fLL;const DB EPS = 1e-9;const DB OO = 1e20;const DB PI = acos(-1.0); //M_PI;const int maxn = 10001;int a[maxn];int res[maxn];int c[5];int vis[5];int main(){ #ifdef DoubleQ freopen("in.txt","r",stdin); #endif int n; while(~scanf("%d",&n)) { for(int i = 1; i <= n ; i ++) scanf("%d",&a[i]); int flag = 0; int s; for(int x = 1; x <= 500 ; x ++) { for(int y = x ; y <= 3 * x ; y ++) { c[1] = x; c[2] = y; c[3] = 4 * x - y; c[4] = 3 * x; int cnt = 0; memset(vis , 0 ,sizeof(vis)); for(int i = 1 ; i <= n ; i ++) { for(int j = 1 ; j <= 4 ; j ++) { if(a[i] == c[j] && !vis[j]) { vis[j] = 1; cnt ++; break; } } } if(cnt == n) { s = 0; for(int i = 1 ; i <= 4 ; i ++) { if(!vis[i]) { res[s++] = c[i]; } } flag = 1; break; } } if(flag) break; } if(flag) { sort(res , res + s); printf("YES\n"); for(int i = 0 ; i < s ; i ++) { printf("%d\n", res[i]); } } else { printf("NO\n"); } }}
- CF #278 (Div. 2) B.(暴力枚举+推导公式+数学构造)
- CF#278 (Div. 2) A.(暴力枚举)
- CF#284 (Div. 2) B.(暴力)
- CF#368 B. Bakery (暴力枚举)
- CF#274 (Div. 2) B.(暴力模拟)
- CF#368 C. Pythagorean Triples (数学/推导公式)
- cf 551B 暴力枚举
- CF#275 (Div. 2) C(数学构造)
- CF 178(div 2)B(greedy + 枚举)
- cf#276-B - Maximum Value- (数学+暴力)/(二分)
- Codeforces Round #278 (Div. 2)---B. Candy Boxes (暴力+构造)
- (CF)B. Restore Cube (暴力枚举判断)
- 枚举暴力 cf B. Preparing Olympiad
- Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) -- B. Batch Sort(暴力枚举)
- Codeforces Round #243 (Div. 2) A,B,C(暴力枚举)
- Codeforces Round #392(Div. 2) B Blown Garland【暴力枚举】
- Codeforces Round #402 (Div. 2)B. Weird Rounding【暴力枚举】
- Codeforces Round #392(Div. 2) B Blown Garland【暴力枚举】
- ubuntu好用的源收集
- SimpleDateFormat类详解
- 设备适配以及dp、ppi、sp等相关概念图文理解
- iOS状态栏颜色以及版本判断
- jdbc连mysql的两种方法
- CF #278 (Div. 2) B.(暴力枚举+推导公式+数学构造)
- 《实战nginx》第3章-nginx基本配置与优化
- ch2.HTML,javascript简介-- javascript
- Your boot partition is on a disk using the GPT
- 黑马程序员——JAVA基础之程序控制流结构之循环结构,循环嵌套
- 这是我的文章不要乱转载啊谢谢
- Using the Raspberry Pi as an RTL-SDR streaming server
- uva 1201
- java数据结构-HashMap