本文主要是介绍Making the Grade [kuangbin带你飞]刷题记录,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Making the Grade
题目链接
核心思想 :
暴力枚举版的dp
我们可以发现一个结论 : 只要a[i]
需要改变 , 那么它一定会等于它前面那个最终确定值
或者后面那个最终确定值
即就是这个 :只要a[i]
需要改变那么b[i]==b[i-1]或者a[i-1]或者b[i+1]或者a[i+1]
推到这里了我们就可以直接dp枚举出答案了
AC代码
#include<iostream>
#include<queue>
#include<string>
#include<string.h>
#include<algorithm>
#include<cstdio>
#include<map>
#include<set>
#include<stack>
#include<vector>
#include<cmath>
using namespace std;
typedef long long ll;
#define repi(x,y,z) for(int x = y; x<=z;++x)
#define deci(x,y,z) for(int x = y; x>=z;--x)
#define repl(x,y,z) for(ll x = y; x<=z;++x)
#define decl(x,y,z) for(ll x = y; x>=z;--x)
#define gcd(a, b) __gcd(a, b)
#define lcm(a, b) (a * b / gcd(a, b))
#define INF 0x3f3f3f3f
#define ms(a,b) memset( a, b , sizeof (a) )
#define txt intxt()
#define CAS int cas;cin>>cas;while(cas--)
#define py puts("YES")
#define pn puts("NO")
#define pcn putchar('\n')
inline void intxt( ){#ifndef ONLINE_JUDGEfreopen("in.txt","r",stdin);#endif
}
inline int read( ){int f = 1,x = 0;char ch = getchar();while (ch < '0' || ch > '9') {if (ch == '-') f = -1; ch = getchar();}while (ch >= '0' && ch <= '9') {x = x * 10 + ch - '0'; ch = getchar();}x *= f;return x;
}
const int maxn=2e3+5;int n;
ll a[maxn];
ll b[maxn];
ll dp[maxn][maxn];ll abb( ll aa ){//这里是手写的求绝对值if(aa>=0)return aa;return -aa;
}int main(){txt;n=read();repi(i,1,n){a[i]=read();b[i]=a[i];}sort( b+1,b+1+n );repi(i,1,n){repi(j,1,n){dp[i][j] = 1e18;}}ll ans=1e18;repi(i,1,n){repi(j,1,n){dp[i][j]=min( dp[i][j],dp[i-1][j]+abb( a[i]-b[j] ) );}repi(j,2,n){dp[i][j]=min( dp[i][j],dp[i][j-1] );//只要满足b[j]>b[j-1],就可以把dp[i][j]替换成dp[i][j-1]}}repi(i,1,n){ans=min(ans,dp[n][i]);}cout<<ans<<endl;return 0;
}
这篇关于Making the Grade [kuangbin带你飞]刷题记录的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!