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 ...
随机推荐
- UVA821 PageHopping (Floyd)
求所有点直接的平均最短距离,保存一下出现过的点,题目保证是所有点连通,Floyd求出最短路以后两个for统计一下. #include<bits/stdc++.h> using namesp ...
- DRBD+NFS+Keepalived高可用环境
1.前提条件 准备两台配置相同的服务器 2.安装DRBD [root@server139 ~]# yum -y update kernel kernel-devel [root@server139 ~ ...
- Linux系统分区 进程管理 软件包安装
对于一块新的磁盘来说,系统能够使用需要有分区.格式化文件系统.挂载等主要操作,下面通过命令的方式对一块磁盘进行操作. 一. Linux系统分区 1.1 在虚拟机开机前选择虚拟机配置,添加一个新的SCS ...
- PAT (Basic Level) Practise (中文)-1025. 反转链表 (25)
PAT (Basic Level) Practise (中文)-1025. 反转链表 (25) http://www.patest.cn/contests/pat-b-practise/1025 ...
- ios 注册功能研究学习
通常,移动App的注册功能通常采用手机号码注册或者邮箱帐号注册. 不过在国内这样隐私堪忧的环境下,需要手机号来注册会流失不少用户.即便是新浪微博这样的应用,需要绑定手机号也令我不信任.除非是像淘宝.支 ...
- PAT 乙级 1019
题目 题目地址:PAT 乙级 1019 思路 本题没有考虑到小于1000的情况,当小于1000的时需要给vector的向量中推入0,直到向量中有四位数字,之后再进行排序并进行相关计算 代码 #incl ...
- Linux系统状态检测
基于Red Hat Enterprise Linux 7.5 1.ifconfig ifconfig用于获取和配置网络接口的网络参数,格式为“ifconfig [网络设备] [参数]” 参数: add ...
- Maven项目:@Override is not allowed when implement interface method
今天新建一个maven项目实现接口方法的时候报错编译不通过@Override is not allowed when implement interface method,要配置pom文件的compi ...
- 常用JS方法整理
目录: 截取指定字节数的字符串 判断是否微信 获取时间格式的几个举例 获取字符串字节长度 对象克隆.深拷贝 组织结构代码证验证 身份证号验证 js正则为url添加http标识 URL有效性校验方法 自 ...
- sed快速学习