POJ 1661 记忆化搜索

2024-06-20 04:38
文章标签 搜索 记忆 poj 1661

本文主要是介绍POJ 1661 记忆化搜索,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

测试数据:


7
3 8 17 20
0 10 8
0 10 13
4 14 3


3 8 7 2
6 14 6
4 10 4
5 14 2


1 6 10 20
2 3 5


10 899 52 50
893 903 18
890 900 38
898 908 8
910 920 8
894 904 43
881 891 18
872 882 38
867 877 43
842 852 43
895 905 3


3 8 17 20
8 10 8
8 10 13
8 14 3


60 822 302 50
813 823 298
813 823 293
816 826 213
816 826 178
817 827 218
813 823 148
821 831 73
814 824 248
813 823 283
815 825 158
819 829 58
814 824 13
813 823 28
819 829 233
814 824 43
773 783 293
821 831 93
818 828 268
816 826 198
818 828 113
814 824 208
816 826 68
821 831 133
794 804 248
814 824 108
829 839 28
818 828 143
844 854 298
802 812 133
801 811 28
818 828 238
817 827 8
816 826 48
820 830 3
819 829 288
822 832 138
820 830 183
855 865 13
777 787 268
820 830 63
789 799 28
822 832 33
855 865 213
779 789 208
836 846 248
806 816 33
821 831 263
818 828 83
846 856 263
789 799 68
854 864 8
854 864 283
801 811 298
805 815 143
822 832 23
821 831 173
813 823 153
858 868 138
818 828 98
839 849 133


4 14 5 6
3 22 1
16 23 2
16 30 3
13 21 4

ans:

23
17
10
61
17
352

15


#include "stdio.h"
#include "string.h"
#include "algorithm"
using namespace std;int inf=2000000000;
int n,Max;
struct comp
{int l,r,y;
}data[1010];struct node
{int l,r;
}dp[1010];int min(int a,int b)
{if (a<b) return a; else return b;
}bool cmp(const comp a,const comp b)
{return a.y>b.y;
}
void dfs(int w)
{int i,now;if (data[w].y==0) return ;if (dp[w].l!=inf || dp[w].r!=inf ) return ;//查找从左边掉落的最优值for (i=w+1;i<=n+1;i++)if (data[i].l<=data[w].l &&data[i].r>=data[w].l){now=i;break;}if (data[w].y-data[now].y>Max) dp[w].l=inf;else{dfs(now);if (data[now].y==0) dp[w].l=0;elsedp[w].l=min(dp[now].l+data[w].l-data[now].l,dp[now].r+data[now].r-data[w].l);}//查找从右边掉落的最优值for (i=w+1;i<=n+1;i++)if (data[i].l<=data[w].r &&data[i].r>=data[w].r){now=i;break;}if (data[w].y-data[now].y>Max) dp[w].r=inf;else{dfs(now);if (data[now].y==0) dp[w].r=0;elsedp[w].r=min(dp[now].l+data[w].r-data[now].l,dp[now].r+data[now].r-data[w].r);}return ;}
int main()
{int t,x,y,i;scanf("%d",&t);while (t--){scanf("%d%d%d%d",&n,&x,&y,&Max);for (i=1;i<=n;i++)scanf("%d%d%d",&data[i].l,&data[i].r,&data[i].y);data[0].l=data[0].r=x; data[0].y=y;data[n+1].l=-20000; data[n+1].r=20000; data[n+1].y=0;sort(data,data+n+2,cmp);for (i=0;i<=n;i++)dp[i].l=dp[i].r=inf;dp[n+1].l=dp[n+1].r=0;dfs(0);printf("%d\n",dp[0].l+y);}return 0;
}




这篇关于POJ 1661 记忆化搜索的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1077121

相关文章

【文末附gpt升级秘笈】腾讯元宝AI搜索解析能力升级:千万字超长文处理的新里程碑

腾讯元宝AI搜索解析能力升级:千万字超长文处理的新里程碑 一、引言 随着人工智能技术的飞速发展,自然语言处理(NLP)和机器学习(ML)在各行各业的应用日益广泛。其中,AI搜索解析能力作为信息检索和知识抽取的核心技术,受到了广泛的关注和研究。腾讯作为互联网行业的领军企业,其在AI领域的探索和创新一直走在前列。近日,腾讯旗下的AI大模型应用——腾讯元宝,迎来了1.1.7版本的升级,新版本在AI搜

代码随想录算法训练营第三十九天|62.不同路径 63. 不同路径 II 343.整数拆分 96.不同的二叉搜索树

LeetCode 62.不同路径 题目链接:62.不同路径 踩坑:二维的vector数组需要初始化,否则会报错访问空指针 思路: 确定动态数组的含义:dp[i][j]:到达(i,j)有多少条路经递推公式:dp[i][j] = dp[i-1][j] + dp[i][j-1]初始化动态数组:dp[0][0] = 1遍历顺序:从左到右,从上到下 代码: class Solution {pu

poj 3882(Stammering Aliens) 后缀数组 或者 hash

后缀数组:  构建后缀数组,注意要在字符串莫末尾加上一个没出现过的字符。然后可以2分或者直接扫描,直接扫描需要用单调队列来维护 VIEW CODE #include<cstdio>#include<algorithm>#include<iostream>#include<cmath>#include<queue>#include<stack>#include<string

poj 3294(Life Forms) 2分+ 后缀数组

我曾用字符串hash写,但是超时了。只能用后最数组了。大致思路:用不同的符号吧字符串连接起来,构建后缀数组,然后2分答案,依次扫描后缀数组,看是否瞒住条件。 VIEW CODE #include<cstdio>#include<vector>#include<cmath>#include<algorithm>#include<cstring>#include<cassert>#

poj 2391 Ombrophobic Bovines (网络流)

这是一道很经典的网络流的题目。首先我们考虑假如我们的时间为无穷大。我们吧每个点拆成2个点 i和i' .。虚拟源点s和汇点t。对于每个点建边(s,i, a[i])  (i‘,t,ib[i]) 。 其中a[i]为给点有多少牛,b[i]为容量。i和j连通 建边 (i,j',inf);如果最大流==所有牛的个数,就可能装下所有的牛。那么现在我们考虑时间。假设最大时间为T.那么如果i到j的的最短时间>T

poj 1330 LCA 最近公共祖先

水题目。直接上代码了。 VIEW CODE #include<cstdio>#include<algorithm>#include<iostream>#include<cmath>#include<queue>#include<stack>#include<string>#include<cstring>#include<map>#include<vector>#

poj 3160 Father Christmas flymouse 强连通+dp

首先我们可以确定的是,对于val值小于0的节点都变成0.   假设一个集合内2个房间都能任意到达,那么我就可以吧集合内的所有点的价值都取到,并且可以达到任一点。实际上集合内的每个点是相同的,这样的集合就是一个强连通分量。 那么我们就可以用tarjin算法进行强连通缩点, 最后形成一个dag的图。在dag的图上面进行dp。可以先用拓扑排序后dp。或者建反响边记忆化搜索 。 VIEW

leetcode刷题(45)——35. 二叉搜索树的最近公共祖先

给定一个二叉搜索树, 找到该树中两个指定节点的最近公共祖先。 百度百科中最近公共祖先的定义为:“对于有根树 T 的两个结点 p、q,最近公共祖先表示为一个结点 x,满足 x 是 p、q 的祖先且 x 的深度尽可能大(一个节点也可以是它自己的祖先)。” 例如,给定如下二叉搜索树: root = [6,2,8,0,4,7,9,null,null,3,5] 示例 1: 输入: root = [

【智能优化算法改进策略之局部搜索算子(五)—自适应Rosenbrock坐标轮换法】

1、原理介绍 作为一种有效的直接搜索技术,Rosenbrock坐标轮换法[1,2]是根据Rosenbrock著名的“香蕉函数”的特点量身定制的,该函数的最小值位于曲线狭窄的山谷中。此外,该方法是一种典型的基于自适应搜索方向集的无导数局部搜索技术。此法于1960年由Rosenbrock提出,它与Hooke-Jeeves模式搜索法有些类似,但比模式搜索更为有效。每次迭代运算分为两部分[3]: 1)

Day59 代码随想录打卡|二叉树篇---把二叉搜索树转换为累加树

题目(leecode T538): 给出二叉 搜索 树的根节点,该树的节点值各不相同,请你将其转换为累加树(Greater Sum Tree),使每个节点 node 的新值等于原树中大于或等于 node.val 的值之和。 提醒一下,二叉搜索树满足下列约束条件: 节点的左子树仅包含键 小于 节点键的节点。节点的右子树仅包含键 大于 节点键的节点。左右子树也必须是二叉搜索树。 方法:本题