题目链接:

  Poj 3666 Making the Grade

题目描述:

  给出一组数,每个数代表当前位置的地面高度,问把路径修成非递增或者非递减,需要花费的最小代价?

解题思路:

  对于修好的路径的每个位置的高度肯定都是以前存在的高度,修好路后不会出现以前没有出现过得高度

  dp[i][j]代表位置i的地面高度为第j个高度,然后我们可以把以前的路修好后变成非递减路径,或者把以前的路径首尾颠倒,然后修成非递减路径。状态转移方程为:dp[i][j] = min(dp[i-1][k]) + a[i] - a[j];因为要把路径修成非递减路径,所以a[j]>a[k],所以我们要三重循环来搞,时间复杂度O(n^3)。如果我们对原来数列进行复制排序的话,a[j]>a[k]就转化为了j>k,这样就成功的把时间复杂度从O(n^3)降为O(n^2)。空间复杂度也可以用滚动数组从O(n^2)变为O(n)。

注意啦!注意啦!一定要用长整形,要不然会爆炸。用了长整形的话,取绝对值的时候就不能用abs了,只有手动实现或者用fabs咯。亲试······ce好几次(吐血~)

 #include<stdio.h>
#include <queue>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#include<algorithm> using namespace std;
typedef long long LL;
const LL maxn = ;
const LL INF = 0xfffffff;
LL dp[maxn][maxn];
//dp[i][j]第i个木棒,长度是j LL solve (LL a[], LL b[], LL n)
{
for (int i=; i<=n; i++)
dp[][i] = fabs (b[i] - a[]);
for (int i=; i<=n; i++)//第i个位置
{
LL Min = dp[i-][];
for (int j=; j<=n; j++)
{
Min = min (dp[i-][j], Min);
dp[i][j] = Min + fabs(a[i] - b[j]); }
}
LL Max = dp[n][];
for (int i=; i<=n; i++)
Max = min (Max, dp[n][i]);
return Max;
} int main ()
{
LL n, a[maxn], b[maxn];
while (scanf ("%lld", &n) != EOF)
{
for (int i=; i<=n; i++)
{
scanf ("%lld", &a[i]);
b[i] = a[i];
}
sort (b+, b+n+); LL ans = solve (a, b, n);
for (int i=, j=n; i<j; i++, j--)
swap (a[i], a[j]); ans = min (ans, solve(a, b, n));
printf ("%lld\n", ans);
}
return ;
}

Poj 3666 Making the Grade (排序+dp)的更多相关文章

  1. POJ - 3666 Making the Grade(dp+离散化)

    Description A straight dirt road connects two fields on FJ's farm, but it changes elevation more tha ...

  2. poj 3666 Making the Grade(dp离散化)

    Making the Grade Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7068   Accepted: 3265 ...

  3. poj 3666 Making the Grade(离散化+dp)

    Description A straight dirt road connects two fields on FJ's farm, but it changes elevation more tha ...

  4. POJ 3666 Making the Grade【DP】

    读题堪忧啊,敲完了才发现理解错了..理解题必须看样例啊!! 题目链接: http://acm.hust.edu.cn/vjudge/contest/view.action?cid=110495#pro ...

  5. POJ 3666 Making the Grade (DP)

    题意:输入N, 然后输入N个数,求最小的改动这些数使之成非严格递增即可,要是非严格递减,反过来再求一下就可以了. 析:并不会做,知道是DP,但就是不会,菜....d[i][j]表示前 i 个数中,最大 ...

  6. POJ 3666 Making the Grade (DP滚动数组)

    题意:农夫约翰想修一条尽量平缓的路,路的每一段海拔是A[i],修理后是B[i],花费|A[i] – B[i]|,求最小花费.(数据有问题,代码只是单调递增的情况) #include <stdio ...

  7. POJ 3666 Making the Grade(数列变成非降序/非升序数组的最小代价,dp)

    传送门: http://poj.org/problem?id=3666 Making the Grade Time Limit: 1000MS   Memory Limit: 65536K Total ...

  8. poj 3666 Making the Grade(dp)

    Description A straight dirt road connects two fields on FJ's farm, but it changes elevation more tha ...

  9. POJ 3666 Making the Grade(二维DP)

    题目链接:http://poj.org/problem?id=3666 题目大意:给出长度为n的整数数列,每次可以将一个数加1或者减1,最少要多少次可以将其变成单调不降或者单调不增(题目BUG,只能求 ...

随机推荐

  1. WCF服务端的.NET Core支持项目Core WCF 正式启动

    长期以来在wcf客户端库 https://github.com/dotnet/wcf 里反应最强烈的就是.NET Core的服务端支持 https://github.com/dotnet/wcf/is ...

  2. [Android]Android5.0实现静默接听电话功能

    原因: android曾经能够通过AIDL进行静默接听.可是5.0以后就被谷歌给屏蔽了.这时候我们仅仅能通过其它方式实现了. 解决方式: try { Runtime.getRuntime().exec ...

  3. HTTP请求中带有特殊字符"|",返回400错误

    Java平台,服务器是Tomcat8,前端ajax访问服务器时,F12返回400错误,经分析,URL地址中get传参值里面含有“|“, Invalid character found and RFC ...

  4. ArrayList遍历的4种方法

    public class ArrayListDemo { public static void main(String args[]){ List<String> list = new A ...

  5. 解决gradle多模块依赖在Idea中能运行,gradle build失败的问题。

    最近需要初始化一个SpringBoot新项目遇到一个问题就是:项目中有多个子模块,使用gradle依赖管理成功. 项目结构如下: project --module1     --module2我的mo ...

  6. date format记录

    各种日期格式定义,容易忘记,这里备注下: * 支持格式为 yyyy.MM.dd G 'at' hh:mm:ss z 如 '2002-1-1 AD at 22:10:59 PSD'<br> ...

  7. spin_lock、spin_lock_irq、spin_lock_irqsave区别

    void spin_lock(spinlock_t *lock); void spin_lock_irq(spinlock_t *lock); void spin_lock_irqsave(spinl ...

  8. Why is an 'Any CPU' application running as x86 on a x64 machine?

      It's likely that you linked some assemblies that are not Any CPU, but include native code (or are ...

  9. hdu-5718 Oracle(水题)

    题目链接: Oracle Time Limit: 8000/4000 MS (Java/Others)     Memory Limit: 262144/262144 K (Java/Others) ...

  10. SPOJ:House Fence(分治&DP)

    "Holiday is coming, holiday is coming, hurray hurray!" shouts Joke in the last day of his ...