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. 1052: [HAOI2007]覆盖问题 - BZOJ

    Description 某人在山上种了N棵小树苗.冬天来了,温度急速下降,小树苗脆弱得不堪一击,于是树主人想用一些塑料薄膜把这些小树遮盖起来,经过一番长久的思考,他决定用3个L*L的正方形塑料薄膜将小 ...

  2. Hibernate - SQLQuery

    使用SQLQuery 对原生SQL查询执行的控制是通过SQLQuery接口进行的,通过执行Session.createSQLQuery()获取这个接口.下面来描述如何使用这个API进行查询. 标量查询 ...

  3. MySQL Replication 常用架构

    转自: http://www.cnblogs.com/ggjucheng/archive/2012/11/13/2768879.html 前言 MySQLReplicaion本身是一个比较简单的架构, ...

  4. NOI考前乱写

    还有13天NOI,把各种乱七八糟的算法都重新过一遍还是比较有必要的... //HDU 5046 Airport //DancingLink #include<iostream> #incl ...

  5. BZOJ 4127 Abs 解题报告

    这个题感觉很厉害的样子.. 首先我们注意到一点:每次加的 $d$ 都是非负的. 那么就说明一个数只可能从负数变成非负数并且只会变一次. 所以我们就可以暴力地去改变一个数的正负情况. 然后我们就可以用树 ...

  6. [转载]HTML5 Audio/Video 标签,属性,方法,事件汇总

    <audio> 标签属性: src:音乐的URL preload:预加载 autoplay:自动播放 loop:循环播放 controls:浏览器自带的控制条 <audio id=& ...

  7. 字符串转到js对象

    var obj = (new Function("return " + str))();

  8. 团体程序设计天梯赛-练习集L2-004. 这是二叉搜索树吗

    L2-004. 这是二叉搜索树吗? 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 一棵二叉搜索树可被递归地定义为具有下列性质的 ...

  9. What is SuppressWarnings (“unchecked”) in Java?

    ometime when looking through code, I see many methods specify an annotation: @SuppressWarnings(" ...

  10. cannot find w3wp.exe in VS

    Same thing was happening to me, when i remembered that IIS has an idle timeout! As above the proc w3 ...