HDU 4570 Multi-bit Trie(区间dp)

原创 2015年07月10日 10:09:50

Multi-bit Trie

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 539    Accepted Submission(s): 214


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
 

Source
 

Recommend

/*
题意:一个长度为n的数列,将其分成若干段(每一段的长度要<=20),
要求∑ai*(2^bi)最小,其中ai是每一段数列的第一项,bi是每一段的长度。

比如: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


*/



#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<set>
#include<map>

#define L(x) (x<<1)
#define R(x) (x<<1|1)
#define MID(x,y) ((x+y)>>1)

#define bug printf("hihi\n")

#define eps 1e-8
typedef __int64 ll;

using namespace std;

#define INF 0x3f3f3f3f
#define N 65

ll dp[N];
ll a[N];
int n;
int main()
{
    int i,j,t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        for(i=1;i<=n;i++)
            {
                scanf("%I64d",&a[i]);

            }
        memset(dp,INF,sizeof(dp));
        for(i=n+1;i<=n+20;i++)
            dp[i]=0;
        dp[n]=a[n]*2;

         for(i=n;i>=1;i--)
            for(j=i+1;j<=min(i+20,n+1);j++)//在j这个位置分开
         {
             dp[i]=min(dp[i],a[i]*(1<<(j-i))+dp[j]);
         }
         printf("%I64d\n",dp[1]);
    }
    return 0;
}






版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

Hdu 4570 Multi-bit Trie(区间dp)

点击打开链接 Multi-bit Trie Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Jav...

hdu4570Multi-bit Trie (区间DP)

Problem Description   IP lookup is one of the key functions of routers for packets forwarding and c...

HDU4570:Multi-bit Trie(区间DP)

Problem Description   IP lookup is one of the key functions of routers for packets forwarding and cl...

HDU-4570 Multi-bit Trie (DP)

题意太难懂了 准备直接看题解,结果一看大家都说是简单DP,就先回去好好看题 大致明白后,既然知道是dp,就往dp想了...想到一个很麻烦dp转移方程,dp[i]由dp[j]更新,其中再取后k个合并为...

hdu 4570 Multi-bit Trie(dp水题)

Multi-bit Trie Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

hdu4570 Multi-bit Trie

Hot~ 2014暑期多校联合训练——正式启动报名~ 详见“杭电ACM”微博~ Multi-bit Trie Time Limit: 2000/1000 MS (Java/Ot...

2011 Multi-University Training Contest 3 - Host by BIT No Gambling 也就是hdu 3863

这个题我是见识了博弈的妙处,虽然我不懂博弈,但是当我按照队友的想法a了这个题的时候,我很是吃惊,甚至敲得时候我都没有底气,好吧,废话少说,先看题! 原题传送门:http://acm.hdu.edu....

HDU-6069 Counting Divisors - 2017 Multi-University Training Contest - Team 4(分解质因子区间筛法)

In mathematics, the function d(n)d(n) denotes the number of divisors of positive integer nn. For exa...
  • yo_bc
  • yo_bc
  • 2017年08月04日 16:50
  • 95

2016 Multi-University Training Contest 10 [HDU 5861] Road (线段树区间更新+差分数组)

链接HDU 5861题意在n个村庄之间存在n-1段路,令某段路开放一天需要交纳wi的费用,但是每段路只能开放一次,一旦关闭将不再开放。现在给你接下来m天内的计划,在第i天,需要对村庄ai到村庄bi的道...

HDU1556 Color the ball(树状数组BIT 区间修改单点询问)

Color the ball Time Limit: 3000msMemory Limit: 32768KB 64-bit integer IO format: %lld Java cla...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:HDU 4570 Multi-bit Trie(区间dp)
举报原因:
原因补充:

(最多只允许输入30个字)