#include<iostream>#include<cstring>#include<vector>#include<stdio.h>#include<queue>#include<math.h>#include<stack>#include<math.h>#include<algorithm>#include<map>#include<set>#include<iostream>usingnamespace std;#define MAX 0x3f3f3f3ftypedeflonglong ll;int n,m,k;int dp[100001];//表示第i块石头最少的消耗int a[100001];intmain(){
cin>>n;for(int i =0; i < n ; i++)
cin>>a[i];
dp[0]=0;
dp[1]=abs(a[1]- a[0]);for(int i =2; i < n ; i++)
dp[i]=min(dp[i-1]+abs(a[i]- a[i-1]), dp[i-2]+abs(a[i]- a[i-2]));
cout<<dp[n-1];}
B - Frog 2
#include<iostream>#include<cstring>#include<vector>#include<stdio.h>#include<queue>#include<math.h>#include<stack>#include<math.h>#include<algorithm>#include<map>#include<set>#include<iostream>usingnamespace std;#define MAX 0x3f3f3f3ftypedeflonglong ll;int n,m,k;int dp[100001];//表示第i块石头最少的消耗int a[100001];intmain(){
cin>>n>>k;for(int i =0; i < n ; i++)
cin>>a[i];
dp[0]=0;
dp[1]=abs(a[1]- a[0]);for(int i =2; i < n ; i++){int j;if(i >= k)
j = i - k;else
j =0;int mmin = MAX;for(;j < i ; j++)
mmin =min(mmin, dp[j]+abs(a[i]- a[j]));
dp[i]= mmin;}
cout<<dp[n-1];}
C - Vacation
#include<iostream>#include<cstring>#include<vector>#include<stdio.h>#include<queue>#include<math.h>#include<stack>#include<math.h>#include<algorithm>#include<map>#include<set>#include<iostream>usingnamespace std;#define MAX 0x3f3f3f3ftypedeflonglong ll;int n,m,k;struct Node{int a,b,c;};
Node node[100005];int dp[100005][5];//dp[i][j]表示第i天选第j个intmain(){
cin>>n;for(int i =0; i < n ; i++)scanf("%d%d%d",&node[i].a,&node[i].b,&node[i].c);
dp[0][0]= node[0].a;
dp[0][1]= node[0].b;
dp[0][2]= node[0].c;for(int i =1; i < n ; i++){
dp[i][0]+=max(dp[i-1][1],dp[i-1][2])+ node[i].a;
dp[i][1]+=max(dp[i-1][0],dp[i-1][2])+ node[i].b;
dp[i][2]+=max(dp[i-1][0],dp[i-1][1])+ node[i].c;}
cout<<max(max(dp[n-1][0],dp[n-1][1]),dp[n-1][2]);return0;}
D - Knapsack 1(0-1背包)
#include<iostream>#include<cstring>#include<vector>#include<stdio.h>#include<queue>#include<math.h>#include<stack>#include<math.h>#include<algorithm>#include<map>#include<set>#include<iostream>usingnamespace std;#define MAX 0x3f3f3f3ftypedeflonglong ll;int n,m,k;
ll dp[100001];
ll w[100001];
ll v[100001];intmain(){
cin>>n>>k;for(int i =0; i < n ; i++)
cin>>w[i]>>v[i];for(int i =0; i <n ; i ++)for(int j = k ; j>= w[i]; j--)
dp[j]=max(dp[j],dp[j-w[i]]+ v[i]);
cout<<dp[k];}