Atoms: There and Back Again

Time limit 2 seconds
Memory limit 256Mb
Input stdin
Output stdout

Legend

Yura and Roman got bored with the original “Atoms” board game and came up with a better game that uses the same playing set. The game starts with one group of atoms. On each step, a player chooses number X and divides each group of atoms into two non-empty groups, at least one of them consisting ofX atoms. If there is no such X that it can be done, the game stops. The winner in this situation is... Roman and Yura have not decided yet, and it is currently irrelevant. Roman has just distributed some atoms into groups not following any specific rule, and now he wonders: is it possible to get this position while playing this new game?

Input format

The first line of input holds one integer N (1 ≤ N ≤ 105): the number of groups. The second line holds Nspace-separated integers Ai: the number of atoms in i-th group (1 ≤ Ai ≤ 1018). If you are using stdinin C/C++, use format %lld for reading Ai.

Output format

Output a single word “YES” if the given position can be reached from a single group by the new rules, or “NO” otherwise.

Sample 1

Input Output
4
1 2 6 1
YES

Sample 2

Input Output
4
1 2 3 4
NO

Notes

In the first example, players can start with a group of 10, divide it into 3 and 7, then further divide the groups to get 1, 2, 1, 6.

C++ STL 中对List 的第一次运用。。

题目没什么好说的。。

因为每次必定有1个数超过半数(不可能有2个,0个无解)

那么不断模拟往上推就行了。。

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<functional>
#include<iostream>
#include<cmath>
#include<cctype>
#include<ctime>
#include<list>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Lson (x<<1)
#define Rson ((x<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (100000007)
#define MAXN (100000+10)
long long mul(long long a,long long b){return (a*b)%F;}
long long add(long long a,long long b){return (a+b)%F;}
long long sub(long long a,long long b){return (a-b+(a-b)/F*F+F)%F;}
typedef long long ll;
typedef unsigned long long ull;
int n;
ull a[MAXN];
list<int> L;
int main()
{
// freopen("yandex-2013 Q-Atoms.in","r",stdin);
scanf("%d",&n);
For(i,n) cin>>a[i];
sort(a+1,a+1+n);
For(i,n) L.push_back(a[i]);
while (n>1)
{
if (n&1) {puts("NO");return 0; }
int tot=0,p=-1;
for(list<int>::iterator it=L.begin();it!=L.end();it++)
{
if (p==*it) tot++;else tot=1,p=*it;
if (tot>=n/2) break;
}
if (tot<n/2) {puts("NO");return 0; }
for(list<int>::iterator it=L.begin();it!=L.end();)
{
list<int>::iterator it2=it;
if (p==*it&&tot) tot--,it++,L.erase(it2);else *it+=p,it++;
}
n>>=1;
}
puts("YES"); return 0;
}

Yandex 2013Q(Atoms: There and Back Again-贪心+模拟+List)的更多相关文章

  1. 贪心+模拟 Codeforces Round #288 (Div. 2) C. Anya and Ghosts

    题目传送门 /* 贪心 + 模拟:首先,如果蜡烛的燃烧时间小于最少需要点燃的蜡烛数一定是-1(蜡烛是1秒点一支), num[g[i]]记录每个鬼访问时已点燃的蜡烛数,若不够,tmp为还需要的蜡烛数, ...

  2. 贪心+模拟 ZOJ 3829 Known Notation

    题目传送门 /* 题意:一串字符串,问要最少操作数使得成为合法的后缀表达式 贪心+模拟:数字个数 >= *个数+1 所以若数字少了先补上在前面,然后把不合法的*和最后的数字交换,记录次数 岛娘的 ...

  3. CodeForces ---596B--Wilbur and Array(贪心模拟)

    Wilbur and Array Time Limit: 2000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u Su ...

  4. hdu 4023 2011上海赛区网络赛C 贪心+模拟

    以为是贪心,结果不是,2333 贪心最后对自己绝对有利的情况 点我 #include<cstdio> #include<iostream> #include<algori ...

  5. HDU 4023 (博弈 贪心 模拟) Game

    如果硬要说这算是博弈题目的话,那这个博弈是不公平博弈(partizan games),因为双方面对同一个局面做出来的决策是不一样的. 我们平时做的博弈都是公平博弈(impartial games),所 ...

  6. UVA 10714 Ants 蚂蚁 贪心+模拟 水题

    题意:蚂蚁在木棍上爬,速度1cm/s,给出木棍长度和每只蚂蚁的位置,问蚂蚁全部下木棍的最长时间和最短时间. 模拟一下,发现其实灰常水的贪心... 不能直接求最大和最小的= =.只要求出每只蚂蚁都走长路 ...

  7. UVA 311 Packets 贪心+模拟

    题意:有6种箱子,1x1 2x2 3x3 4x4 5x5 6x6,已知每种箱子的数量,要用6x6的箱子把全部箱子都装进去,问需要几个. 一开始以为能箱子套箱子,原来不是... 装箱规则:可以把箱子都看 ...

  8. CodeForces 797C Minimal string:贪心+模拟

    题目链接:http://codeforces.com/problemset/problem/797/C 题意: 给你一个非空字符串s,空字符串t和u.有两种操作:(1)把s的首字符取出并添加到t的末尾 ...

  9. CodeForces - 730A 贪心+模拟

    贪心策略: 1.只有一个最大值,选着第二大的一起参加比赛减分. 2.有奇数个最大值,选择三个进行比赛. 3.偶数个最大值,选择两个进行比赛. 为什么不把最大值全部选择? 因为最多只能选五个,有可能选择 ...

随机推荐

  1. 我的Jekyll博客

    我在GitHub Page上托管的Jekyll博客地址:http://lastavenger.github.io/

  2. Solr4.8.0源码分析(5)之查询流程分析总述

    Solr4.8.0源码分析(5)之查询流程分析总述 前面已经写到,solr查询是通过http发送命令,solr servlet接受并进行处理.所以solr的查询流程从SolrDispatchsFilt ...

  3. 7月12日至芯FPGA就业班招生

    至芯科技FPGA就业培训班将于2014年7月12日正式开班,届时至芯科技将秉承着一贯的教学传统,为广大师生打造一个专业的技术平台和轻松的学习环境. 在夏宇闻教授的带领下,至芯科技汇聚了一批来自国内外大 ...

  4. poj 2983Is the Information Reliable?

    http://poj.org/problem?id=2983 #include<cstdio> #include<cstring> #include<algorithm& ...

  5. 【HDOJ】1716 排列2

    STL. /* 1716 */ #include <iostream> #include <algorithm> #include <cstdio> #includ ...

  6. -_-#【Better Code】字符串匹配

    提高 web 应用性能之 JavaScript 性能调优

  7. 设计模式(四):SIMPLE FACTORY简单工厂模式 -- 创建型模式

    1.定义 简单工厂模式又称静态工厂方法模式.重命名上就可以看出这个模式一定很简单.它存在的目的很简单:定义一个用于创建对象的接口. 2.适用场景 如果一个客户要一款宝马车,一般的做法是客户去创建一款宝 ...

  8. HDU4757--Tree 可持久化trie + LCA

    题意:n个点的树,Q次询问,询问u-v路径上的点的权值与z抑或的最大值. 先考虑,在一个区间上的问题,可以先建一个可持久化的Trie,然后每次询问,就和线段树的操作差不多,从最高位开始考虑选1还是选0 ...

  9. HDU2058

    The sum problem Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  10. POJ 2718 穷举

    题意:给定一组数字,如0, 1, 2, 4, 6, 7,用这些数字组成两个数,并使这两个数之差最小.求这个最小差.在这个例子上,就是204和176,差为28. 分析:首先可以想到,这两个数必定是用各一 ...