zoukankan      html  css  js  c++  java
  • poj 3666 Making the Grade

    题目链接http://poj.org/problem?id=3666

    题目分类:动态规划

    代码

    #include<stdio.h>
    #include<string.h>
    #include<math.h>
    #include<algorithm>
    #include<iostream>
    
    using namespace std;
    
    //用dp[i][j]表示:前i个数构成的序列,这个序列最大值为j,dp[i][j]的值代表相应的cost。
    //dp[i][j]=abs(j-w[i])+min(dp[i-1][k]);(k<=j)
    //i-1构成的序列,最大值是k +  (j-w[i])变成此步需要的花费
    
    #define INF 0x3f3f3f3f
    int n;
    int a[50500];
    int b[5050];
    int dp[5050][5050];
    
    int main()
    {
        //freopen("in.txt","r",stdin);
        //freopen("out.txt","w",stdout);
    
        while(scanf("%d",&n)!=EOF)
        {
            memset(a,0,sizeof(a));
            memset(b,0,sizeof(b));
    
            for(int i=1;i<=n;i++)
            {
                scanf("%d",&a[i]);
                b[i]=a[i];
            }
            sort(b+1,b+n+1);
    
            for(int i=1;i<=n;i++)
            {
                int temp=INF;
                for(int j=1;j<=n;j++)
                {
                    temp=min(temp,dp[i-1][j]);
                    dp[i][j]=abs(a[i]-b[j])+temp;
                }
            }
            int ans=INF;
            for(int i=1;i<=n;i++)
            {
                ans=min(ans,dp[n][i]);
            }
            printf("%d
    ",ans);
        }
        return 0;
    }
    anytime you feel the pain.hey,refrain.don't carry the world upon your shoulders
  • 相关阅读:
    Thymeleaf标签使用
    mybatis映射和条件查询
    开发模型
    Sentinel降级服务
    Sentinel
    Nacos注册中心
    SpringCloudAlibaba简介
    Sleuth
    Stream消息驱动
    如何用JAVA爬取AJAX加载后的页面(利用phantomjs)【以天眼查为例】
  • 原文地址:https://www.cnblogs.com/gaoss/p/4943935.html
Copyright © 2011-2022 走看看