题目描述 A little known fact about Bessie and friends is that they love stair climbing races. A better known fact is that cows really don't like going down stairs. So after the cows finish racing to the top of their favorite skyscraper, they had a proble…
P3052 [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper 题目描述 A little known fact about Bessie and friends is that they love stair climbing races. A better known fact is that cows really don't like going down stairs. So after the cows finish racing to the top…
题目描述 给出n个物品,体积为w[i],现把其分成若干组,要求每组总体积<=W,问最小分组.(n<=18) 输入格式: Line 1: N and W separated by a space. Lines 2..1+N: Line i+1 contains the integer C_i, giving the weight of one of the cows. 输出格式: Line 1: A single integer, R, indicating the minimum number…
题目描述 A little known fact about Bessie and friends is that they love stair climbing races. A better known fact is that cows really don't like going down stairs. So after the cows finish racing to the top of their favorite skyscraper, they had a proble…
这个状压dp其实很明显,n < 18写在前面了当然是状压.状态其实也很好想,但是有点问题,就是如何判断空间是否够大. 再单开一个g数组,存剩余空间就行了. 题干: 题目描述 A little known fact about Bessie and friends is that they love stair climbing races. A better known fact is that cows really don't like going down stairs. So after…
洛谷题目链接:[USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper 题目描述 A little known fact about Bessie and friends is that they love stair climbing races. A better known fact is that cows really don't like going down stairs. So after the cows finish racing to the top…
题目描述 A little known fact about Bessie and friends is that they love stair climbing races. A better known fact is that cows really don't like going down stairs. So after the cows finish racing to the top of their favorite skyscraper, they had a proble…
题目链接 状压\(dp\) 根据套路,先设\(f[sta]\)为状态为\(sta\)时所用的最小分组数. 可以发现,这个状态不好转移,无法判断是否可以装下新的一个物品.于是再设一个状态\(g[sta]\)表示状态为\(sta\)时 每组剩下的体积的最大值 的最大值,当枚举状态为\(sta\),枚举到第\(i\)个时,可以得到\(g\)的转移: \[ g[v]=max(g[v],g[sta]-w[i]) \] 其中,\(v​\)为转移后的状态. 然后每次就可以根据\(g\)来转移\(f\)了. #…
传送门 输出被阉割了. 只输出最少分的组数即可. f 数组为结构体 f[S].cnt 表示集合 S 最少的分组数 f[S].v 表示集合 S 最少分组数下当前组所用的最少容量 f[S] = min(f[S], f[S - i] + a[i]) (i ∈ S) 运算重载一下即可. ——代码 #include <cstdio> #include <iostream> int n, m, w; ]; struct qwq { int cnt, v; qwq(, ) : cnt(cnt),…
不打算把题目放着,给个空间传送门,读者们自己去看,传送门(点我)    . 这题是自己做的第一道状态压缩的动态规划. 思路: 在这题中,我们设f[i]为i在二进制下表示的那些牛所用的最小电梯数. 设g[i]为i在二进制下表示的那些牛使用的电梯中剩下的最大容量. 所以很明显的,我们只要枚举每一只牛就可以了. 如果当前状态下,最大容量能装进某只牛,则装进去,并且用两个变量保存装进去后的f值与g值,否则再使用一个新的电梯,并且用变量保存用新电梯后的f与g值. 在每次的枚举,我们还要将当前保存的f值与g…
状压DP: #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #define R(a,b,c) for(register int a = (b); a <= (c); ++ a) #define nR(a,b,c) for(register int a = (b); a >= (c); -- a)…
题目传送门 摩天大楼里的奶牛 题目描述 A little known fact about Bessie and friends is that they love stair climbing races. A better known fact is that cows really don't like going down stairs. So after the cows finish racing to the top of their favorite skyscraper, th…
迭代加深搜索基础 题目描述 A little known fact about Bessie and friends is that they love stair climbing races. A better known fact is that cows really don't like going down stairs. So after the cows finish racing to the top of their favorite skyscraper, they had…
(已经一句话了) 第一反应:暴力 第二反应:朴素算法过不去 第三反应:没法折半暴搜(没法统计答案) 所以,歪歪了一个类似贪心刷表的方法,过了这道题. 首先,如果爆搜的话会有几个状态: 当前牛 当前几个箱子 当前的牛数量 而且它的复杂度是阶乘级别. 发现这道题目有显然单调性(答案处在分界线,-1不合法,+1不是最优)所以歪歪了一个类似二分check的dfs方法. 那么状态就得改变了.传入的还是牛的编号,但是,在dfs内部,枚举的是当前的牛放在哪个箱子里.如果能搜到最后一步,就返回. 于是乎,这样d…
题意 给出n个物品,体积为w[i],现把其分成若干组,要求每组总体积<=W,问最小分组.(n<=18) 题解 一看以为是弱智题.(可能真的是,我太菜了) 然后跟walthou夸下海口:这么简单我做出来给你讲. 结果就被打脸了(对waithou说:我不会,自己看题解吧) 然后我就看了题解.. 设dp[i][j]为当前选i组已经选的情况为j的第i组的最小重量. 然后转移时,一个一个奶牛转移. 具体就是对于枚举的状态,如果dp[i][j]有不为INF,就枚举一个不属于j的x. 方程是 dp[i][j…
一道状压题,但今天闲来无事又用遗传乱搞了一下. 设了一个DNA数组,DNA[i]记录第i个物品放在哪个组里.适应度是n-这个生物的组数+1. 交配选用的是轮盘赌和单亲繁殖——0.3的几率单点变异.(事实上有性生殖我似乎写不出来……代码量略大) 种群大小开到了400,在vijos上繁殖了2050代,下数据自己测也是对的. 然而只有84分 这究竟是为什么啊    下数据自己测是没错的啊……………… 疯了 代码和数据先放到这里,以后再改吧 #include<cstdio> #include<c…
参见ZHT467的题解. f[i]表示在i这个集合下的最少分组数和当前组最少的容量. 从1到(1<<n)-1枚举i,对于每个i枚举它的子奶牛,然后重载运算符计算. 代码如下 #include<iostream> #include<cstdio> #include<cstring> #include<cctype> #include<cmath> #include<algorithm> using namespace std…
Luogu P1290 欧几里得的游戏/UVA10368 Euclid's Game 对于博弈论的题目没接触过多少,而这道又是比较经典的SG博弈,所以就只能自己来推关系-- 假设我们有两个数$m,n$,我们先把$m$设为较大值,$n$设为较小值.现在我们把它分成三种情况: 1.若两数为倍数关系,当前操作的一方赢. 2.若$m \div n>1$,那么还是操作一方赢. 为什么呢? 拿$(25,7)$来举例.这时的操作方就有三种选择:$(8,7)$,$(11,7)$,$(4,7)$, 如果他选$(1…
  2621: [Usaco2012 Mar]Cows in a Skyscraper Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 303  Solved: 150[Submit][Status][Discuss] Description [Mark Gordon, Neal Wu, Fatih Gelgi, 2012] A little known fact about Bessie and friends is that they love…
原题传送门:P1204 [USACO1.2]挤牛奶Milking Cows 实际是道很弱智的题目qaq 但窝还是觉得用珂朵莉树写会++rp(窝都初二了,还要考pj) 前置芝士:珂朵莉树 窝博客里对珂朵莉树的介绍 没什么好说的自己看看吧 每个农夫就assign_val一下,但要注意一下细节qaq 窝不会告诉你因这个错调了我十几分钟qaq 应该写assign_val(l,r-1,1),查询时应写query(lmin,rmax,1/0) 其他没什么好说的了,细节详见程序 #pragma GCC opt…
题目描述 Farmer John has noticed that his cows often move between nearby fields. Taking this into account, he wants to plant enough grass in each of his fields not only for the cows situated initially in that field, but also for cows visiting from nearby…
https://www.luogu.org/problemnew/show/P3611 二分答案+优先队列 二分O(logn) 判一次正确性O(nlogn) 总体O(nlognlogn) 为了让priority_queue变成小根堆,就把元素全部取相反数了. #include<iostream> #include<cstdio> #include<queue> using namespace std; inline int rd(){ ,f=;char c; :; +c…
$k$ 十分小,直接暴力维护 $1$~$k$ 的答案即可. 然后需要用父亲转移到儿子的方式转移一下. Code: #include <bits/stdc++.h> #define M 23 #define N 100005 #define setIO(s) freopen(s".in","r",stdin) using namespace std; int n,K,edges; int f[N][M],hd[N],to[N<<1],nex[N…
刷水~ Code: #include<bits/stdc++.h> using namespace std; #define setIO(s) freopen(s".in","r",stdin) #define maxn 300000 #define inf 1000000 deque<int>p,q; struct Node { int x,y; }nd[maxn]; bool cmp(Node a, Node b) { return a.…
传送门 解题思路 树形dp,看到数据范围应该能想到是O(nk)级别的算法,进而就可以设出dp状态,dp[x][j]表示以x为根的子树,距离它为i的点的总和,第一遍dp首先自底向上,dp出每个节点的子树中到他距离为j的,转移方程dp[x][j]=dp[u][j-1] ,第二遍dp自顶向下,dp出每个节点父亲那头的距离为j的,转移方程dp[u][j]+=dp[x][j-1]-dp[u][j-2] 代码 //dp[x][j] 以i为根的子树,距离为j的牛的和 //dp[x][j]+=dp[u][j-1…
题目链接 题目大意:给定一个集合\(S\),给一个限制条件\(P\),要求划分集合,使得每一个子集\(A\in S\),\(A\)满足限制条件\(P\),且划分总数最小. 注意到数据范围\(n<=18\). 第一感状压. 搜索不想写,于是\(dp\). 原本设计的状态是\(f[i][j]\)表示当前状态为\(i\),枚举到第\(j\)件物品的最大容量,\(g[i][j]\)表示状态为\(i\),枚举到\(j\)的最小划分数.然而太复杂了,没有必要,写崩了搞了\(16pts\). 换一种思路.设计…
第一眼是3^n*n的做法...然而并不可行T T 后来发现对于奶牛的一个状态i,最优情况下剩下那个可以装奶牛的电梯剩下的可用重量是一定的,于是我们设f[i]表示奶牛状态为i的最小电梯数,g[i]为奶牛状态为i剩下那个电梯的可用重量,每次都枚举一个奶牛看看能不能塞进电梯就好,塞不进就新开一个电梯. #include<iostream> #include<cstring> #include<cstdlib> #include<cstdio> #include&l…
首先比较容易想到是状态压缩DP 令$f[S]$表示选取了集合$S$以后,已经送了最少次数$cnt$且当前电梯剩下的体积$rest$最大(即$f[S]$是一个二元组$(cnt, rest)$) 于是$f[S] = min_{i \in S} f[S - {i}] + v[i]$ 重载的$<$和$+$运算详情就请看程序好了,反正就是一个贪心思想,总复杂度$O(n * 2 ^ {n - 1})$ /****************************************************…
本博客主要记录一些在刷题的途中遇到的一些巧妙的题目 砝码称重 一开始想到可以DP递推标记能凑成的数量 但发现同一种砝码可能有多个于是想多开一维状态存当前还剩多少砝码 真是愚蠢至极 直接把所有砝码单独看待不就行了么... #include<iostream> #include<cstdio> #include<cmath> using namespace std; int a[10010],x,num,b[10]={0,1,2,3,5,10,20},ans; bool t…
Description 了解奶牛们的人都知道,奶牛喜欢成群结队.观察约翰的N(1≤N≤100000)只奶牛,你会发现她们已经结成了几个“群”.每只奶牛在吃草的 时候有一个独一无二的位置坐标Xi,Yi(l≤Xi,Yi≤[1..10^9]:Xi,Yi∈整数.当满足下列两个条件之一,两只奶牛i和j是属于同一个 群的:   1.两只奶牛的曼哈顿距离不超过C(1≤C≤10^9),即lXi - xil+IYi - Yil≤C.   2.两只奶牛有共同的邻居.即,存在一只奶牛k,使i与k,j与k均同属一个群.…