Yandex 2013Q(Atoms: There and Back Again-贪心+模拟+List)
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 |
YES |
Sample 2
| Input | Output |
|---|---|
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)的更多相关文章
- 贪心+模拟 Codeforces Round #288 (Div. 2) C. Anya and Ghosts
题目传送门 /* 贪心 + 模拟:首先,如果蜡烛的燃烧时间小于最少需要点燃的蜡烛数一定是-1(蜡烛是1秒点一支), num[g[i]]记录每个鬼访问时已点燃的蜡烛数,若不够,tmp为还需要的蜡烛数, ...
- 贪心+模拟 ZOJ 3829 Known Notation
题目传送门 /* 题意:一串字符串,问要最少操作数使得成为合法的后缀表达式 贪心+模拟:数字个数 >= *个数+1 所以若数字少了先补上在前面,然后把不合法的*和最后的数字交换,记录次数 岛娘的 ...
- CodeForces ---596B--Wilbur and Array(贪心模拟)
Wilbur and Array Time Limit: 2000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64u Su ...
- hdu 4023 2011上海赛区网络赛C 贪心+模拟
以为是贪心,结果不是,2333 贪心最后对自己绝对有利的情况 点我 #include<cstdio> #include<iostream> #include<algori ...
- HDU 4023 (博弈 贪心 模拟) Game
如果硬要说这算是博弈题目的话,那这个博弈是不公平博弈(partizan games),因为双方面对同一个局面做出来的决策是不一样的. 我们平时做的博弈都是公平博弈(impartial games),所 ...
- UVA 10714 Ants 蚂蚁 贪心+模拟 水题
题意:蚂蚁在木棍上爬,速度1cm/s,给出木棍长度和每只蚂蚁的位置,问蚂蚁全部下木棍的最长时间和最短时间. 模拟一下,发现其实灰常水的贪心... 不能直接求最大和最小的= =.只要求出每只蚂蚁都走长路 ...
- UVA 311 Packets 贪心+模拟
题意:有6种箱子,1x1 2x2 3x3 4x4 5x5 6x6,已知每种箱子的数量,要用6x6的箱子把全部箱子都装进去,问需要几个. 一开始以为能箱子套箱子,原来不是... 装箱规则:可以把箱子都看 ...
- CodeForces 797C Minimal string:贪心+模拟
题目链接:http://codeforces.com/problemset/problem/797/C 题意: 给你一个非空字符串s,空字符串t和u.有两种操作:(1)把s的首字符取出并添加到t的末尾 ...
- CodeForces - 730A 贪心+模拟
贪心策略: 1.只有一个最大值,选着第二大的一起参加比赛减分. 2.有奇数个最大值,选择三个进行比赛. 3.偶数个最大值,选择两个进行比赛. 为什么不把最大值全部选择? 因为最多只能选五个,有可能选择 ...
随机推荐
- 过多if-else分支的优化
http://www.udpwork.com/item/9294.html 我想谈一谈这个话题是因为我的上一篇博客在ITEye上有一些朋友回复,说if-else过多的分支可以使用switch或者责任链 ...
- POJ 3268 Silver Cow Party ( Dijkstra )
题目大意: 有N个农场每个农场要有一头牛去参加一个聚会,连接每个农场有m条路, 聚会地点是X,并且路是单向的.要求的是所有牛赶到聚会地点并且回到自己原先的农场所需要的最短时间. 题目分析: 其实就是以 ...
- List<T> 和DataTable的相互转换
我用的将集合类转换为DataTable 的方法 /// <summary> /// 将集合类转换成DataTable /// </summary> /// <param ...
- P2P风险淮安样本:5000万连锁漩涡牵出银行内案
春节后第一个工作周,来自南京的投资人李宏(化名)频繁游走于两个维权群,因为在江苏淮安的网贷平台投资经历,他结识了136名P2P投资人. 在他们的QQ群里,每个投资人的备注均为网名+投资额,如他自己的“ ...
- 两个bootstrap插件bootstrap-select和bootstrap-paginator
基于bootstrap的选择器 http://silviomoreto.github.io/bootstrap-select/ <label for="androids" c ...
- HDU5406---CRB and Apple( DP) 2015 Multi-University Training Contest 10
题意比较简单, dp[i][j] 表示上一次男女吃的deliciousness分别为i, j的时候的吃的最多的苹果. 那么dp[i][j] = max(dp[i][k] + 1), 0 < ...
- CapsLock indicator on Ubuntu for Thinkpad
http://askubuntu.com/questions/292535/how-to-get-caps-num-scroll-lock-keys-osd-notification sudo add ...
- mysql查询最近一小时的数据
date_sub()函数: DATE_SUB(date,INTERVAL expr type) 实例: SELECT NOW(),DATE_SUB(NOW(),INTERVAL HOUR) as th ...
- 二、linux文件系统之linux启动
Linux组成 kernel shell 文件系统 application(应用程序) 标准库函数 内核源码位置: /usr/src /boot/vmlinuz*(内核压缩文件,启动要加载) ...
- 【建模】UML类关系分析
一.UML类关系分析 1.关联(asociation) 一般是一方拥有另一方对象的指针.箭头的方向是访问方向. 2.聚合(Aggregation)和组合(Composition) 聚合和关联一般不做区 ...