UVa 12170 Easy Climb

题目:

http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=24844

思路:

 引别人一个题解琢磨一下:

from:http://blog.csdn.net/glqac/article/details/45257659

代码:

 #include<iostream>
#include<algorithm>
#define FOR(a,b,c) for(int a=(b);a<(c);a++)
using namespace std; typedef long long LL; const int maxn = + ;
const int maxx = maxn*maxn*;
const LL INF = (1LL << ); LL h[maxn],x[maxx],dp[][maxx]; int main() {
int T; cin>>T;
while(T--) {
int n; LL d;
cin>>n>>d;
FOR(i,,n) cin>>h[i];
if(abs(h[]-h[n-])> (n-)*d) { //input无解
cout<<"impossible\n";
continue;
} int nx=;
FOR(i,,n)
FOR(j,-(n-),n) //-(n-1)..(n-1)
x[nx++]=h[i]+j*d;
sort(x,x+nx); //从小到大单调增长
nx=unique(x,x+nx)-x; //去重且返回最终尾标 //x构造为修改后的所有可能取值 FOR(i,,nx) { //dp_init
dp[][i]=INF;
if(x[i]==h[]) dp[][i]=; //原本第i个就是h[0] //第0个数不能修改
} //d[i][j]意味着已经修改i个数其中第i个数修改成为x[j]需要的最小费用 int t=; //滚动数组的空间优化
FOR(i,,n) {
int k=;
FOR(j,,nx) {
while(k<nx && x[k]<x[j]-d) k++; //两者的取值必须不超过d
while(k+<nx && x[k+]<=x[j]+d && dp[t][k+]<=dp[t][k]) //在 滑动窗口 中最小的
k++;
//刷表法 更新
if(dp[t][k]==INF) dp[t^][j]=INF;
else dp[t^][j]=dp[t][k]+abs(x[j]-h[i]);
}
t^=;
} FOR(i,,nx) if(x[i]==h[n-]) //第N-1个数不能修改
cout<<dp[t][i]<<"\n"; }
return ;
}

【暑假】[深入动态规划]UVa 12170 Easy Climb的更多相关文章

  1. Easy Climb UVA - 12170 滚动dp +离散化+ 单调队列优化

    E.Easy Climb Somewhere in the neighborhood we have a very nice mountain that gives a splendid view o ...

  2. uva 11991 - Easy Problem from Rujia Liu?(STL)

    option=com_onlinejudge&Itemid=8&page=show_problem&problem=3142" target="_blank ...

  3. CJOJ 2485 UVa 11991 生日礼物 / UVa 11991 Easy Problem from Rujia Liu?

    CJOJ 2485 UVa 11991 生日礼物 / UVa 11991 Easy Problem from Rujia Liu? Description (原题来自刘汝佳<训练指南>Pa ...

  4. 【暑假】[深入动态规划]UVa 1628 Pizza Delivery

    UVa 1628 Pizza Delivery 题目: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=51189 思路:    ...

  5. 【暑假】[深入动态规划]UVa 1380 A Scheduling Problem

     UVa 1380 A Scheduling Problem 题目: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=41557 ...

  6. 【暑假】[深入动态规划]UVa 10618 The Bookcase

    UVa 12099  The Bookcase 题目: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=42067 思路:    ...

  7. 【暑假】[深入动态规划]UVa 10618 Fun Game

    UVa 10618 Fun Game 题目: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=36035 思路:   一圈人围坐 ...

  8. 【暑假】[深入动态规划]UVa 10618 Fixing the Great Wall

    UVa 10618 Fixing the Great Wall 题目:  http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=361 ...

  9. 【暑假】[深入动态规划]UVa 1627 Team them up!

    UVa 1627 Team them up! 题目: Team them up! Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Forma ...

随机推荐

  1. 1031: [JSOI2007]字符加密Cipher - BZOJ

    Description喜欢钻研问题的JS 同学,最近又迷上了对加密方法的思考.一天,他突然想出了一种他认为是终极的加密办法:把需要加密的信息排成一圈,显然,它们有很多种不同的读法.例如下图,可以读作: ...

  2. 18:字符串-char型字符串

    1 什么是字符串? 字符串是以空字符(\)结尾的字符数组.空字符的assii码为:0, 空格的ascii码为322 \0的作用'\0'是一个空字符标志,它的ASSII码为0,C++有好多处理字符串的函 ...

  3. mybatis 的<![CDATA[ ]]>

    示例: xml文件: <!-- 获取条数 --> <select id="getCount" parameterType="Map" resu ...

  4. Spring 声明式事务,propagation属性列表及isolation(隔离级别)

    Spring 声明式事务,propagation属性列表 TransactionDefinition接口中定义,共有7种选项可用: PROPAGATION_REQUIRED:支持当前事务,如果当前没有 ...

  5. 【更新链接】U盘启动制作工具(UDTOOL) v3.0.2014.0427

    [校验值] 文件: UDTOOLV3_Setup.exe大小: 525 MB版本: 3.0.2014.0427时间: 2014年4月27日MD5: 2E5187B7D9081E8A69B4DC45C8 ...

  6. 如何使用 APM 搞定 PHP 应用的性能优化?

    APM 究竟是什么? 很多人都是第一次听说 APM 的概念,本文主要阐述如何使用 APM 的解决方案来实现 PHP 应用性能的优化.首先先介绍一下 APM (Application Performan ...

  7. 找出程序cpu使用率高的原因

    确定是CPU过高 使用top观察是否存在CPU使用率过高现象 找出线程 对CPU使用率过高的进程的所有线程进行排序 ps H -e -o pid,tid,pcpu,cmd --sort=pcpu |g ...

  8. 1028-Digital Roots

    描述 The digital root of a positive integer is found by summing the digits of the integer. If the resu ...

  9. Tarjan+模板

    #include<stdio.h> #include<string.h> #include<stdlib.h> #include<math.h> #in ...

  10. zoj 3640 Help Me Escape 概率DP

    记忆化搜索+概率DP 代码如下: #include<iostream> #include<stdio.h> #include<algorithm> #include ...