【BZOJ4254】Aerial Tramway 树形DP
【BZOJ4254】Aerial Tramway
题意:给你一座山上n点的坐标,让你在山里建m条缆车,要求缆车两端的高度必须相等,且中间经过的点的高度都小于缆车的高度。并且不能存在一个点位于至少k条缆车的下方。求缆车的最大总长度。
n,m<=200,k<=10。
题解:这么神奇的题面居然有人能想到要用树形DP。。。
先枚举所有可能的缆车,然后暴力得出这些缆车的关系。因为上面的缆车一定比它下面的缆车长,所以这形成了一个树形结构,我们建树跑树形DP。
用f[x][a][b]表示x的子树中已经减了y个缆车,且一个点最多位于k条缆车下方,的最大总长度。转移时是惯用的树形背包套路,然后用前缀最大值优化一下即可。时间复杂度O(n*m*k)。
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;
int T,n,m,K,tot,ans,cas;
int x[210],y[210],v[210],bel[210],siz[210],g[210][15],f[210][210][15],d[210];
vector<int> ch[210];
inline int rd()
{
int ret=0,f=1; char gc=getchar();
while(gc<'0'||gc>'9') {if(gc=='-') f=-f; gc=getchar();}
while(gc>='0'&&gc<='9') ret=ret*10+gc-'0',gc=getchar();
return ret*f;
}
void dfs(int x)
{
siz[x]=1;
int i,j,k,l,y;
for(l=0;l<=K;l++) f[x][0][l]=0;
for(i=0;i<(int)ch[x].size();i++)
{
y=ch[x][i],dfs(y);
memcpy(g,f[x],sizeof(f[x]));
for(j=0;j<=siz[x]&&j<=m;j++) for(k=0;k<=siz[y]&&j+k<=m;k++) for(l=0;l<=K;l++)
g[j+k][l]=max(g[j+k][l],f[x][j][l]+f[y][k][l]);
siz[x]+=siz[y];
for(j=0;j<=siz[x]&&j<=m;j++) for(l=1;l<=K;l++) f[x][j][l]=max(g[j][l],f[x][j][l-1]);
}
for(j=min(m,siz[x]);j>=1;j--) for(l=1;l<=K;l++)
{
f[x][j][l]=max(f[x][j][l],f[x][j-1][l-1]+v[x]);
f[x][j][l]=max(f[x][j][l],f[x][j][l-1]);
}
ans=max(ans,f[x][m][K]);
}
void work()
{
tot=0,K--;
int i,j;
for(i=1;i<=n;i++) x[i]=rd(),y[i]=rd(),ch[i].clear(),bel[i]=0;
for(i=1;i<=n;i++)
{
for(j=i-1;j>=1;j--)
{
if(y[j]>y[i]) break;
if(y[j]==y[i])
{
bel[i]=++tot,v[tot]=x[i]-x[j],d[tot]=0;
break;
}
}
if(y[j]==y[i]&&bel[i]) for(j++;j<i;j++) if(y[j]<y[i]&&bel[j]&&!d[bel[j]])
d[bel[j]]=1,ch[bel[i]].push_back(bel[j]);
}
memset(f,0xc0,sizeof(f));
v[++tot]=-1<<20;
for(i=1;i<tot;i++) if(!d[i]) ch[tot].push_back(i);
ans=-1,dfs(tot);
printf("%d\n",ans);
}
int main()
{
while(scanf("%d%d%d",&n,&m,&K)!=EOF)
{
printf("Case %d: ",++cas);
work();
}
return 0;
}//14 3 3 1 8 2 6 3 4 4 6 5 3 6 4 7 1 8 4 9 6 10 4 11 6 12 5 13 6 14 8 14 3 2 1 8 2 6 3 4 4 6 5 3 6 4 7 1 8 4 9 6 10 4 11 6 12 5 13 6 14 8
【BZOJ4254】Aerial Tramway 树形DP的更多相关文章
- BZOJ4254 : Aerial Tramway
可以修建的缆车总数不超过n,于是可以先通过$O(n^2)$的枚举求出所有可以修建的缆车. 对于一个缆车,若它仅连接i和i+1,那么它不受k的限制,把这种缆车额外取出,从大到小排序. 剩下的缆车两两之间 ...
- 【刷题】BZOJ 4254 Aerial Tramway
Description You own a park located on a mountain, which can be described as a sequence of n points ( ...
- poj3417 LCA + 树形dp
Network Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 4478 Accepted: 1292 Descripti ...
- COGS 2532. [HZOI 2016]树之美 树形dp
可以发现这道题的数据范围有些奇怪,为毛n辣么大,而k只有10 我们从树形dp的角度来考虑这个问题. 如果我们设f[x][k]表示与x距离为k的点的数量,那么我们可以O(1)回答一个询问 可是这样的话d ...
- 【BZOJ-4726】Sabota? 树形DP
4726: [POI2017]Sabota? Time Limit: 20 Sec Memory Limit: 128 MBSec Special JudgeSubmit: 128 Solved ...
- 树形DP+DFS序+树状数组 HDOJ 5293 Tree chain problem(树链问题)
题目链接 题意: 有n个点的一棵树.其中树上有m条已知的链,每条链有一个权值.从中选出任意个不相交的链使得链的权值和最大. 思路: 树形DP.设dp[i]表示i的子树下的最优权值和,sum[i]表示不 ...
- 树形DP
切题ing!!!!! HDU 2196 Anniversary party 经典树形DP,以前写的太搓了,终于学会简单写法了.... #include <iostream> #inclu ...
- BZOJ 2286 消耗战 (虚树+树形DP)
给出一个n节点的无向树,每条边都有一个边权,给出m个询问,每个询问询问ki个点,问切掉一些边后使得这些顶点无法与顶点1连接.最少的边权和是多少.(n<=250000,sigma(ki)<= ...
- POJ2342 树形dp
原题:http://poj.org/problem?id=2342 树形dp入门题. 我们让dp[i][0]表示第i个人不去,dp[i][1]表示第i个人去 ,根据题意我们可以很容易的得到如下递推公式 ...
随机推荐
- 使用while循环和伪列的存储过程
使用while循环和伪列的存储过程如下: USE [JointFrame2] GO /****** Object: StoredProcedure [dbo].[Proc_enterprise_uni ...
- [Informix] unload load
select tabname from systables where tabname like 'aa%' select * from syscolumns where tabname like ...
- 线程相关函数(2)-pthread_self()获取调用线程ID
获取调用线程tid #include <pthread.h>pthread_t pthread_self(void); 示例: #include <pthread.h> #in ...
- SCWS 中文分词
SCWS 中文分词v1.2.3 开源免费的中文分词系统,PHP分词的上乘之选! 首页 下载 演示 文档 关于 服务&支持 API/HTTP 论坛 捐赠 源码@github 文档目录 SCWS- ...
- rip中的连续子网以及不连续子网
RIPv1 RIPv2 距离矢量2 距离矢量 最大跳计数15 最大跳计数15 有类的 无类的 基于广播的 基于组播224.0.09 不支持VLSM 支持VLSM 无认证 允许MD5认证 不支持不 ...
- 用swift开发仪表盘控件(二)
二.代码分析 这个控件本质就是从UIView继承的一个类而已.所以整个代码事实上就是一个定制的UIView类. 依据UIView的规则进行例如以下初始化: required init(coder aD ...
- post 获取checkbox值
$str_tag=""; $s=$_POST['goods_server_name']; for($i=0;$i<count($s);$i++){ if($i==0){ $s ...
- C++ 引用本质的详解
//引用本质的理解① #include<iostream> using namespace std; int GetA(){ ; return a; } int & GetB(){ ...
- The Properties of Posterior of Topic Model
1.Tang, Jian, et al. "Understanding the Limiting Factors of Topic Modeling via Posterior Contra ...
- git error Another git process seems to be running in this repository
How to fix error Another git process seems to be running in this repository When you use Git, you se ...