CSAPC2008 skyline
一座山的山稜线由许多片段的45度斜坡构成,每一个片段不是上坡就是下坡。
*
* * /\
* /\ /\/ \
/\/ \/ \
在我们眼前的所见的任何宽度为n个单位的山稜形状,可以轻松地观察到所有山顶的位置。
请问有多少种山稜线的形状,使得所有山顶的位置由左而右非递减呢?
所有的山稜线都必须完整,也就是说左右两端都必须是高度为0的山脚,而且不能有任何山谷的位置隐没在地平线底下。
输入说明 :
输入仅包含一个数字n,n一定会是偶数,因为会有相同片段数量的上坡以及下坡。
输出说明 :
请输出山顶位置由左而右非递减的山稜线形状总数。
由于答案可能很大,你只要输出以十进位表示时,它的最后9位数即可。
范例输入 :
6
范例输出 :
4
提示 :
佔总分20%的测试数据中 n<=60
佔总分40%的测试数据中 n<=200
佔总分100%的测试数据中 n<=3000
题解:
动态规划
记dp[i][j]表示长度为i+j,山稜最大高度为j时的方案数,那么它可以由以下两种状态转移过来。
1)dp[i-1][j-1](在原图形的两侧分别加上一条线,如下图)
2)之前已经存在了的最大高度为j的山峰,在左边补上高度比它小的山峰而形成的,如下图
我们将情况2)通过一个前缀和数组sum[i][j]表示(表示长度不超过i+j且高度为j的方案数)
那么状态转移方程即为:dp[i][j]=dp[i-1][j-1]+sum[i-2][j],
sum[i][j]=sum[i-1][j]+dp[i][j]
为了保证合法则必须有(i+j) mod 2=0
那么这题是不是结束了?并没有
在情况2)中,我们直接加上了sum[i-2][j],这样做有什么问题吗?
我们考虑下面的图
对于左面新添加的山稜,为了保证答案的合法,只能在它的旁边加上高度不超过当前最大高度的山稜,即下面的转移是不合法的
但是如果我们只是单纯的加上sum[i-2][j]是无法解决上面所提到的不合法的情况
即:我们在转移完了之后,要减去一部分不合法的情况
这些不合法的情况是:长度小于i-j,高度为j的所有山稜
所以在转移完成之后需要将sum[i-j-j-1][j]减去即可
最后答案:
程序:
#include <stdio.h> #include <iostream> using namespace std; const int maxd=; int dp[][]={}; int sum[][]={}; int main() { int n,i,j,s; dp[][] = ; sum[][] = ; for( i=; i<=; i++ ) { for( j=; j<i; j++ ) { if((i+j)%==) { dp[i][j] = (dp[i-][j-]+sum[i-][j])%maxd; if(i-j-j->=) { dp[i][j] = (dp[i][j]-sum[i-j-j-][j])%maxd; if(dp[i][j]<) dp[i][j]+=maxd; } } sum[i][j] = (sum[i-][j]+dp[i][j])%maxd; } dp[i][i] = ; sum[i][i] = ; } scanf("%d",&n); s = ; for( i=; i<=n/; i++ ) { s = (s+dp[n-i][i])%maxd; } printf("%d\n",s); return ; }
CSAPC2008 skyline的更多相关文章
- [LeetCode] The Skyline Problem 天际线问题
A city's skyline is the outer contour of the silhouette formed by all the buildings in that city whe ...
- UVALive - 4108 SKYLINE[线段树]
UVALive - 4108 SKYLINE Time Limit: 3000MS 64bit IO Format: %lld & %llu Submit Status uDebug ...
- [LeetCode] The Skyline Problem
A city's skyline is the outer contour of the silhouette formed by all the buildings in that city whe ...
- [地图SkyLine二次开发]框架(5)完结篇
上节讲到,将菜单悬浮到地图上面,而且任何操作都不会让地图把菜单盖住. 这节带大家,具体开发一个简单的功能,来了进一步了解,这个框架. 1.想菜单中添加按钮 -上节定义的mainLayout.js文件里 ...
- [地图SkyLine二次开发]框架(2)
上节讲到,地图加载. 但我们可以发现,当没有页面布局的情况下,<OBJECT>控件,没有占满整个屏幕,这里我们就要用到Extjs的功能了. 这节要讲的是用Extjs为<OBJECT& ...
- [地图SkyLine二次开发]框架(1)
项目介绍: 项目是三维地理信息系统的开发,框架MVC4.0 + EF5.0 + Extjs4.2 + SkyLine + Arcgis,是对SkyLine的二次开发. 项目快结束了,先给大家看一眼效果 ...
- Java for LeetCode 218 The Skyline Problem【HARD】
A city's skyline is the outer contour of the silhouette formed by all the buildings in that city whe ...
- The Skyline Problem
A city's skyline is the outer contour of the silhouette formed by all the buildings in that city whe ...
- [LA4108]SKYLINE
[LA4108]SKYLINE 试题描述 The skyline of Singapore as viewed from the Marina Promenade (shown on the left ...
随机推荐
- Leetcode 665. Non-decreasing Array(Easy)
Given an array with n integers, your task is to check if it could become non-decreasing by modifying ...
- 如何让vba与java的TripleDES算法通用
本文链接:http://www.cnblogs.com/Charltsing/p/TripleDES.html 众所周知,java默认采用的TripleDES算法是ECB+PKCS#5填充方式.网上可 ...
- Django 中的Form、ModelForm
一.ModelForm 源码 class ModelForm(BaseModelForm, metaclass=ModelFormMetaclass): pass def modelform_fact ...
- #Leetcode# 1009. Complement of Base 10 Integer
https://leetcode.com/problems/complement-of-base-10-integer/ Every non-negative integer N has a bina ...
- CRM系统设计方案
CRM系统设计方案 - 百度文库https://wenku.baidu.com/view/a34eebeb0242a8956bece473.html 服务支持http://www.uf-crm.com ...
- MySQL 性能调优之存储引擎
原文:http://bbs.landingbj.com/t-0-246222-1.html http://bbs.landingbj.com/t-0-245851-1.html MySQ ...
- cpp11_thread线程
一.进程与线程 cpu一般有m核n线程的说法,那么该cpu只能同时运行n个线程(线程中没有sleep). #include <thread> #include <mutex> ...
- laravel 项目表单中有csrf_token,但一直报错419错误 解决redis连接错误:MISCONF Redis is configured to save RDB snapshots, but it is currently not able to persi
laravel 项目表单中有csrf_token,但一直报错419错误,因为项目中使用到Redis缓存,在强制关闭Redis后出现的问题,查询laravel.log文件查找相关问题 安装redis后在 ...
- 深入python的set和dict
一. collections中的abc 和list(Sequence)相似,都继承于Collection,添加了一些方法 二. dict的常见用法 (setdefault,defaultdict,__ ...
- python学习笔记(9)--函数
函数定义: def <函数名>(<参数(0个或多个)>): 函数体 return <返回值> 参数有非可选参数,和可选参数,可选参数放在参数列表的最后,可以为可选参 ...