![](https://img-blog.csdnimg.cn/20201014180756925.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
dp
【杰杰】
这个作者很懒,什么都没留下…
展开
-
P1386 座位安排
传送门题意:有n个位置,m个人已经被安排好了座位的编号,现在问你剩下的人安排座位编号的方式有多少种。思路:计数dp,先求出对于当前位置i来说可安排编号的最大人数bib_ibi,设dp[i][j]表示编号安排在i位置到n位置的人数,则对于dp[i][j],我们枚举有k个人的编号被安排在i位置,则dp[i][j]+=dp[i+1][j-k]+cbikc_{b_i}^kcbik。#include<bits/stdc++.h>using namespace std;#define原创 2022-03-26 17:17:19 · 557 阅读 · 0 评论 -
P1005 [NOIP2007 提高组] 矩阵取数游戏
传送门题意:给你一个n*m的矩阵,在每一行矩阵的两头单独取数,问最终获得的数的总和的最大值。思路:对于每行单独区间dp,设dp[st][ed]表示st到ed区间的最大值,因为取数只能从两端取,那么dp[st][ed]可以由dp[st][ed-1]*2+a[i][ed]*2和dp[st+1][ed]*2+a[i][st]*2转移得来。#include<bits/stdc++.h>using namespace std;#define ll long long#define end原创 2022-03-19 18:39:27 · 4186 阅读 · 0 评论 -
D. MEX Sequences
传送门题意:给你一个序列,该序列的满足题目所给条件的任意子序列数目是多少,答案对998244353取模。思路:当前的数x只能加入到mex的值为x-1,x,x+1的序列中,因而我们记dp[x][1]为mex的值等于x-1的序列的数目,dp[x][0]为mex的值等于x的值的数目。由此:假设当前要加入的数为x,那么mex为x+1的序列数dp[x+1][0]=dp[x+1][0]*2。而mex为x的序列数dp[x+1][1]=dp[x+1][1]*2+dp[x-1][0]还要注意x对x+1的me原创 2022-02-22 17:34:03 · 271 阅读 · 0 评论 -
P1052 [NOIP2005 提高组] 过河
传送门题意:青蛙想从0处到达L处,路上有m块石子,问最少踩多少块石子可以过河(可以踩石子之外的地方)。思路:暴力dp比较好想,这样做是O(l),l最大1e9,肯定超时。但这题石头少,可以考虑离散化。离散化方案可以抽象成这样一个问题:有两个数i,i+1,从x开始,每次对x加i或者加(i+1),可以得到哪些数,然后会发现x+i*(i+1)之后的所有数都可以得到,于是我们将两点距离大于i*(i+1)的石子都减去i*(i+1),在这题上面,我们考虑st的做法,若两点距离大于st,则一直减小t倍距离直到两原创 2022-01-06 11:50:34 · 626 阅读 · 0 评论 -
J. Xingqiu‘s Joke
传送门题意:给你两个数a,b,每次可对两个数同时进行三种操作:加1,减1或者同时除以他们的公共质因数,问使得其中任意一个数到达1的最小操作 次数。思路:不妨设a>b,f(a,d)表示a,b-a这组数转化为1的最小次数,d为b-a的值,设k为b-a的质因数,那么f(a,b)是可以由状态f(a/k,b/k)和f(a/k+1,b/k)转化而来,于是,通过dfs的方式求解。#include<bits/stdc++.h>using namespace std;#define ll l原创 2021-12-25 22:32:26 · 832 阅读 · 0 评论 -
D. Fox And Jumping
传送门题意:给你n张卡片和初始坐标0,每张卡片都有一个lil_ili,每一张卡片可以跳到当前坐标加减lil_ili的位置同时每张卡片有对应的花费,每张卡片选取后可以任意次使用,问你想要跳到任意一个格子的最小花费。思路:首先解决一个问题:选取的卡片要满足何种条件才能够走到所有格子。这个问题不难解决,假设选取的卡片能跳的距离为a1,a2...aka_1,a_2...a_ka1,a2...ak,当前位置为pospospos那么选取的这k个卡片必须要能跳到pos+1或pos-1的位置,这样才能使原创 2021-12-18 14:01:21 · 243 阅读 · 0 评论 -
P1004 [NOIP2000 提高组] 方格取数
传送门题意:给你一个n∗nn*nn∗n的矩阵,问你先后两次从左上角走到右下角,途中获取对应值,最多获得的值是多少。思路:类似于只走一次从左上角走到右下角的题,这题只需要枚举两个点在不同位置的最大值进行递推即可。用dp[i][j][k][l]dp[i][j][k][l]dp[i][j][k][l]表示走到(i,j)和(k,l)(i,j)和(k,l)(i,j)和(k,l)时的最大值。#include<bits/stdc++.h>#define ll long longusing na原创 2021-10-14 22:57:07 · 150 阅读 · 0 评论 -
P1359 租用游艇
传送门思路:题目是dp,但我感觉更像最短路。。。#include<bits/stdc++.h>using namespace std;#define ll long longll g[210][210];int main(){ int n; cin>>n; for(int i = 1; i < n; i++) { for(int j = i+1; j <= n; j++) { cin>>g[i][j]; } }原创 2021-05-13 19:50:33 · 77 阅读 · 0 评论 -
P1832 A+B Problem(再升级)
传送门思路:dp[i]表示1到i有多少种方案;从1到n遍历,选择当前遍历的数是否可选(是否为素数);#include<bits/stdc++.h>using namespace std;#define ll long longint vis[1010];int ans[1010];int cnt = 1;void prime(int n){ for(int i = 2; i <= n; i++) { if(!vis[i]) { ans[cnt++]原创 2021-05-12 22:13:45 · 96 阅读 · 0 评论 -
P1802 5倍经验日
传送门思路:01背包,dp(x)表示吃x的药所能得到的最大经验。#include<bits/stdc++.h>using namespace std;#define ll long longconst ll inf = 0x3f3f3f3f;ll lo[1010];ll wi[1010];ll num[1010];ll dp[1010];int main(){ ll n,x; cin>>n>>x; for(ll i = 1; i <原创 2021-05-11 20:09:25 · 64 阅读 · 0 评论 -
P1049 [NOIP2001 普及组] 装箱问题
传送门思路:转化为01背包问题。#include<bits/stdc++.h>using namespace std;#define ll long longconst int inf = 0x3f3f3f3f;int a[50];int dp[20010];int main(){ int v; cin>>v; int n; cin>>n; for(int i = 1; i <= n; i++) { scanf("%d",&a原创 2021-05-11 19:40:46 · 217 阅读 · 0 评论 -
不要62
传送门#include <bits/stdc++.h>using namespace std;#define ll long longint dp[1000010];int num[20];int main(){ int sum = 0; int n,m; for(int i = 1; i <= 1000000; i++) { int k = i; while(k > 0) { if(k%10==4||k%100==62) {原创 2021-05-07 20:17:45 · 45 阅读 · 0 评论 -
Educational Codeforces Round 108 (Rated for Div. 2) D. Maximum Sum of Products
传送门You are given two integer arrays a and b of length n.You can reverse at most one subarray (continuous subsegment) of the array a.Your task is to reverse such a subarray that the sum ∑i=1nai⋅bi is maximized.InputThe first line contains one integer n原创 2021-04-30 23:53:44 · 53 阅读 · 1 评论 -
NASA的食物计划
传送门思路:dp(a,b)表示v体积m质量食物所能有的最大卡路里。#include<bits/stdc++.h>using namespace std;int vi[410];int mi[410];int w[410];int dp[410][410];int main(){ int v,m; cin>>v>>m; int n; cin>>n; for(int i = 1; i <= n; i++) { cin&g原创 2021-04-28 23:37:06 · 100 阅读 · 0 评论 -
选学霸
传送门思路:需要先用并查集找出每一个需要选择的不同的学霸堆,然后就是01背包的板子题。#include<bits/stdc++.h>using namespace std;const int mod = 1000007;int f[40010];int ans[40010];int vis[40010];int dp[40010];int find(int x){ if(f[x] != x) return f[x] = find(f[x]); return x;原创 2021-04-26 23:45:02 · 104 阅读 · 0 评论 -
Codeforces Round #715 (Div. 2) C-The Sports Festival
传送门The student council is preparing for the relay race at the sports festival.The council consists of n members. They will run one after the other in the race, the speed of member i is si. The discrepancy di of the i-th stage is the difference between th原创 2021-04-17 21:58:31 · 164 阅读 · 0 评论 -
合唱队形
传送门思路:从左到右求出1到i的最长上升子序列的长度,再求出1到n的最长下降子序列,最后枚举要求中的最大值。#include<bits/stdc++.h>#define ll long longusing namespace std;int a[110];int dp[110];int dp2[110];int dp3[110];int main(){ int n; cin>>n; for(int i = 1; i <= n; i++) {原创 2021-04-15 21:10:58 · 72 阅读 · 0 评论 -
股票买卖 II
传送门给定一个长度为 N 的数组,数组中的第 i 个数字表示一个给定股票在第 i 天的价格。设计一个算法来计算你所能获取的最大利润。你可以尽可能地完成更多的交易(多次买卖一支股票)。注意:你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。输入格式第一行包含整数 N,表示数组长度。第二行包含 N 个不大于 10000 的正整数,表示完整的数组。输出格式输出一个整数,表示最大利润。数据范围1≤N≤1e5思路:当前状态分为两种,买或不买,若不买,则根据上次情况:上次买,则这次原创 2021-04-15 20:29:43 · 69 阅读 · 0 评论 -
Array Shrinking
传送门You are given an array a1,a2,…,an. You can perform the following operation any number of times:Choose a pair of two neighboring equal elements ai=ai+1 (if there is at least one such pair).Replace them by one element with value ai+1.After each such o原创 2021-04-14 21:54:04 · 181 阅读 · 0 评论 -
P3146 [USACO16OPEN]248 G
思路:枚举区间长度,起点,分割点。#include<bits/stdc++.h>using namespace std;int a[300];int dp[300][300];int main(){ int n; cin>>n; for(int i = 1; i <= n; i++) { cin>>a[i]; dp[i][i] = a[i]; } int ans = 0; for(int len = 1; len <= .原创 2021-04-13 20:52:47 · 46 阅读 · 0 评论 -
Boredom
Alex doesn’t like boredom. That’s why whenever he gets bored, he comes up with games. One long winter evening he came up with a game and decided to play it.Given a sequence a consisting of n integers. The player can make several steps. In a single step he原创 2021-04-13 00:27:06 · 105 阅读 · 0 评论 -
石子合并
传送门将 n 堆石子绕圆形操场排放,现要将石子有序地合并成一堆。规定每次只能选相邻的两堆合并成新的一堆,并将新的一堆的石子数记做该次合并的得分。请编写一个程序,读入堆数 n 及每堆的石子数,并进行如下计算:选择一种合并石子的方案,使得做 n−1 次合并得分总和最大。选择一种合并石子的方案,使得做 n−1 次合并得分总和最小。输入格式输入第一行一个整数 n,表示有 n 堆石子。第二行 n 个整数,表示每堆石子的数量。输出格式输出共两行:第一行为合并得分总和最小值,第二行为合并得分总和最大原创 2021-04-12 23:36:23 · 462 阅读 · 0 评论 -
子序列个数
传送门子序列的定义:对于一个序列a=a1,a2,…an。则非空序列a’=ap1,ap2…apm为a的一个子序列,其中1<=p1<p2<…<pm<=n。例如4,14,2,3和14,1,2,3都为4,13,14,1,2,3的子序列。对于给出序列a,有些子序列可能是相同的,这里只算做1个,请输出a的不同子序列的数量。由于答案比较大,输出Mod 10^9 + 7的结果即可。Input第1行:一个数N,表示序列的长度(1 <= N <= 100000) 第2 - N原创 2021-04-11 21:35:39 · 273 阅读 · 0 评论 -
Basketball Exercise
传送门Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. 2⋅n students have come to Demid’s exercise session, and he lined up them into two rows of the same size (there are exactly n people in each原创 2021-04-10 17:24:57 · 169 阅读 · 0 评论 -
编辑距离
传送门编辑距离,又称Levenshtein距离(也叫做Edit Distance),是指两个字串之间,由一个转成另一个所需的最少编辑操作次数。许可的编辑操作包括将一个字符替换成另一个字符,插入一个字符,删除一个字符。例如将kitten一字转成sitting:sitten (k->s)sittin (e->i)sitting (->g)所以kitten和sitting的编辑距离是3。俄罗斯科学家Vladimir Levenshtein在1965年提出这个概念。给出两个字符串a,原创 2021-04-10 17:18:51 · 190 阅读 · 0 评论 -
最长公共子序列Lcs
传送门给出两个字符串A B,求A与B的最长公共子序列(子序列不要求是连续的)。比如两个串为:abcicbaabdkscabab是两个串的子序列,abc也是,abca也是,其中abca是这两个字符串最长的子序列。Input第1行:字符串A 第2行:字符串B (A,B的长度 <= 1000)Output输出最长的子序列,如果有多个,随意输出1个。思路:设 dp[i][j] 为a串前i个与b串前j个串里的最长公共子序列。以O(mn)遍历a,b串,若a[i] == b[j],则dp[i原创 2021-04-09 21:22:45 · 116 阅读 · 0 评论 -
最长递增子序列
传送门给出长度为N的数组,找出这个数组的最长递增子序列。(递增子序列是指,子序列的元素是递增的)例如:5 1 6 8 2 4 5 10,最长递增子序列是1 2 4 5 10。Input第1行:1个数N,N为序列的长度(2 <= N <= 50000) 第2 - N + 1行:每行1个数,对应序列的元素(-10^9 <= Si <= 10^9)Output输出最长递增子序列的长度。思路:以O(n)来遍历序列,dp[i]储存当前最优子序列,每当遇到a[j] > dp原创 2021-04-09 19:09:24 · 83 阅读 · 0 评论 -
最大连续子段和
传送门给定有n个整数(可能为负整数)组成的序列a1,a2,…,an,求该序列连续的子段和的最大值。如果该序列的所有元素都是负整数时定义其最大子段和为0。例如,当(a1,a2,a3,a4,a5)=(-5,11,-4,13,-4-2)时,最大子段和为11+(-4)+13=20。Input第一行整数个数N 第二行为N个整数,每个整数之间用一空格隔开。 1<=N<=100000 N个整数的范围为-1000到1000Output一行一个数,表示最大连续子段和的值思路:判断当前所选的a[i]原创 2021-04-08 19:07:50 · 1094 阅读 · 0 评论 -
序列拆分
传送门题目描述给出一个含有nn个数的整数序列,把它拆分成两个序列,使分成的两个序列之和的差值最小输入格式第一行一个正整数n,第二行含n个正整数的序列a[n]。输出格式一个正整数表示分成的两个子序列之和的最小差值思路:先得出序列的总值sum,然后用01背包解法找到小于等于sum/2的子序列。#include<bits/stdc++.h>using namespace std;#define ll long longint a[110];int dp[1100000];原创 2021-04-07 22:53:41 · 819 阅读 · 0 评论 -
FATE
传送门最近xhd正在玩一款叫做FATE的游戏,为了得到极品装备,xhd在不停的杀怪做任务。久而久之xhd开始对杀怪产生的厌恶感,但又不得不通过杀怪来升完这最后一级。现在的问题是,xhd升掉最后一级还需n的经验值,xhd还留有m的忍耐度,每杀一个怪xhd会得到相应的经验,并减掉相应的忍耐度。当忍耐度降到0或者0以下时,xhd就不会玩这游戏。xhd还说了他最多只杀s只怪。请问他能升掉这最后一级吗?Input输入数据有多组,对于每组数据第一行输入n,m,k,s(0 < n,m,k,s < 100原创 2021-04-06 21:13:34 · 168 阅读 · 0 评论 -
Robberies
传送门The aspiring Roy the Robber has seen a lot of American movies, and knows that the bad guys usually gets caught in the end, often because they become too greedy. He has decided to work in the lucrative business of bank robbery only for a short while, be原创 2021-04-03 19:47:50 · 127 阅读 · 0 评论 -
P1060 [NOIP2006 普及组] 开心的金明
传送门#include<bits/stdc++.h>using namespace std;#define ll long longint v[30];int p[30];int dp[30010];int main(){ int n,m; cin>>n>>m; for(int i = 1; i <= m; i++) cin>>v[i]>>p[i]; for(int i = 1; i <= m; i++)原创 2021-04-02 19:26:28 · 163 阅读 · 0 评论 -
Buns
传送门Lavrenty, a baker, is going to make several buns with stuffings and sell them.Lavrenty has n grams of dough as well as m different stuffing types. The stuffing types are numerated from 1 to m. Lavrenty knows that he has ai grams left of the i-th stuff原创 2021-04-02 11:58:32 · 269 阅读 · 0 评论 -
Bone Collector--2021SWUST新生训练--背包
Many years ago , in Teddy’s hometown there was a man who was called “Bone Collector”. This man like to collect varies of bones , such as dog’s , cow’s , also he went to the grave …The bone collector had a big bag with a volume of V ,and along his trip of原创 2021-03-30 22:17:05 · 116 阅读 · 0 评论