线性动态规划
穷源溯流
路很长,尽管走便是。
展开
-
Codeforces Round #198 (Div. 2) D. Bubble Sort Graph(二分)
给出长度为 n 的排列,求如果 a[i]>a[i+1] 那么这两个数交换位置,并且 a[i] 与 a[i+1] 之间形成一条边,问任意两个数字之间没有边的最大数量是多少题目转化一下就是求最长上升子序列,由于 n 的范围很大,所以用 dp 的方法会超时,所以利用二分和一个栈求解const int N=2e5+5; int n,m; int i,j,k; int a[N]; int stk[N],top=1;int main(){ ...原创 2021-03-13 09:23:15 · 127 阅读 · 0 评论 -
洛谷 P2375 [NOI2014] 动物园(KMP+DP)
const ll mod=1e9+7;const int N=1e6+5; int n,m,t; int i,j,k; char s[N]; int f[N]; int dp[N];void getfail(char *s){ f[0]=f[1]=0; dp[0]=0,dp[1]=1; for(int i=1;s[i];i++){ int j=f[i]; while(j &&...原创 2021-02-07 22:38:03 · 475 阅读 · 0 评论 -
2020ICPC Intelligent Warehouse(线性动态规划)
其实看到这个题时能够想到 dp,当时想的是 dp[i] 以数 i 结尾的最大长度,其实想法没错,写了一个 O(n*n)的程序一开始的思路是,如果 dp[i] 是以 i 为结尾,那么(j<i 且 i %j ==0)的话,那么可以更新数组但是 n 太大,可以进行优化直至 O(n*sqrt(1e7)) ,换种思路想一下,以 i 为结尾,那么可以向后更新 dp[j],而不是向前更新O(n*n) 超时代码:int main(){ //IOS; while...原创 2020-10-27 22:11:24 · 311 阅读 · 0 评论 -
C. Mortal Kombat Tower(DP 走台阶问题)
有两个人玩游戏,a 先走,b 再走,每人最多走一步,最少走两步,给出数组 boss ,当 boss 的值为 1 时,a 需花费 1 金币才可以走,boss 为 0 时不用,b 随便走问最少花费多少金币const int N=2e5+5; int n,m,t; int i,j,k; int a[N]; int dp[N][2];int main(){ //IOS; rush(){ sd(n); ...原创 2020-09-15 07:26:50 · 733 阅读 · 0 评论 -
C. Two Arrays(DP)
由题意我们很容易想到要构造一个长度为 m*2 的序列满足单调递增或单调递减在这里我构造了一个单调递增的序列,即 a[1]a[2]……a[m]b[1]b[2]……b[m]制约一个序列的是他的起始位置和终止位置,我们可以确定终止位置及长度来代替进行动态规划dp[i][j] 为长度为 i ,并以 j 为结尾的序列有多少个则动态方程为 dp[i][j]=,这样复杂度接近O(N^2*m) for(i=2;i<=m;i++){ for(j=1;...原创 2020-07-07 08:09:31 · 306 阅读 · 0 评论 -
K for the Price of One (Hard Version)(dp/贪心)
有 n件物品,每一件的价格为ai,你有m枚钱,你可以选择买一件,或者买k件并支付最大价值的那件的价格显然dp[i]是由前一件或前k件状态转移过来dp[i]=min(dp[i-1]+a[i],dp[i-k]+a[i]);dp[i]表示到第i件货物的最小花费(由于排序之后,表示前 i 件货物最小花费)#include <iostream>#include <...原创 2020-03-09 21:28:17 · 940 阅读 · 0 评论 -
Making the Grade
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13727 Accepted: 6247 Description A straight dirt road connects two fields on FJ's farm, but it ...原创 2020-04-08 21:46:06 · 305 阅读 · 0 评论 -
How many ways(DP&&深搜)
这是一个简单的生存游戏,你控制一个机器人从一个棋盘的起始点(1,1)走到棋盘的终点(n,m)。游戏的规则描述如下:1.机器人一开始在棋盘的起始点并有起始点所标有的能量。2.机器人只能向右或者向下走,并且每走一步消耗一单位能量。3.机器人不能在原地停留。4.当机器人选择了一条可行路径后,当他走到这条路径的终点时,他将只有终点所标记的能量。如上图,机器人一开始在(1,1)点,并...原创 2020-04-04 18:06:20 · 183 阅读 · 0 评论 -
Milking Time
Bessie is such a hard-working cow. In fact, she is so focused on maximizing her productivity that she decides to schedule her next N (1 ≤ N ≤ 1,000,000) hours (conveniently labeled 0..N-1) so that ...原创 2020-04-03 21:34:00 · 250 阅读 · 0 评论 -
Functions again
Something happened in Uzhlyandia again... There are riots on the streets... Famous Uzhlyandian superheroes Shean the Sheep and Stas the Giraffe were called in order to save the situation. Upon the arr...原创 2020-04-02 23:34:32 · 219 阅读 · 0 评论 -
Dynamic Programming
Dynamic Programming, short for DP, is the favorite of iSea. It is a method for solving complex problems by breaking them down into simpler sub-problems. It is applicable to problems exhibiting the pro...原创 2020-04-02 22:38:19 · 261 阅读 · 0 评论 -
最大两字段和
Sample Input1101 -1 2 2 3 -3 4 -4 5 -5Sample Output13#include <iostream>#include <cstdio>#include <vector>#include <algorithm>#include <cstring>#inc...原创 2020-03-28 20:51:45 · 187 阅读 · 0 评论