POJ 2374 Fence Obstacle Course(map优化DP)
来源:互联网 发布:网络事件 编辑:程序博客网 时间:2024/06/01 08:34
题目链接
用下面这个图辅助说明
这题只需要从上到下按层 维护图中这些虚线即可。
例如当前需要在图中红栏杆处做决策时,需要把上面的三条虚线删除,而添加两端点处的新路径,路程当然要去上面三条路的最小值 。
代码:
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <map>using namespace std;const int maxn=50050 , INF = 0x7f7f7f7f;map<int,int>DP;typedef map<int ,int>::iterator MIT;int L[maxn] , R[maxn] , N, S;int readint(){ char c=getchar(); int ans = 0 ,t=1; while((c<'0' || c>'9') && c != '-') c=getchar(); if(c=='-') {t=-1; c=getchar(); } while(c>='0' && c<='9') ans= ans*10 + c-'0' , c=getchar(); return ans*t;}int main(){ while(scanf("%d%d",&N,&S)!=EOF){ DP.clear(); for(int i=N; i>=1; i--) L[i] = readint() , R[i] = readint(); //scanf("%d%d",&L[i],&R[i]); DP[S]=0; for(int i=1;i<=N;i++){ MIT Begin = DP.lower_bound(L[i]+1) , End = DP.lower_bound(R[i]); if(Begin==End) continue; int ML=INF , MR = INF; for(MIT it=Begin; it!=End ; it++){ ML = min(ML , it->second + it->first-L[i]); MR = min(MR , it->second + R[i]-it->first); } DP.erase(Begin,End); if(DP.count(L[i])) DP[L[i]] = min (DP[L[i]] , ML); else DP[L[i]] = ML; if(DP.count(R[i])) DP[R[i]] = min(DP[R[i]] , MR); else DP[R[i]] = MR; } int ans = INF; for(MIT it=DP.begin(); it!=DP.end() ; it++){ ans = min(ans , it->second + abs(it->first)); } printf("%d\n",ans); } return 0;}
- POJ 2374 Fence Obstacle Course(map优化DP)
- POJ 2374 Fence Obstacle Course 线段树优化DP
- poj 2374 Fence Obstacle Course
- POJ 2374Fence Obstacle Course
- [线段树]POJ 2374 Fence Obstacle Course
- POJ 2374 Fence Obstacle Course(线段树+动态规划)
- pku2374 Fence Obstacle Course
- poj2374 Fence Obstacle Course
- POJ2374-Fence Obstacle Course
- POJ 2374/bzoj 3387: [Usaco2004 Dec]Fence Obstacle Course栅栏行动
- poj2374 Fence Obstacle Course(线段树+建图spfa)
- HDOJ 题目3152 Obstacle Course(BFS,优先队列优化)
- TOJ 1600 Fence Obstacle Course -- 线段树,链表
- BZOJ3387 [USACO2004 Dec] Fence Obstacle Course栅栏行动
- BZOJ3387 [Usaco2004 Dec]Fence Obstacle Course栅栏行动
- BZOJ3387: [Usaco2004 Dec]Fence Obstacle Course栅栏行动
- poj 1821 Fence(dp+单调队列优化)
- POJ 1821 Fence(DP+单调队列优化)
- poj 1276(背包问题,二进制优化技巧)
- Linux基本命令
- 8.4.7:一元运算符
- poj 3267(匹配最近的单词)
- unity4.x for mac破解(含Unity全版本破解)
- POJ 2374 Fence Obstacle Course(map优化DP)
- SimpleDateFormat的线程安全问题
- 测试怎样拿高工资--由‘参加1年测试培训想要拿高工资(7000-8000)的帖子’想到
- poj 1836(简单DP+两边lis)
- 求四个数的最大公约数
- Taobao数据库这5年(摘要)
- PowerHA(HACMP)测试 以及 故障排除
- poj 1260(购买且是最后一个购买的dp问题)
- poj 2533(裸的LIS,很少见的水题)