Codeforces Round #278 (Div. 2) B. Candy Boxes [brute force+constructive algorithms]
哎,最近弱爆了,,,不过这题还是不错滴~~ 要考虑完整各种情况
8795058 | 2014-11-22 06:52:58 | njczy2010 | B - Candy Boxes | GNU C++ | Accepted | 31 ms | 4 KB |
8795016 | 2014-11-22 06:48:15 | njczy2010 | B - Candy Boxes | GNU C++ | Wrong answer on test 13 | 30 ms | 0 KB |
8795000 | 2014-11-22 06:44:39 | njczy2010 | B - Candy Boxes | GNU C++ | Wrong answer on test 5 | 15 ms | 0 KB |
1 second
256 megabytes
standard input
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)?
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.
2 1 1
YES 3 3
3 1 1 1
NO
4 1 2 2 3
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 all 2.
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.
主要就是根据那三个等式,化简,得两个方程:
x4=3*x1;
x2+x3=4*x1;
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<queue>
#include<map>
#include<set>
#include<string>
//#include<pair> #define N 10005
#define M 1005
#define mod 1000000007
//#define p 10000007
#define mod2 1000000000
#define ll long long
#define LL long long
#define eps 1e-9
#define maxi(a,b) (a)>(b)? (a) : (b)
#define mini(a,b) (a)<(b)? (a) : (b) using namespace std; int n;
int a[];
int b[];
int flag; void ini()
{
flag=;
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
}
sort(a+,a++n);
} void solve0()
{
flag=;
b[]=;b[]=;b[]=;b[]=;
} void solve1()
{
flag=;
b[]=a[];b[]=*a[];b[]=*a[];
} void solve2()
{
if(a[]%==){
if(a[]/==a[]){
flag=;
b[]=a[];b[]=a[];
}
else if(a[]/<a[]){
flag=;b[]=a[]/;b[]=a[]+b[]-a[];
}
else{
return;
}
}
else{
if(a[]*<=a[]){
return;
}
else{
flag=;
b[]=a[]*;
b[]=b[]+a[]-a[];
}
}
} void solve3()
{
if(a[]%!=){
if(a[]*<=a[]){
return;
}
else{
if(a[]*==a[]+a[]){
flag=;b[]=a[]*;
}
else return;
}
}
else{
if(a[]/==a[]){
flag=;
b[]=a[]+a[]-a[];
}
else if(a[]/<a[]){
b[]=a[]/;
if(b[]+a[]==a[]+a[]){
flag=;
}
else return;
}
else{
return;
}
}
} void solve4()
{
if(a[]==*a[] && a[]+a[]==*a[]){
flag=;
}
} void solve()
{
if(n==){
solve4();
}
else if(n==){
solve0();
}
else if(n==){
solve1();
}
else if(n==){
solve2();
}
else if(n==){
solve3();
}
} void out()
{
int i;
if(flag==){
printf("YES\n");
for(i=;i<=-n;i++){
printf("%d\n",b[i]);
}
}
else{
printf("NO\n");
}
} int main()
{
//freopen("data.in","r",stdin);
//freopen("data.out","w",stdout);
//scanf("%d",&T);
//for(int ccnt=1;ccnt<=T;ccnt++)
// while(T--)
while(scanf("%d",&n)!=EOF)
{
// if(n==0 && m==0 ) break;
//printf("Case %d: ",ccnt);
ini();
solve();
out();
} return ;
}
Codeforces Round #278 (Div. 2) B. Candy Boxes [brute force+constructive algorithms]的更多相关文章
- codeforces水题100道 第二十题 Codeforces Round #191 (Div. 2) A. Flipping Game (brute force)
题目链接:http://www.codeforces.com/problemset/problem/327/A题意:你现在有n张牌,这些派一面是0,另一面是1.编号从1到n,你需要翻转[i,j]区间的 ...
- codeforces水题100道 第十九题 Codeforces Round #109 (Div. 2) A. I_love_%username% (brute force)
题目链接:http://www.codeforces.com/problemset/problem/155/A题意:找到当前最大值或者最小值出现的次数.“当前”的意思差不多是a[i]大于所有a[j]( ...
- codeforces水题100道 第十三题 Codeforces Round #166 (Div. 2) A. Beautiful Year (brute force)
题目链接:http://www.codeforces.com/problemset/problem/271/A题意:给你一个四位数,求比这个数大的最小的满足四个位的数字不同的四位数.C++代码: #i ...
- 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 ...
- Codeforces Round #278 (Div. 2)
题目链接:http://codeforces.com/contest/488 A. Giga Tower Giga Tower is the tallest and deepest building ...
- Codeforces Round #278 (Div. 1) B. Strip multiset维护DP
B. Strip Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/487/problem/B De ...
- Codeforces Round #278 (Div. 1) A. Fight the Monster 暴力
A. Fight the Monster Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/487/ ...
- CodeForces Round #278 (Div.2) (待续)
A 这么简单的题直接贴代码好了. #include <cstdio> #include <cmath> using namespace std; bool islucky(in ...
- Codeforces Round #278 (Div. 1)
A A monster is attacking the Cyberland! Master Yang, a braver, is going to beat the monster. Yang an ...
随机推荐
- Hermite 矩阵的特征值不等式
将要学习 关于 Hermite 矩阵的特征值不等式. Weyl 定理 以及推论. Weyl 定理 Hermann Weyl 的如下定理是大量不等式的基础,这些不等式要么涉及两个 Hermite 矩 ...
- Java生成固定长度的随机字符串(以大小写字母和数字)
package org.jimmy.autosearch2019.test; import java.util.ArrayList; import java.util.Random; /** * @a ...
- java反序列化字节转字符串工具
https://github.com/NickstaDB/SerializationDumper SerializationDumper-v1.1.jar 用法 : java -jar Seriali ...
- vue计算属性无法监听到数组内部变化
计算属性可以帮助我们简化代码,做到实时更新,不用再自己添加function去修改data. 首先看一下计算属性的基本写法(摘自官网) var vm = new Vue({ el: '#demo', d ...
- off-by-one&doublefree. 看雪10月ctf2017 TSRC 第四题赛后学习
off-by-one 0x00 发现漏洞 1.off-by-one 在massage函数中,如图所示,可以修改的字节数比原内存大小多了一个字节 2.悬挂指针 可以看到,在free堆块的时候,没有清空指 ...
- Java中的线程--线程的互斥与同步通信
Java中的线程之前也提到过,但是还是想再详细的学习一下,跟着张孝祥老师,系统的再学习一下. 一.线程中的互斥 线程安全中的问题解释:线程安全问题可以用银行中的转账 例题描述: 线程A与线程B分别访问 ...
- Bootstrap历练实例:链接样式按钮
<!DOCTYPE html><html><head> <meta http-equiv="Content-Type" content=& ...
- java mongodb 增删改查 工具类
package com.jttx.demo; import com.mongodb.*; import com.mongodb.util.JSON; import java.net.Unkno ...
- 使用custom elements和Shadow DOM自定义标签
具体的api我就不写 官网上面多 如果不知道这个的话也可以搜索一下 目前感觉这个还是相当好用的直接上码. <!DOCTYPE html> <html lang="en&q ...
- PAT Basic 1029
1029 旧键盘 旧键盘上坏了几个键,于是在敲一段文字的时候,对应的字符就不会出现.现在给出应该输入的一段文字.以及实际被输入的文字,请你列出肯定坏掉的那些键. 输入格式: 输入在2行中分别给出应该输 ...