Codeforces G. Ant colony】的更多相关文章

题目描述: F. Ant colonytime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard output Mole is hungry again. He found one ant colony, consisting of n ants, ordered in a row. Each ant i (1 ≤ i ≤ n) has a strength si.…
Ant colony 题解: 因为一个数是合法数,那么询问区间内的其他数都要是这个数的倍数,也就是这个区间内的gcd刚好是这个数. 对于这个区间的gcd来说,不能通过前后缀来算. 所以通过ST表来询问这个区间的gcd. 那么题目就变成了询问一个区间内有多少个k. 我们对于每个数都离散化之后,在相应的数字存下下标. 然后二分一下个数. 代码: #include<bits/stdc++.h> using namespace std; #define Fopen freopen("_in.…
注意到每个区间生存下来的蚂蚁的长度等于区间的gcd 于是可以先预处理出区间的gcd 然后二分查找就好了 预处理gcd我这里用的是倍增法 总的时间复杂度O(NlogN) /* Cf 271F 倍增求区间GCD 对下标二分 时间复杂度O(NlogN) */ #include <iostream> #include <algorithm> #include <vector> #include <map> using namespace std; ; ]; int…
线段树求某一段的GCD..... F. Ant colony time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Mole is hungry again. He found one ant colony, consisting of n ants, ordered in a row. Each ant i (1 ≤ i ≤ n)…
F. Ant colony time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Mole is hungry again. He found one ant colony, consisting of n ants, ordered in a row. Each ant i (1 ≤ i ≤ n) has a strength si…
[BZOJ3872][Poi2014]Ant colony 试题描述 There is an entrance to the ant hill in every chamber with only one corridor leading into (or out of) it. At each entry, there are g groups of m1,m2,...,mg ants respectively. These groups will enter the ant hill one…
[BZOJ3872]Ant colony(二分,动态规划) 题面 又是权限题... Description There is an entrance to the ant hill in every chamber with only one corridor leading into (or out of) it. At each entry, there are g groups of m1,m2,...,mg ants respectively. These groups will ent…
3872: [Poi2014]Ant colony Time Limit: 30 Sec  Memory Limit: 128 MB Description   There is an entrance to the ant hill in every chamber with only one corridor leading into (or out of) it. At each entry, there are g groups of m1,m2,...,mg ants respecti…
[BZOJ3872][Poi2014]Ant colony Description 给定一棵有n个节点的树.在每个叶子节点,有g群蚂蚁要从外面进来,其中第i群有m[i]只蚂蚁.这些蚂蚁会相继进入树中,而且要保证每一时刻每个节点最多只有一群蚂蚁.这些蚂蚁会按以下方式前进: ·在即将离开某个度数为d+1的点时,该群蚂蚁有d个方向还没有走过,这群蚂蚁就会分裂成d群,每群数量都相等.如果d=0,那么蚂蚁会离开这棵树. ·如果蚂蚁不能等分,那么蚂蚁之间会互相吞噬,直到可以等分为止,即一群蚂蚁有m只,要分成…
codeforces 704B - Ant Man 贪心 题意:n个点,每个点有5个值,每次从一个点跳到另一个点,向左跳:abs(b.x-a.x)+a.ll+b.rr 向右跳:abs(b.x-a.x)+a.lr+b.rl,遍历完所有的点,问你最后的花费是多少 思路:每次选一个点的时候,在当前确定的每个点比较一下,选最短的距离. 为什么可以贪心?应为答案唯一,那么路径必定是唯一的,每个点所在的位置也一定是最短的. #include <bits/stdc++.h> using namespace…
题目链接:http://codeforces.com/contest/474/problem/F 题意简而言之就是问你区间l到r之间有多少个数能整除区间内除了这个数的其他的数,然后区间长度减去数的个数就是答案. 要是符合条件的话,那这个数的大小一定是等于gcd(a[l]...a[r]). 我们求区间gcd的话,既可以利用线段树性质区间递归下去然后返回求解,但是每次查询是log的,所以还可以用RMQ,查询就变成O(1)了. 然后求解区间内有多少个数的大小等于gcd的话,也是利用线段树的性质,区间递…
题目地址:http://codeforces.com/contest/474/problem/F 由题意可知,最后能够留下来的一定是区间最小gcd. 那就转化成了该区间内与区间最小gcd数相等的个数.区间最小gcd一定小于等于区间最小值.所以仅仅要先推断最小值是否是最小gcd.若是的话,就求出最小值的个数.然后用r-l+1-个数就可以. 对于以上信息.能够用线段树来维护.分别维护区间gcd,区间最小值以及区间最小值的个数. 代码例如以下: #include <iostream> #includ…
Description There is an entrance to the ant hill in every chamber with only one corridor leading into (or out of) it. At each entry, there are \(g\) groups of \(m_{1},m_{2},...,m_{g}\) ants respectively. These groups will enter the ant hill one after…
题目链接:http://codeforces.com/contest/474/problem/F 一个数组,每一次询问一个区间中有多少个数字可以整除其他所有区间内的数字. 能够整除其他所有数字的数一定是这些数字的gcd,所以可以用一个线段树来查询区间gcd. 接着需要统计区间内这个数字的出现个数,在读取数组时,顺便保存一个pair<int,int>键为数字,val为位置的数组,再sort一下,统计区间内某个数字出现次数,直接upper_bound-lower_bound即可. #include…
题面 既然我们只知道最后数量为$k$的蚂蚁会在特殊边上被吃掉,不妨逆着推回去,然后到达每个叶节点的时候就会有一个被吃掉的蚂蚁的区间,然后二分一下就好啦 #include<cstdio> #include<cstring> #include<algorithm> using namespace std; ,maxx=1e9; int ant[N],deg[N],leaf[N]; *N],goal[*N]; int n,g,k,t,root,t1,t2,n1,n2,cnt;…
题目链接:点击打开链接 题意: 给定n个节点的树 1为根 则此时叶子节点已经确定 最后一行给出叶子节点的顺序 目标: 遍历树并输出路径.要求遍历叶子节点时依照给定叶子节点的先后顺序訪问. 思路: 给每一个节点加一个优先级. 把最后一个叶子节点到父节点的路径上的点优先级改为1 把倒数第二个叶子节点到父节点的路径上的点优先级改为2 如此每一个点就有一个优先级,每一个訪问儿子节点时先訪问优先级大的就可以 对于无解的推断:得到的欧拉序列不满足输入的叶子节点顺序即是无解. #include <cstdio…
题目大意: 给定一棵$n(n\le10^6)$个结点的树.在每个叶子结点,有$g$群蚂蚁要从外面进来,其中第$i$群有$m_i$只蚂蚁.这些蚂蚁依次爬树(一群蚂蚁爬完后才会爬另一群),若当前经过结点度为$d+1$,蚂蚁数量为$m$,则接下来没走过的$d$个方向,每个方向爬$\lfloor\frac md\rfloor$只蚂蚁,剩下蚂蚁消失.在给定的一条边上有一只食蚁兽,若当前经过这条边的蚂蚁数量为$k$,则吃掉这些蚂蚁.问最后能吃到多少蚂蚁? 思路: 从给定边出发BFS,算出每条边会被吃掉的蚁群…
D. Ant on the Tree time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Connected undirected graph without cycles is called a tree. Trees is a class of graphs which is interesting not only for…
区间重复不会影响GCD,ST表当然是支持的啦,常数这么小. 学到了三个东西: 1.lower_bound返回的是大于等于的位置,要判是否不存在(end())和是否超出所求[x,y]范围. 2.ST表更新时存在一个i+(1<<j-1)的下标,存在极大越界隐患,以前数组开的大就无所谓,讲道理边界是要判的. 3.及时存代码,又蓝屏了qwq #include<iostream> #include<cstdio> #include<vector> #include&l…
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3872 从食蚁兽所在的边向叶节点推,会得到一个渐渐放大的取值区间,在叶子节点上二分有几群蚂蚁符合条件即可: 注意中途判断,如果已经超过范围就返回或者处理一下,据说会爆 long long 之类的: 而且食蚁兽所在的边的两个端点的初始值不一定是 k 和 k+1 !也要看度数! 注意统计答案的 num 也是 long long . 代码如下: #include<iostream> #inclu…
洛谷题目页面传送门 & CodeForces题目页面传送门 题意见洛谷里的翻译. 这题有\(\bm3\)种解法,但只有一种是正解(这不是废话嘛). 方法\(\bm1\):最近公共祖先LCA(正解) 真的把它当作一棵树来做.使用父亲表示法,记录每个节点的父亲.可是输入中只能告诉你谁和谁连,并没有说谁是谁的父亲,这该怎么办呢?其实很简单,只需要通过根是\(1\)这个信息,先把\(1\)的父亲设成自己,把所有\(1\)的邻居\(i\)的父亲都设成\(1\),然后再对\(i\)进行如下操作:把所有\(i…
题目描述: Ciel the Commander time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Now Fox Ciel becomes a commander of Tree Land. Tree Land, like its name said, has n cities connected by n - 1 undire…
题目链接:http://codeforces.com/problemset/problem/704/B -------------------------------------------------------------------------------- 比赛时最远也就猜到了拆公式,算贡献这一步 然后就$GG$了 结束后问了$Randolph87$ 他给了一个"括号匹配"的思路 感觉这个思路比官方题解更可做 然后我就按照这个思路开始思考 对于非起点非终点外的所有点 有这四种情…
正解:二分+$dp$ 解题报告: 传送门$QwQ$ 一年过去了依然没有头绪,,,$gql$的$NOIp$必将惨败了$kk$. 考虑倒推,因为知道知道除数和答案,所以可以推出被除数的范围,然后一路推到叶子节点就成$QwQ$ $over$ 嗷注意一个细节是有可能乘爆,所以每次和$m_max$取个$min$就成$QwQ$ #include<bits/stdc++.h> using namespace std; #define il inline #define gc getchar() #defin…
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3872 可以倒推出每个叶子节点可以接受的值域.然后每个叶子二分有多少个区间符合即可. 注意一开始的两个点不是直接是 l[ u ]=r[ u ]=lm !也要看度数的!且把那条边的两个端点分别算子树很方便. 而且过程中似乎会爆 long long ,所以如果 l[ i ] 都大于最大值就不往下算了:r[ i ]也要每次与最大值取min. 然后是和网上一份题解对拍出错却仍A了此题的代码.不想管是…
啊我把分子分母混了WA了好几次-- 就是从食蚁兽在的边段成两棵树,然后dp下去可取的蚂蚁数量区间,也就是每次转移是l[e[i].to]=l[u](d[u]-1),r[e[i].to]=(r[u]+1)(d[u]-1)-1,这里注意当l>maxm的时候就return,不然之后的没有贡献而且会爆long long 然后把每个dp到的叶子都二分一下看他的贡献区间里有几群蚂蚁加进答案里最后乘上k即可 #include<iostream> #include<cstdio> #inclu…
#include<iostream> #include<cstring> #include<cstdio> #include<algorithm> #include<cmath> using namespace std; ; int a[N],n,m,tmp; struct tree{ int l,r,gcd,sum; }tr[N*]; int gcd(int x,int y){ )return x; else return gcd(y,x%y)…
题目描述: The Brand New Function time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Polycarpus has a sequence, consisting of n non-negative integers: a1, a2, ..., *a**n*. Let's define function f(…
题目描述: Bus Number time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output This night wasn't easy on Vasya. His favorite team lost, and he didn't find himself victorious either - although he played p…
题目描述: Nick had received an awesome array of integers a=[a1,a2,…,an] as a gift for his 5 birthday from his mother. He was already going to explore its various properties but after unpacking he was disappointed a lot because the product a1⋅a2⋅…an of it…