【刷题】BZOJ 4254 Aerial Tramway
Description
You own a park located on a mountain, which can be described as a sequence of n points (xi, yi) from left to right, where xi,yi>0, xi<xi+1, yi!=yi+1 (that means there will not be horizontal segments in the mountain skyline), illustrated below(the numbers are the corresponding x-coordinate):
You own a park located on a mountain, which can be described as a sequence of n points (xi, yi) from left to right, where xi,yi>0, xi<xi+1, yi!=yi+1 (that means there will not be horizontal segments in the mountain skyline), illustrated below(the numbers are the corresponding x-coordinate): Since the mountain is very sloppy, some aerial tramways across the park would be very helpful. In the figure above, people can go from p4 to p9 directly, by taking a tram. Otherwise he must follow a rather
zigzag path: p4-p5-p6-p7-p8-p9.
Your job is to design an aerial tramway system. There should be exactly m trams, each following a horizontal segment in the air, between two points pi and pj. "Horizontal" means yi=yj, “in the air" means all the points in between are strictly below, i.e. yk<yi for every i<k<j. For example, no tram can travel between p2 and p9, because p4 is not strictly below p2-p9. However, you can have two trams, one
from p2 to p4, and one p4 to p9. There is another important restriction: no point can be strictly below k or more tramways, because it’ll be dangerous. For example, if k=3, we cannot build these 3 tramways simultaneously: p1-p14, p4-p9, p6-p8, because p7 would be dangerous. You want to make this system as useful as possible, so you would like to maximize the total length of all tramways. For example, if m=3, k=3, the best design for the figure above is p1-p14, p2-p4 and p4-p9,the total length is 20. If m=3, k=2, you have to replace p1-p14 with p11-p13, the total length becomes 9.
Input
There will be at most 200 test cases. Each case begins with three integers n, m and k(1<=n,m<=200, 2<=k<=10), the number of points, the number of trams in your design and the dangerous parameter introduced earlier. The next line contains n pairs of positive integers xi and yi.(1<=xi,yi<=10^5).
Output
For each test case, print the case number and the maximal sum. If it is impossible to have exactly m tramways, print -1.
Sample Input
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
Sample Output
Case 1: 20
Case 2: 9
HINT
2015年湖南省大学生程序设计竞赛
Solution
我们把所有可以建的索道当做一个个区间
会发现这些区间之间要么互不相关,要么互相包含
于是就可以将这些区间建成一棵树,我们要做的就是在这棵树里选择正好 \(m\) 个点,使得点权和最大,并且对于每一条深度递增的链,链上选的点必须小于 \(k\) 个
这个树形dp一下就好了,设 \(f_{i,j,p}\) 代表到树上 \(i\) 点,其子树中选 \(j\) 个,深度递增的链中选的最多的链选的个数为 \(k\)
转移方程为 \(f_{u,j+r,max(p,t)}=\max\{f_{u,j,p}+f_{v,r,t}\}\)
用前缀最大值省去最后第三维的枚举
就可以过了
#include<bits/stdc++.h>
#define ui unsigned int
#define ll long long
#define db double
#define ld long double
#define ull unsigned long long
const int MAXN=200+10,MAXP=100000+10,MAXK=20+10,inf=0x3f3f3f3f;
int n,m,k,e,beg[MAXN],nex[MAXN<<1],to[MAXN<<1],cnt,fa[MAXN],val[MAXN],f[MAXN][MAXN][MAXK],g[MAXN][MAXK],size[MAXN],ans;
struct node{
int id,x,y;
inline bool operator < (const node &A) const {
return x<A.x;
};
};
node mountain[MAXN];
std::vector<node> V[MAXP];
struct interval{
int l,r;
inline bool operator < (const interval &A) const {
return r-l>A.r-A.l;
};
};
interval line[MAXN];
template<typename T> inline void read(T &x)
{
T data=0,w=1;
char ch=0;
while(ch!='-'&&(ch<'0'||ch>'9'))ch=getchar();
if(ch=='-')w=-1,ch=getchar();
while(ch>='0'&&ch<='9')data=((T)data<<3)+((T)data<<1)+(ch^'0'),ch=getchar();
x=data*w;
}
template<typename T> inline void write(T x,char ch='\0')
{
if(x<0)putchar('-'),x=-x;
if(x>9)write(x/10);
putchar(x%10+'0');
if(ch!='\0')putchar(ch);
}
template<typename T> inline void chkmin(T &x,T y){x=(y<x?y:x);}
template<typename T> inline void chkmax(T &x,T y){x=(y>x?y:x);}
template<typename T> inline T min(T x,T y){return x<y?x:y;}
template<typename T> inline T max(T x,T y){return x>y?x:y;}
inline void insert(int x,int y)
{
to[++e]=y;
nex[e]=beg[x];
beg[x]=e;
}
inline void dfs(int x)
{
for(register int i=0;i<=m;++i)
for(register int j=0;j<k;++j)f[x][i][j]=-inf;
f[x][0][0]=0;
for(register int i=beg[x];i;i=nex[i])
if(to[i]==fa[x])continue;
else
{
dfs(to[i]);
static int q1,q2;
for(register int j=0;j<=size[x]+size[to[i]];++j)
for(register int p=0;p<k;++p)g[j][p]=f[x][j][p];
for(register int j=0;j<=size[x];++j)
for(register int p=0;p<=min(size[to[i]],m-j);++p)
{
q1=-inf,q2=-inf;
for(register int t=0;t<k;++t)
{
chkmax(q1,f[x][j][t]),chkmax(q2,f[to[i]][p][t]);
chkmax(g[j+p][t],f[x][j][t]+q2);
chkmax(g[j+p][t],f[to[i]][p][t]+q1);
}
}
size[x]+=size[to[i]];
for(register int j=0;j<=min(m,size[x]);++j)
for(register int p=0;p<k;++p)f[x][j][p]=g[j][p];
}
if(x)
{
size[x]++;
for(register int i=min(size[x],m)-1;i>=0;--i)
for(register int j=k-2;j>=0;--j)chkmax(f[x][i+1][j+1],f[x][i][j]+val[x]);
}
}
int main()
{
static int cases=0;
while(scanf("%d%d%d",&n,&m,&k)!=EOF)
{
for(register int i=1;i<=1e5;++i)V[i].clear();
e=0;memset(beg,0,sizeof(beg));cnt=0;ans=-1;
memset(size,0,sizeof(size));
memset(fa,0,sizeof(fa));
for(register int i=1;i<=n;++i)
{
int x,y;read(x);read(y);
mountain[i]=(node){i,x,y};
}
std::sort(mountain+1,mountain+n+1);
for(register int i=1;i<=n;++i)V[mountain[i].y].push_back(mountain[i]);
for(register int i=1;i<=1e5;++i)
for(register int j=1,lt=V[i].size();j<lt;++j)
{
int l=V[i][j-1].id+1,r=V[i][j].id-1,nowh=0;
for(register int k=l;k<=r;++k)chkmax(nowh,mountain[k].y);
if(nowh<i)line[++cnt]=(interval){V[i][j-1].x,V[i][j].x};
}
std::sort(line+1,line+cnt+1);
for(register int i=1;i<=cnt;++i)
for(register int j=i-1;j>=1;--j)
if(line[j].l<=line[i].l&&line[i].r<=line[j].r)
{
insert(i,j),insert(j,i);
fa[i]=j;break;
}
for(register int i=1;i<=cnt;++i)
{
val[i]=line[i].r-line[i].l;
if(!fa[i])insert(0,i),insert(i,0);
}
fa[0]=-1;dfs(0);
for(register int i=0;i<k;++i)chkmax(ans,f[0][m][i]);
printf("Case %d: %d\n",++cases,ans);
}
return 0;
}
【刷题】BZOJ 4254 Aerial Tramway的更多相关文章
- 【刷题】BZOJ 2407 探险
Description 探险家小T好高兴!X国要举办一次溶洞探险比赛,获奖者将得到丰厚奖品哦!小T虽然对奖品不感兴趣,但是这个大振名声的机会当然不能错过! 比赛即将开始,工作人员说明了这次比赛的规则: ...
- 【刷题】BZOJ 4543 [POI2014]Hotel加强版
Description 同OJ3522 数据范围:n<=100000 Solution dp的设计见[刷题]BZOJ 3522 [Poi2014]Hotel 然后发现dp的第二维与深度有关,于是 ...
- 【刷题】BZOJ 4316 小C的独立集
Description 图论小王子小C经常虐菜,特别是在图论方面,经常把小D虐得很惨很惨. 这不,小C让小D去求一个无向图的最大独立集,通俗地讲就是:在无向图中选出若干个点,这些点互相没有边连接,并使 ...
- 【刷题】BZOJ 4176 Lucas的数论
Description 去年的Lucas非常喜欢数论题,但是一年以后的Lucas却不那么喜欢了. 在整理以前的试题时,发现了这样一道题目"求Sigma(f(i)),其中1<=i< ...
- BZOJ第一页刷题计划
BZOJ第一页刷题计划 已完成:67 / 90 [BZOJ1000]A+B Problem:A+B: [BZOJ1001][BeiJing2006]狼抓兔子:最小割: [BZOJ1002][FJOI2 ...
- 【刷题】BZOJ 2260 商店购物
Description Grant是一个个体户老板,他经营的小店因为其丰富的优惠方案深受附近居民的青睐,生意红火.小店的优惠方案十分简单有趣.Grant规定:在一次消费过程中,如果您在本店购买了精制油 ...
- 【刷题】BZOJ 4566 [Haoi2016]找相同字符
Description 给定两个字符串,求出在两个字符串中各取出一个子串使得这两个子串相同的方案数.两个方案不同当且仅当这两个子串中有一个位置不同. Input 两行,两个字符串s1,s2,长度分别为 ...
- 【刷题】BZOJ 3365 [Usaco2004 Feb]Distance Statistics 路程统计
Description 在得知了自己农场的完整地图后(地图形式如前三题所述),约翰又有了新的问题.他提供 一个整数K(1≤K≤109),希望你输出有多少对农场之间的距离是不超过K的. Input 第1 ...
- 【刷题】BZOJ 2001 [Hnoi2010]City 城市建设
Description PS国是一个拥有诸多城市的大国,国王Louis为城市的交通建设可谓绞尽脑汁.Louis可以在某些城市之间修建道路,在不同的城市之间修建道路需要不同的花费.Louis希望建造最少 ...
随机推荐
- DM9161 和 STM32F107 和 FreeRTOS 和 LWIP
1.首先移植 FreeRTOS到 107上 可以正常运行. 可以到群下载移植好的,文件名称:STM32F107VC+FreeRTOS V8.2.3+kfifo(巧夺天工)! 2.第二步 :ST 官方 ...
- 20155209 Exp5 MSF基础应用
Exp5 MSF基础应用 实验准备 在实验之前,上网搜集了很多有关Metasploit渗透测试的资料.对这次实验影响最大的是一篇最受欢迎的10个Metasploit模块和插件.排名第一位的是MSB-M ...
- 20155229《网络对抗技术》Exp2:后门原理与实践
实验预习 后门: 指绕过安全控制而获取对程序或系统访问权的方法.最主要目的就是方便以后再次秘密进入或者控制系统. 木马与后门的区别: 木马:通过欺骗用户的方法(包含捆绑,利用网页等)让用户不知不觉的安 ...
- Keras实现风格迁移
风格迁移 风格迁移算法经历多次定义和更新,现在应用在许多智能手机APP上. 风格迁移在保留目标图片内容的基础上,将图片风格引用在目标图片上. 风格本质上是指在各种空间尺度上图像中的纹理,颜色和视觉图案 ...
- elasticsearch同步mongodb--mongo connector的使用
部署准备 python-3.6.4-amd64.exe mongodb-win32-x86_64-3.4.6-signed.msi (如果已经安装可以忽略) 注意点! 之前我写的一篇文章用的是ela ...
- Js_特效II
字号缩放 让文字大点,让更多的用户看的更清楚.(也可以把字体变为百分比来实现)<script type="text/javascript"> function doZ ...
- 对html第一次尝试
1.对于写文档 修改后缀为html,双击进入为网页模式. 2.编写网页 1)新建 2)基本格式 <!DOCTYPE html><!-- ...
- Unity Inspector添加自定义按钮(Button)
在Unity开发游戏的时候,为了有一个更快更方便的工作流,我们往往会在Editor下开发一些方便实用的工具.在工具中,用到最多,最关键的就是按钮,它是工具的首席执行官.下面就用最简单的代码来演示添加一 ...
- 利用可道云kodexplorer在树莓派raspbian上搭建私有云网盘
可道云kodexplorer是一款开源私有云系统,类似于owncloud,Dropbox.SkyDrive,seafile等.将可道云kodexplorer搭建在树莓派上,从而在树莓派上存储.管理家庭 ...
- linux 操作 mysql 指定端口登录 以及启动 停止
linux 操作 mysql 指定端口登录 mysql -uroot -p -h10.154.0.43 -P3341 1.查看mysql版本方法一:status;方法二:select version( ...