https://www.luogu.org/problem/show?pid=2904 题目描述 Farmer John is herding his N cows (1 <= N <= 2,500) across the expanses of his farm when he finds himself blocked by a river. A single raft is available for transportation. FJ knows that he must ride…
题目 动规方程 f[i]=min(f[i],f[i−j]+sum) 我们默认为新加一头牛,自占一条船.想象一下,它不断招呼前面的牛,邀请它们坐自己这条船,当且仅当所需总时间更短时,前一头奶牛会接受邀请,最多邀请前面的所有奶牛一起坐这条船. #include<iostream> #include<cstring> #include<cstdio> using namespace std; ; int n,m,mt[maxn],f[maxn]; int main(){ sc…
P2904 [USACO08MAR]跨河River Crossing 显然的dp 设$f[i]$表示运走$i$头奶牛,木筏停在未过河奶牛一侧所用的最小代价 $s[i]$表示一次运$i$头奶牛到对面的代价 我们枚举上次运走了$j$头,显然$f[i]=min(f[i],f[i-j]+s[j]+s[0])$(注意自己要划回来) 最后不用划回来,减去一个$s[0]$即可 end. #include<iostream> #include<cstdio> #include<cstring…
题目描述 Farmer John is herding his N cows (1 <= N <= 2,500) across the expanses of his farm when he finds himself blocked by a river. A single raft is available for transportation. FJ knows that he must ride on the raft for all crossings and that that…
题目描述 Farmer John is herding his N cows (1 <= N <= 2,500) across the expanses of his farm when he finds himself blocked by a river. A single raft is available for transportation. FJ knows that he must ride on the raft for all crossings and that that…
题目描述 Farmer John is herding his N cows (1 <= N <= 2,500) across the expanses of his farm when he finds himself blocked by a river. A single raft is available for transportation. FJ knows that he must ride on the raft for all crossings and that that…
题目:洛谷2904 分析: 裸dp-- dp方程也不难想: \(dp[i]\)表示运\(i\)头牛需要的最短时间,\(sum[i]\)表示一次运\(i\)头牛(往返)所需的时间,则 \[dp[i]=min(dp[i],dp[j]+sum[i-j])(0<=j<i)\] 注意\(sum[0]\)是\(m*2\)而不是\(0\)(船上只有FJ也需要往返时间) 以及最后一次不需要返回,所以输出要减去\(m\) 代码: #include<cstdio> #include<algori…
传送门 f[i] 表示送前 i 头牛过去再回来的最短时间 f[i] = min(f[i], f[j] + sum[i - j] + m) (0 <= j < i) ——代码 #include <cstdio> #include <iostream> , INF = ; int n, m; int sum[MAXN], f[MAXN]; inline long long read() { , f = ; char ch = getchar(); ; ) + (x <…
洛谷题目传送门 用两种不一样的思路立体地理解斜率优化,你值得拥有. 题意分析 既然所有的土地都要买,那么我们可以考虑到,如果一块土地的宽和高(其实是蒟蒻把长方形立在了平面上)都比另一块要小,那么肯定是直接并购,这一块对答案没有任何贡献. 我们先把这些给去掉,具体做法可以是,按高为第一关键字,宽为第二关键字从大到小排序,然后上双指针扫一遍. 于是,剩下的就是一个高度递减.宽度递增的矩形序列.考虑怎样制定它们的并购方案会最优.显然如果要并购,一定要挑序列中的一段区间,这样贡献答案的就只有最左边矩形的…
洛谷题目传送门 通过瞪眼法发现,\(a_{i,j}=(i-1)\text{ xor }(j-1)+1\). 二维差分一下,我们只要能求\(\sum\limits_{i=0}^x\sum\limits_{j=0}^y[i\text{ xor }j\le k]\)就好了. 比较套路的数位DP. 从高位往低位做,设\(f[t][0/1][0/1][0/1]\)表示到第\(t\)位,\(i,j,i\text{ xor }j\)已确定的值是否卡到\(x,y,k\)前\(t\)位的上界的方案数和权值和. 每…