E - Multi-bit Trie

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

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
 //2016.8.6
//区间dp
#include<iostream>
#include<cstdio>
#include<cstring> using namespace std; typedef long long ll;
const int inf = 0x3f3f3f3f;
ll a[], dp[][]; int main()
{
int T, n;
cin>>T;
while(T--)
{
cin>>n;
for(int i = ; i < n; i++)
scanf("%lld", &a[i]); for(int i = ; i < n; i++)
for(int j = ; j < n; j++)
if(j-i<=)
dp[i][j] = a[i]*(<<(j-i+));
else
dp[i][j] = inf; for(int i = ; i < n; i++)
for(int j = ; j < n; j++)
for(int k = i; k < j; k++)
dp[i][j] = min(dp[i][j], dp[i][k]+dp[k+][j]); cout<<dp[][n-]<<endl;
} return ;
}

HDU 4570(区间dp)的更多相关文章

  1. hdu 4283 区间dp

    You Are the One Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  2. HDU 2829 区间DP & 前缀和优化 & 四边形不等式优化

    HDU 2829 区间DP & 前缀和优化 & 四边形不等式优化 n个节点n-1条线性边,炸掉M条边也就是分为m+1个区间 问你各个区间的总策略值最少的炸法 就题目本身而言,中规中矩的 ...

  3. HDU 4293---Groups(区间DP)

    题目链接 http://acm.split.hdu.edu.cn/showproblem.php?pid=4293 Problem Description After the regional con ...

  4. String painter HDU - 2476 -区间DP

    HDU - 2476 思路:分解问题,先考虑从一个空串染色成 B串的最小花费 ,区间DP可以解决这个问题 具体的就是,当 str [ l ] = = str [ r ]时 dp [ L ] [ R ] ...

  5. HDU 4632 区间DP 取模

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4632 注意到任意一个回文子序列收尾两个字符一定是相同的,于是可以区间dp,用dp[i][j]表示原字 ...

  6. 2016 ACM/ICPC Asia Regional Shenyang Online 1009/HDU 5900 区间dp

    QSC and Master Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) ...

  7. hdu 2476 区间dp

    题意: 给出两个串s1和s2,一次只能将一个区间刷一次,问最少几次能让s1=s2 例如zzzzzfzzzzz,长度为11,我们就将下标看做0~10 先将0~10刷一次,变成aaaaaaaaaaa 1~ ...

  8. hdu 4632(区间dp)

    Palindrome subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65535 K (Java/ ...

  9. HDU 5273 区间DP

    输入一组数,m次询问 问每一个询问区间的逆序数有多少 区间DP简单题 #include "stdio.h" #include "string.h" int dp ...

随机推荐

  1. Quick Cocos2dx 与 EnterFrame事件

    利用EnterFrame做出行走的效果,效果图如下: 具体操作: 1 给self多加一个bg1用作与bg无限循环换位 2 在AnotherScene:onEnter方法里面新增onEnterFrame ...

  2. svn + jenkins + maven 实现java环境的自动化构建和部署

    1. 环境说明: 系统CentOS 7 x64 IP:  10.6.0.126 1.1 首先安装配置 svn Centos 7  通过yum 安装svn 既可, 版本为1.7.14 # yum -y ...

  3. iOS探究UITableView的内部代码,仿UITableView自定义

    大家都知道UITableView,最经典在于循环利用,这里我自己模仿UITableView循环利用,写了一套自己的TableView实现方案,希望大家看了我的文章,循环利用思想有显著提升. 研究UIT ...

  4. iOS ZBar扫码简单实现

    导入ZBarSDK文件并引入一下框架 AVFoundation.framework CoreMedia.framework CoreVideo.framework QuartzCore.framewo ...

  5. iOS UIActivityIndicatorView 的使用

    UIActivityIndicatorView 非常简单 ,就是一个转圈圈的控件:http://blog.csdn.net/zhaopenghhhhhh/article/details/1209265 ...

  6. leetcode--014 Gas station

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABGoAAADsCAIAAACjc9eHAAAgAElEQVR4nO3dTa7bRt4v4HczXoH2kS

  7. css 之!important

    主要是自己犯了个错误: 把 !important 放到了分号后面; 正确写法写法: .current{ background-color: #f1f1f1; border-left: 2px soli ...

  8. JQuery中 json 和字符串直接相互转换

    json字符串转json对象:jQuery.parseJSON(jsonStr); json对象转json字符串:JSON.stringify(jsonObj);   IE中可能对unicode使用“ ...

  9. java基础(一)面向对象

    对象就是事物存在的实体,例如:人类,计算机等:而对象被分为两个部分,既动态与静态 类:就是同一事物的统称,如果将世界中的一个事物抽象成对象,类就是这类对象的统称,具有相同特性和行为的一类事物就是类. ...

  10. 安卓 canvas

    [转]http://blog.sina.com.cn/s/blog_61ef49250100qw9x.html(easy) [转]http://blog.csdn.net/rhljiayou/arti ...