CF-14E - Camels(DP)
2013-01-15 13:31
288 查看
E - Camels
Crawling in process...Crawling failedTime
Limit:2000MS Memory Limit:65536KB
64bit IO Format:%I64d & %I64u
SubmitStatusPracticeCodeForces
14E
Description
Bob likes to draw camels: with a single hump, two humps, three humps, etc. He draws a camel by connecting points on a coordinate plane. Now he's drawing camels with
t humps, representing them as polylines in the plane. Each polyline consists of
n vertices with coordinates
(x1, y1),
(x2, y2), ...,
(xn, yn). The first vertex has a coordinate
x1 = 1, the second —
x2 = 2, etc. Coordinates
yi might be any, but should satisfy the following conditions:
there should be t humps precisely, i.e. such indexes
j (2 ≤ j ≤ n - 1), so that
yj - 1 < yj > yj + 1,
there should be precisely t - 1 such indexes
j (2 ≤ j ≤ n - 1), so that
yj - 1 > yj < yj + 1,
no segment of a polyline should be parallel to the Ox-axis,
all yi are integers between 1 and 4.
For a series of his drawings of camels with t humps Bob wants to buy a notebook, but he doesn't know how many pages he will need. Output the amount of different polylines that can be drawn to represent camels with
t humps for a given number
n.
Input
The first line contains a pair of integers n and
t (3 ≤ n ≤ 20,
1 ≤ t ≤ 10).
Output
Output the required amount of camels with t humps.
Sample Input
Input
Output
Input
Output
Sample Output
Hint
In the first sample test sequences of y-coordinates for six camels are: 123421, 123431, 123432, 124321, 134321 и 234321 (each digit corresponds to one value of
yi).
思路:四维DP,d
[t][j][s] n代表点数(步长),t代表尖峰数,j代表高度,s代表上升(0)还是下降(1)。
E(dp[1][1][j][0])=1,用上升峰代表完成一个尖峰。初始值看做一个上升峰。
初始值看成上升峰,那么步长为2就会出现下降峰,但这是不合法的,把它都置0就OK了。
f
[t][i][0]+=f[n-1][t-1][j][1]+f[n-1][t][j][0];///更新上升峰(1:上升峰再加一个上升,2:下降峰加个上升)
f
[t][i][1]+=f[n-1][t][j][1]+f[n-1][t][j][0];///更新下降峰(1:下降峰再加一个下降,2:上升峰加个下降)
失误:比赛的时候想过用DP,但觉得搜索+减枝也行,就直接搜了,后来才发现搜索会爆。。。
Crawling in process...Crawling failedTime
Limit:2000MS Memory Limit:65536KB
64bit IO Format:%I64d & %I64u
SubmitStatusPracticeCodeForces
14E
Description
Bob likes to draw camels: with a single hump, two humps, three humps, etc. He draws a camel by connecting points on a coordinate plane. Now he's drawing camels with
t humps, representing them as polylines in the plane. Each polyline consists of
n vertices with coordinates
(x1, y1),
(x2, y2), ...,
(xn, yn). The first vertex has a coordinate
x1 = 1, the second —
x2 = 2, etc. Coordinates
yi might be any, but should satisfy the following conditions:
there should be t humps precisely, i.e. such indexes
j (2 ≤ j ≤ n - 1), so that
yj - 1 < yj > yj + 1,
there should be precisely t - 1 such indexes
j (2 ≤ j ≤ n - 1), so that
yj - 1 > yj < yj + 1,
no segment of a polyline should be parallel to the Ox-axis,
all yi are integers between 1 and 4.
For a series of his drawings of camels with t humps Bob wants to buy a notebook, but he doesn't know how many pages he will need. Output the amount of different polylines that can be drawn to represent camels with
t humps for a given number
n.
Input
The first line contains a pair of integers n and
t (3 ≤ n ≤ 20,
1 ≤ t ≤ 10).
Output
Output the required amount of camels with t humps.
Sample Input
Input
6 1
Output
6
Input
4 2
Output
0
Sample Output
Hint
In the first sample test sequences of y-coordinates for six camels are: 123421, 123431, 123432, 124321, 134321 и 234321 (each digit corresponds to one value of
yi).
思路:四维DP,d
[t][j][s] n代表点数(步长),t代表尖峰数,j代表高度,s代表上升(0)还是下降(1)。
E(dp[1][1][j][0])=1,用上升峰代表完成一个尖峰。初始值看做一个上升峰。
初始值看成上升峰,那么步长为2就会出现下降峰,但这是不合法的,把它都置0就OK了。
f
[t][i][0]+=f[n-1][t-1][j][1]+f[n-1][t][j][0];///更新上升峰(1:上升峰再加一个上升,2:下降峰加个上升)
f
[t][i][1]+=f[n-1][t][j][1]+f[n-1][t][j][0];///更新下降峰(1:下降峰再加一个下降,2:上升峰加个下降)
失误:比赛的时候想过用DP,但觉得搜索+减枝也行,就直接搜了,后来才发现搜索会爆。。。
#include<iostream> #include<cstring> using namespace std; const int mm=22; int f[mm][mm][mm][2]; int main() { memset(f,0,sizeof(f)); for(int i=1;i<5;i++) f[1][1][i][0]=1;///边界上升尖峰为1 ///0代表上升,1下降,上升时尖峰减1 for(int k=2;k<=20;k++) for(int l=1;l<=10;l++) for(int i=1;i<=4;i++) { for(int j=1;j<i;j++)///更新上升峰(1:上升峰再加一个上升,2:下降峰加个上升) f[k][l][i][0]+=f[k-1][l-1][j][1]+f[k-1][l][j][0]; if(k==2)f[k][l][i][1]=0;///2步不可能的情况 else for(int j=i+1;j<5;j++)///更新下降峰(1:下降峰再加一个下降,2:上升峰加个下降) f[k][l][i][1]+=f[k-1][l][j][1]+f[k-1][l][j][0]; } int n,t; int ans; while(cin>>n>>t) { ans=0; for(int i=1;i<=4;i++) ans+=f [t][i][1]; cout<<ans<<"\n"; } }
相关文章推荐
- CF-14E - Camels(DP)
- CF 675E Trains and Statistic(线段树 + Dp)
- CF round 277 (div2) D题 树形dp
- CF 55D. Beautiful numbers(数位DP)
- CF 148D D Bag of mice (概率dp)
- CF Round #FF (Div. 2) C DZY Loves Sequences_DP
- cf 135div2D Choosing Capital for Treeland 【树形dp】
- CF - 712D 差值dp + 前缀和优化
- CF:322D - Ciel and Duel 贪心 或者 DP 我用的贪心 。。难道sort跟qsort是不一样的么?
- CF D. Kefa and Dishes (状态转移dp)
- 【CF 149D】Coloring Brackets(dp)
- dp cf B. Maximum Submatrix 2
- CF 154 div2 B(dp)
- CF 161D Distance in Tree【树DP】
- CF 461B - Appleman and Tree(树形DP)
- CF 463D Gargari and Permutations [dp]
- [CF803E] Roma and Poker(DP,hash)
- CF 294E - Shaass the Great 树形dp
- CF 811C (DP)
- CF 141 Div2 C Fractal Detector(状态压缩DP)