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. EventSystem

    Unity5.0 EventSystem事件系统的详细说明 一.EventSystem对象的说明 当我们在场景中创建任一UI对象后,Hierarchy面板中都可以看到系统自动创建了对象EventSys ...

  2. JavaScript注意事项

    var m = "false"; Boolean(m); // true而非 false ajax不能设置setRequestHeaders("Cookie", ...

  3. POJ 1704 Georgia and Bob (Nim游戏变形)

    题目:http://poj.org/problem?id=1704 思路:Nim游戏策略,做如下转换,如果N是偶数,则两两配对,将两个数之间的格子数(距离)看做成这一堆石头的数量. 如果N是奇数,则将 ...

  4. RR 和RC 幻读问题

    <pre name="code" class="html">显然 RR 支持 gap lock(next-key lock),而RC则没有gap l ...

  5. 【HDOJ】3732 Ahui Writes Word

    初看01背包,果断TLE.是因为n和C都比较大.但是vi和ci却很小,转化为多重背包. #include <cstdio> #include <cstring> ][]; ]; ...

  6. (2015年郑州轻工业学院ACM校赛题) C 数列

    在我们做完B题之后就去看C题了, 发现很多人都已经做出来了, 并且一血还是我们学弟拿的, 感觉这题不难, 我们举了几个例子之后发现全是Alice 然后我们就决定意淫一下,试试看! 没想到就A了 - . ...

  7. 【转】Android 使用Scroller实现绚丽的ListView左右滑动删除Item效果

    原文网址:http://blog.csdn.net/xiaanming/article/details/17539199 转帖请注明本文出自xiaanming的博客(http://blog.csdn. ...

  8. 【转】android 4.3 BLE onCharacteristicWrite没有回调

    原文网址:http://bbs.csdn.net/topics/390882717?page=1 问题1.我在自己程序有开一个Timer定时去readCharacteristic, 每次read可以成 ...

  9. HDOJ(HDU) 1718 Rank(水题、、、)

    Problem Description Jackson wants to know his rank in the class. The professor has posted a list of ...

  10. 基于deb包快速搭建内外apt源