HDU-4570 Multi-bit Trie
http://acm.hdu.edu.cn/showproblem.php?pid=4570
Multi-bit Trie
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 446 Accepted Submission(s): 169

Trie-based solution is the most wildly used one to solve LPM. As shown in Fig.1(b), an uni-bit trie is just a binary tree. Processing LPM on it needs only traversing it from the root to some leaf, according to the input packet's destination address. The longest prefix along this traversing path is the matched one. In order to reduce the memory accesses for one lookup, we can compress some consecutively levels of the Uni-bit Trie into one level, transforming the Uni-bit Trie into a Multi-bit Trie.
For example, suppose the strides array is {3, 2, 1, 1}, then we can transform the Uni-bit Trie shown in Fig.1(b) into a Multi-bit Trie as shown in Fig.1(c). During the transforming process, some prefixes must be expanded. Such as 11(P2), since the first stride is 3, it should be expanded to 110(P2) and 111(P2). But 110(P5) is already exist in the FIB, so we only store the longer one 110(P5).
Multi-bit Trie can obviously reduce the tree level, but the problem is how to build a Multi-bit Trie with the minimal memory consumption (the number of memory units). As shown in Fig.1, the Uni-bit Trie has 23 nodes and consumes 46 memory units in total, while the Multi-bit Trie has 12 nodes and consumes 38 memory units in total.
The first line of each case contains one integer L, which means the number of levels in the Uni-bit Trie.
Following L lines indicate the nodes in each level of the Uni-bit Trie.
Since only 64 bits of an IPv6 address is used for forwarding, a Uni-bit Trie has maximal 64 levels. Moreover, we suppose that the stride for each level of a Multi-bit Trie must be less than or equal to 20.
题意:这题题意确实有点难懂,起码对于我这个英语渣渣来说是这样,于是去别人的博客看了下题目意思,归纳起来如下:
给出一个长度为n的数列,将其分成若干段,要求最小,其中ai是每一段数列的第一项,bi是每一段的长度,l为将数列分成l段。
比如样例:n=7,A={1 2 4 4 5 4 3},将其分成1 2 4| 4 5| 4| 3,则其所用空间为1*2^3+4*2^2+4*2^1+3*2^1=38,而如果分成1 2| 4 4 5| 4 3,则其所用空间为1*2^2+4*2^3+4*2^2=52,比38大。
思路:区间DP,
dp[i][j]表示i--j层最小的内存;
初始条件:全压缩或全不压缩
因为压缩不能超过20层,所以在小于20层时初始条件:
dp[i][j]=a[i]*fun(2,(j-i+1));
大于20层是只能不压缩
dp[i][j]=(sum[j]-sum[i-1])*2;
然后循环
dp[i][j]=min(dp[i][k]+dp[k+1][j],dp[i][j]); k:i...j;
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
__int64 dp[][];
__int64 a[],sum[];
__int64 fun(int x,__int64 y)
{
int i;
__int64 ans=;
for(i=;i<=y;i++)
ans=ans*x;
return ans;
}
int main()
{
int i,t,n,j,k,g;
scanf("%d",&t);
while(t--)
{
memset(dp,,sizeof(dp));
memset(sum,,sizeof(sum));
memset(a,,sizeof(a));
scanf("%d",&n);
for(i=;i<n;i++)
{
scanf("%I64d",&a[i]);
}
sum[]=a[];
for(i=;i<n;i++)
sum[i]=sum[i-]+a[i]; for(k=;k<n;k++)
{
for(i=;i<n-k;i++)
{
j=i+k;
if(k<)//区间小于20层,全压缩
dp[i][j]=a[i]*fun(,(j-i+));
else
dp[i][j]=(sum[j]-sum[i-])*;//区间多于20,全不压缩
for(g=i;g<=j;g++)
{ dp[i][j]=min(dp[i][j],dp[i][g]+dp[g+][j]); }
}
} printf("%I64d\n",dp[][n-]);
}
return ;
}
/*
1
7
1
2
4
4
5
4
3
*/
HDU-4570 Multi-bit Trie的更多相关文章
- hdu 4825 Xor Sum(trie+贪心)
hdu 4825 Xor Sum(trie+贪心) 刚刚补了前天的CF的D题再做这题感觉轻松了许多.简直一个模子啊...跑树上异或x最大值.贪心地让某位的值与x对应位的值不同即可. #include ...
- hdu 4570 Multi-bit Trie 区间DP入门
Multi-bit Trie 题意:将长度为n(n <= 64)的序列分成若干段,每段的数字个数不超过20,且每段的内存定义为段首的值乘以2^(段的长度):问这段序列总的内存最小为多少? 思路: ...
- HDU 4776 Ants(Trie+优先队列)
Ants Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 327680/327680 K (Java/Others) Total S ...
- HDU 1671 Phone List (Trie·数组实现)
链接:http://blog.csdn.net/acvay/article/details/47089657 题意 给你一组电话号码 判断其中是否有某个电话是另一个电话的前缀 字典树的基础应用 ...
- HDU 5384 Danganronpa (Trie树)
题意:给出两个集合S和T,集合中每个元素是个字符串,而T集合中任一元素都是个子弹,可以打S中的任一怪物,如果子弹是怪物的子串,那么才有伤害值1,若在怪物中出现多次,次数为该子弹打到该怪物的伤害值.每个 ...
- HDU 4825-Xor Sum(trie)
题意: 给你一组数,开始询问给一个数 求组中与该数异或值最大的数. 分析:根据异或的特点 要想得到的异或值最大 尽可能的让两个数的每位都相反 先把给定的一组数建树,数的最后一位对应的节点保存这个数的 ...
- hdu 1251 统计难题 trie入门
统计难题 Problem Description Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本 ...
- HDU 1251 统计拼图 Trie解决问题的方法
基本上找到一个标准前缀的问题是,只需要insert和search它功能. 这里的主要变化是我n该记录方法,这里n国旗代表的不是叶节点,但是话的标志这条道路后的数字. 然后找到需要找到一个词的前缀,假如 ...
- HDU 4570(区间dp)
E - Multi-bit Trie Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u ...
- HDU 1251 统计难题(Trie)
统计难题 [题目链接]统计难题 [题目类型]Trie &题解: Trie的模板题,只不过这题坑点在没给数据范围,改成5e5就可以过了,用的刘汝佳蓝书模板 &代码: #include & ...
随机推荐
- PHP的无限栏目分类
自己在PHP的无线栏目分类上面就是搞了很久都没有明白,所以现在是趁着记忆力还没有完全的消退的时候速度的记录下来 这里讲解的是最简单的树形栏目,适合的是小中型的栏目分类需求 1.这里讲解的是针对是只要通 ...
- PHP的数据库 之 关闭问题
首先,PHP由于有垃圾回收机制,所以数据库即使你不手动关闭,也有自动去关闭的机制, 这里就和操作文本流不同,文本流需要手动去关闭,不然会发生内存浪费现象 并且,PHP在同时连接多个DB的时候,连接到一 ...
- javascript 封装 构造函数继承 非构造函数继承
1 封装 把"属性"(property)和"方法"(method),封装成一个对象,甚至要从原型对象生成一个实例对象 1.1 简单封装:var cat1 = { ...
- [时间操作] C#DateFormat时间帮助类 (转载)
点击下载 DateFormat.rar 主要功能如下 返回每月的第一天和最后一天 看下面代码吧 /// <summary> /// 类说明:时间操作类 /// 编 码 人:苏飞 /// 联 ...
- 本地tomcat的start.bat启动时访问不出现小猫图标
排除端口错误.看看是不是webapps的root文件夹删除了,如果删除了,从tomcat的压缩包中解压一个root文件夹,房里面即可
- phpmyadmin备份小问题
不要将imformation——shame或者mysql等备份,要有选择的备份表 关注我的新浪微博
- librarynotfoundforlPodsAFNetworking解决放案
http://www.it165.net/pro/html/201503/36422.html
- Gulp那些好用的插件 2016.04.20
开始接触LESS.组件化编程后,慢慢意识到需要一个提高工作效率的构建工具,就此接触到了Gulp. Gulp的好处在这里就不细说啦,只有四个API接口学起来简直爽歪歪,减少了大量的I/O操作,用起来很畅 ...
- CSS3弹性盒模型flex box快速入门 2016.03.16
@import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/c ...
- hibernate映射
三种方式: 持久化注解 目前开发主流方式 XML配置描述文件(XML deployment descriptor,可以让Hibernate的PO类与JPA实体类兼容,实际中很少用) ...