Making the Grade [kuangbin带你飞]刷题记录

14 篇文章 0 订阅
3 篇文章 0 订阅

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_JUDGE
    freopen("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;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值