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

Problem Description
  IP lookup is one of the key functions of routers for packets forwarding and classifying. Generally, IP lookup can be simplified as a Longest Prefix Matching (LPM) problem. That's to find the longest prefix in the Forwarding Information Base (FIB) that matches the input packet's destination address, and then output the corresponding Next Hop information.

  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.
 
Input
  The first line is an integer T, which is the number of testing cases.
  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.
 
Output
  Output the minimal possible memory units consumed by the corresponding Multi-bit Trie.
 
Sample Input
1
7
1 2 4 4 5 4 3
 
Sample Output
38
题意真难懂。

题意:这题题意确实有点难懂,起码对于我这个英语渣渣来说是这样,于是去别人的博客看了下题目意思,归纳起来如下:

给出一个长度为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;

 
Source
#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的更多相关文章

  1. hdu 4825 Xor Sum(trie+贪心)

    hdu 4825 Xor Sum(trie+贪心) 刚刚补了前天的CF的D题再做这题感觉轻松了许多.简直一个模子啊...跑树上异或x最大值.贪心地让某位的值与x对应位的值不同即可. #include ...

  2. hdu 4570 Multi-bit Trie 区间DP入门

    Multi-bit Trie 题意:将长度为n(n <= 64)的序列分成若干段,每段的数字个数不超过20,且每段的内存定义为段首的值乘以2^(段的长度):问这段序列总的内存最小为多少? 思路: ...

  3. HDU 4776 Ants(Trie+优先队列)

    Ants Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 327680/327680 K (Java/Others) Total S ...

  4. HDU 1671 Phone List (Trie·数组实现)

    链接:http://blog.csdn.net/acvay/article/details/47089657 题意  给你一组电话号码  判断其中是否有某个电话是另一个电话的前缀 字典树的基础应用   ...

  5. HDU 5384 Danganronpa (Trie树)

    题意:给出两个集合S和T,集合中每个元素是个字符串,而T集合中任一元素都是个子弹,可以打S中的任一怪物,如果子弹是怪物的子串,那么才有伤害值1,若在怪物中出现多次,次数为该子弹打到该怪物的伤害值.每个 ...

  6. HDU 4825-Xor Sum(trie)

    题意: 给你一组数,开始询问给一个数  求组中与该数异或值最大的数. 分析:根据异或的特点 要想得到的异或值最大 尽可能的让两个数的每位都相反 先把给定的一组数建树,数的最后一位对应的节点保存这个数的 ...

  7. hdu 1251 统计难题 trie入门

    统计难题 Problem Description Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本 ...

  8. HDU 1251 统计拼图 Trie解决问题的方法

    基本上找到一个标准前缀的问题是,只需要insert和search它功能. 这里的主要变化是我n该记录方法,这里n国旗代表的不是叶节点,但是话的标志这条道路后的数字. 然后找到需要找到一个词的前缀,假如 ...

  9. HDU 4570(区间dp)

    E - Multi-bit Trie Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u ...

  10. HDU 1251 统计难题(Trie)

    统计难题 [题目链接]统计难题 [题目类型]Trie &题解: Trie的模板题,只不过这题坑点在没给数据范围,改成5e5就可以过了,用的刘汝佳蓝书模板 &代码: #include & ...

随机推荐

  1. CI框架篇之模型篇--直接操作(2)

    在CI里面对数据库的操作有两种形式,一种是直接通过最原始的sql语句操作 一种则是通过CI里面的AR模型进行操作.两种操作各有千秋,应当有机的结合 现在讲解第一种操作的方式: 装载数据路操作类后就能够 ...

  2. (转)apache的keepalive和keepalivetimeout(apache优化)

    KeepAlive指的是保持连接活跃,类似于Mysql的永久连接.   如果将KeepAlive设置为On,那么来自同一客户端的请求就不需要再一次连接,避免每次请求都要新建一个连接而加重服务器的负担. ...

  3. Android 输入法键盘和activity页面遮挡问题解决

    本文主要介绍Android中如何解决输入法键盘和activity页面遮挡的问题. 总结: 不希望遮挡设置activity属性android:windowSoftInputMode="adju ...

  4. SQL使用存儲過程訪問不同服務器

    用openrowset连接远程SQL或插入数据 --如果只是临时访问,可以直接用openrowset --查询示例 select * from openrowset('SQLOLEDB', 'sql服 ...

  5. git 常见问题收集(持续更新中)

    1.问题:在配置完成github上的ssh后如何使用ssh? 答: 1)http方式 url =https://github.com/username/test_repo.git 2)ssh方式:把u ...

  6. Lucene初步搜索

    Lucene在创立索引后,要进行搜索查询 搜索大概需要5部, 1,读取索引. 2,查询索引. 3,匹配数据. 4,封装匹配结果. 5,获取需要的值. 语言表达能力不好,大概就是分着几部吧. /** * ...

  7. HDU 1074 Doing Homework (dp+状态压缩)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1074 题目大意:学生要完成各科作业, 给出各科老师给出交作业的期限和学生完成该科所需时间, 如果逾期一 ...

  8. vs2010 “发生生成错误,运行上次的成功运行的程序”怎么改回不运行。

    当程序出现错误时,会出现下面对话框: 如果选择"是",并且勾选了"不再显示此对话框",对你以后的操作时非常麻烦的. 许多同学想再次调出次窗口,不知道怎么操作,操 ...

  9. POJ 1014 Dividing 多重背包

    Dividing Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 63980   Accepted: 16591 Descri ...

  10. sec:authorize 标签 通过不通过权限例子

    1. 方式一     <sec:authorize ifAnyGranted="ROLE_A">     <a href="a.jsp"> ...