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. 1074: [SCOI2007]折纸origami - BZOJ

    Description 桌上有一张边界平行于坐标轴的正方形纸片,左下角的坐标为(0,0),右上角的坐标为(100,100).接下来执行n条折纸命令.每条命令用两个不同点P1(x1,y1)和P2(x2, ...

  2. [转载]WCF序列化65536大小限制的问题

    错误: The formatter threw an exception while trying to deserialize the message: There was an error whi ...

  3. Chp3: Stacks and Queue

    1. 说明如何用两个队列来实现一个栈,并分析有关栈操作的运行时间. 解法:1.有两个队列q1和q2,先往q1内插入a,b,c,这做的都是栈的push操作.2.现在要做pop操作,即要得到c,这时可以将 ...

  4. Nagios3完整配置文档

    第一章:简单快速安装nagios 1.1 准备软件包 在做安装之前确认要对该机器拥有root权限. 确认你安装好的linux系统上已经安装如下软件包再继续. Apache GCC编译器 GD库与开发库 ...

  5. 【HTTP】Fiddler(一) - Fiddler简介和使用

    1.为什么是Fiddler? 抓包工具有很多,小到最常用的web调试工具firebug,达到通用的强大的抓包工具wireshark.为什么使用fiddler?原因如下: a.Firebug虽然可以抓包 ...

  6. android 在一个scrollView里面嵌套一个需要滑动的控件(listView、gridView)

    package cn.via.dageeeOrderFood.widget; import android.content.Context; import android.graphics.Point ...

  7. R语言学习笔记:查看函数的R源代码

    getAnywhere 该函数可以返回一些函数的R源代码,如: getAnywhere(kmeans) 该函数具体用法,请参看官方说明. Retrieve an R Object, Including ...

  8. 初始化一台linux server来做项目管理和测试

    毕业以后很多没做过这么技术的事情了,不过今年要开始咯. Goal: 练手安装Nginx,并且配置不同的server,后端有Tomcat的(JIRA),有PHP(总得有的),还有Tornado和Node ...

  9. nginx负载均衡 - session失效

    最近迷上了Nginx,真实麻雀虽小,五脏俱全..功能实在强大.. nginx不单可以作为强大的web服务器,也可以作为一个反向代理服务器,而且nginx还可以按照调度规则实现动态.静态页面的分离,可以 ...

  10. ruby中Block, Proc 和 Lambda 浅析

    Block 与Proc的区别: Block是代码块,Proc是对象: 参数列表中最多只能有一个Block, 但是可以有多个Proc或Lambda; Block可以看成是Proc的一个类实例. Proc ...