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]=num[i]*pow(j-i)*2;

大于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 <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std; int n;
__int64 dp[70][70],a[70],sum[70]; __int64 pow(__int64 n)
{
__int64 ans= 1;
int i;
for(i = 1; i<=n; i++)
ans*=2;
return ans;
} int main()
{
int t,i,j,k,s;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
memset(sum,0,sizeof(sum));
for(i = 1; i<=n; i++)
{
scanf("%I64d",&a[i]);
sum[i] = sum[i-1]+a[i];
}
memset(dp,0,sizeof(dp));
for(s = 0; s<=n; s++)
{
for(i = 1; i<=n && i+s<=n; i++)
{
j = i+s;
if(s<=19)//小于20层,全压缩
dp[i][j] =a[i]*pow(j-i)*2;
else//多于20,全不压缩
dp[i][j] = (sum[j]-sum[i-1])*2;
for(k = i; k<=j; k++)//区间dp
dp[i][j] = min(dp[i][j],dp[i][k]+dp[k+1][j]);
}
}
printf("%I64d\n",dp[1][n]);
} return 0;
}

HDU4570:Multi-bit Trie(区间DP)的更多相关文章

  1. 【hdu4570】Multi-bit Trie 区间DP

    标签: 区间dp hdu4570 http://acm.hdu.edu.cn/showproblem.php?pid=4570 题意:这题题意理解变态的.转自大神博客: 这题题意确实有点难懂,起码对于 ...

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

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

  3. HDU 4570---Multi-bit Trie(区间DP)

    题目链接 Problem Description IP lookup is one of the key functions of routers for packets forwarding and ...

  4. Codechef STREDUC Reduce string Trie、bitset、区间DP

    VJ传送门 简化题意:给出一个长度为\(l\)的模板串\(s\)与若干匹配串\(p_i\),每一次你可以选择\(s\)中的一个出现在集合\(\{p_i\}\)中的子串将其消去,其左右分成的两个串拼接在 ...

  5. HDU 4570(区间dp)

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

  6. hdu-5653 Bomber Man wants to bomb an Array.(区间dp)

    题目链接: Bomber Man wants to bomb an Array. Time Limit: 4000/2000 MS (Java/Others)     Memory Limit: 65 ...

  7. 【BZOJ-4380】Myjnie 区间DP

    4380: [POI2015]Myjnie Time Limit: 40 Sec  Memory Limit: 256 MBSec  Special JudgeSubmit: 162  Solved: ...

  8. 【POJ-1390】Blocks 区间DP

    Blocks Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 5252   Accepted: 2165 Descriptio ...

  9. 区间DP LightOJ 1422 Halloween Costumes

    http://lightoj.com/volume_showproblem.php?problem=1422 做的第一道区间DP的题目,试水. 参考解题报告: http://www.cnblogs.c ...

随机推荐

  1. Android EditText如何去除边框添加下划线

    (一)问题 之前的自定义EditText只能显示高度不超过屏幕高度的文本内容,继续增加内容会出现如下问题: (二)原因分析 下部(超出屏幕高度的部分)没有继续画线,也就是说横线没有画够,那么一定是循环 ...

  2. Android进阶篇-线程分析(一)

    转载自:http://www.trinea.cn/android/java-android%E7%BA%BF%E7%A8%8B%E6%B1%A0/ 介绍new Thread的弊端及Java四种线程池的 ...

  3. 如何kill掉TaobaoProtect.exe

    C:\Users\Administrator\AppData\Roaming\TaobaoProtect TaobaoProtect.exe https://technet.microsoft.com ...

  4. Caused by: java.lang.ClassNotFoundException: javax.persistence.EntityListeners

    Answer: This seems to be caused by Hibernate 3.6. It is now dependent on JPA, so it must have a JPA ...

  5. poj2187

    求最远点对,这是一道经典的旋转卡壳的题目话说由于是前年写的,之后就没怎么研究过计算几何了……感觉都不大记得清了,来稍微回忆一下……首先最远点对一定出现在凸包上显然,然后穷举肯定不行,这时候就需要旋转卡 ...

  6. bzoj1054

    弱弱的搜索题, 我的做法是将矩阵看做二进制然后用位运算来做的,感觉比较舒服 ..] ,,,);       dy:..] ,,-,); type node=record        po,next: ...

  7. PHP ‘asn1_time_to_time_t’函数内存损坏漏洞

    漏洞名称: PHP ‘asn1_time_to_time_t’函数内存损坏漏洞 CNNVD编号: CNNVD-201312-348 发布时间: 2013-12-18 更新时间: 2013-12-18 ...

  8. Microsoft Internet Explorer内存破坏漏洞(CVE-2013-5052)

    漏洞版本: Microsoft Internet Explorer 6-11 漏洞描述: BUGTRAQ ID: 64126 CVE(CAN) ID: CVE-2013-5052 Internet E ...

  9. QNX环境

    QNX开发环境和QNX虚拟机都搭建好了,开始写第一个QNX程序. 关于QNX程序开发的最好参考是QNX官网上的pdf书<10 Steps to Developing a QNX Program: ...

  10. .9.png

    .9.png是一种非失真性压缩位图图形文件格式.PNG格式是非失真性压缩的,允许使用类似于GIF格式的调色板技术,支持真彩色图像,并具备阿尔法通道(半透明)等特性.现在有很多人使用PNG格式于互联网及 ...