【暑假】[深入动态规划]UVa 12170 Easy Climb
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的更多相关文章
- Easy Climb UVA - 12170 滚动dp +离散化+ 单调队列优化
E.Easy Climb Somewhere in the neighborhood we have a very nice mountain that gives a splendid view o ...
- uva 11991 - Easy Problem from Rujia Liu?(STL)
option=com_onlinejudge&Itemid=8&page=show_problem&problem=3142" target="_blank ...
- CJOJ 2485 UVa 11991 生日礼物 / UVa 11991 Easy Problem from Rujia Liu?
CJOJ 2485 UVa 11991 生日礼物 / UVa 11991 Easy Problem from Rujia Liu? Description (原题来自刘汝佳<训练指南>Pa ...
- 【暑假】[深入动态规划]UVa 1628 Pizza Delivery
UVa 1628 Pizza Delivery 题目: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=51189 思路: ...
- 【暑假】[深入动态规划]UVa 1380 A Scheduling Problem
UVa 1380 A Scheduling Problem 题目: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=41557 ...
- 【暑假】[深入动态规划]UVa 10618 The Bookcase
UVa 12099 The Bookcase 题目: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=42067 思路: ...
- 【暑假】[深入动态规划]UVa 10618 Fun Game
UVa 10618 Fun Game 题目: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=36035 思路: 一圈人围坐 ...
- 【暑假】[深入动态规划]UVa 10618 Fixing the Great Wall
UVa 10618 Fixing the Great Wall 题目: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=361 ...
- 【暑假】[深入动态规划]UVa 1627 Team them up!
UVa 1627 Team them up! 题目: Team them up! Time Limit: 3000MS Memory Limit: Unknown 64bit IO Forma ...
随机推荐
- 团体程序设计天梯赛-练习集L1-024. 后天
L1-024. 后天 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 如果今天是星期三,后天就是星期五:如果今天是星期六,后天就 ...
- Educational Codeforces Round 11
A. Co-prime Array http://codeforces.com/contest/660/problem/A 题意:给出一段序列,插进一些数,使新的数列两两成互质数,求插最少的个数,并输 ...
- C++11新特性:自动类型推断和类型获取
声明:本文是在Alex Allain的文章http://www.cprogramming.com/c++11/c++11-auto-decltype-return-value-after-functi ...
- Side-by-side assembly
Side-by-side technology is a standard for executable files in Windows 98 Second Edition, Windows 200 ...
- POJ2187Beauty Contest
http://poj.org/problem?id=2187 题意 :有一个农场有N个房子,问最远的房子相距多少距离 . 思路 :凸包,旋转卡壳,通过寻找所有的对锺点,找出最远的点对. #includ ...
- 推荐牛X的一本JS书
主要是看阮一峰的教程时,他参考书目里有这一本中文的, 找来一看,果然高.. 练习一下. function Base(name) { this.name = name; this.getName = f ...
- eclipse运行hadoop程序报错:Connection refused: no further information
eclipse运行hadoop程序报错:Connection refused: no further information log4j:WARN No appenders could be foun ...
- 最短路径算法之一——Floyd算法
Floyd算法 Floyd算法可以用来解决任意两个顶点之间的最短路径问题. 核心公式为: Edge[i][j]=Min{Edge[i][j],Edge[i][k]+Edge[k][j]}. 即通过对i ...
- python脚本工具-2 去除扩展名后提取目录下所有文件名并保存
文件夹里有多个RM格式的视频文件,现需要把它们的文件名都提取出来,并去掉文件的扩展名,以便放到需要的网页里. 源代码: # --- picknames.py --- import os filenam ...
- 简单的神经元算法实现(python)
参考python代码如下 #perceptron x=[[1 ,0, 0],[1,0,1],[1, 1, 0],[1, 1, 1],[0,0,1],[0,1,0],[0,1,1],[0,0,0]] y ...