【动态规划】【记忆化搜索】【搜索】CODEVS 1262 不要把球传我 2012年CCC加拿大高中生信息学奥赛

可以暴力递归求解,应该不会TLE,但是我们考虑记忆化优化。

设f(i,j)表示第i个数为j时的方案数。

f(i,j)=f(1,j-1)+f(2,j-1)+……+f(i-1,j-1) (4>=j>=1),从f(n,4)开始递归求解就行。

但是考虑到状态最多只有n*4种,所以记忆化掉吧。

初始化:f(i,1)=1 (1<=i<=n-3)

 1 #include<cstdio>
 2 using namespace std;
 3 int n;
 4 long long memory[5][101];
 5 long long f(int cur,int now)
 6 {
 7     if(now==0||cur<now) return 0;
 8     if(memory[now][cur]) return memory[now][cur];
 9     long long res=0;
10     for(int i=1;i<cur;i++) res+=f(i,now-1);
11     return memory[now][cur]=res;
12 }
13 int main()
14 {
15     scanf("%d",&n);
16     for(int i=1;i<=n-3;i++) memory[1][i]=1;
17     printf("%lld\n",f(n,4));
18     return 0;
19 }
时间: 2024-08-28 06:35:08

【动态规划】【记忆化搜索】【搜索】CODEVS 1262 不要把球传我 2012年CCC加拿大高中生信息学奥赛的相关文章

codevs 1277 生活大爆炸 2012年CCC加拿大高中生信息学奥赛

时间限制: 1 s 空间限制: 128000 KB 题目等级 : 白银 Silver 题目描述 Description Sheldon and Leonard are physicists who are ?xated on the BIG BANG theory. In order to exchange secret insights they have devised a code that encodes UPPERCASE words by shifting their letters

codevs 1262 不要把球传我

我们考虑在1...n-1中选取三个数不重复的有多少种?答案是(n-1)*(n-2)*(n-3)种. 每三个数组成六种序列,只有一种单增. #include<iostream>#include<cstdio>using namespace std;int n;int main(){ scanf("%d",&n); printf("%d\n",(n-3)*(n-1)*(n-2)/6); return 0;}

sicily 1176. Two Ends (Top-down 动态规划+记忆化搜索 v.s. Bottom-up 动态规划)

DescriptionIn the two-player game "Two Ends", an even number of cards is laid out in a row. On each card, face up, is written a positive integer. Players take turns removing a card from either end of the row and placing the card in their pile. T

Codevs_1017_乘积最大_(划分型动态规划/记忆化搜索)

描述 http://codevs.cn/problem/1017/ 给出一个n位数,在数字中间添加k个乘号,使得最终的乘积最大. 1017 乘积最大 2000年NOIP全国联赛普及组NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 今年是国际数学联盟确定的“2000——世界数学年”,又恰逢我国著名数学家华罗庚先生诞辰90周年.在华罗庚先生的家乡江苏金坛,组织了一场别开生面的数学智力竞赛的活动,你的一个好朋友

动态规划①——记忆化搜索

首先的首先,必须明白动态规划(DP)以后很有用很有用很有用很有用……首先的其次,必须明白:动规≍搜索=枚举 一.最简单的记忆化搜索(应该可以算DP) 题目(来自洛谷OJ)http://www.luogu.org/problem/show?pid=1434# [不麻烦大家自己找了]题目描述 DescriptionMichael喜欢滑雪.这并不奇怪,因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你.Michael想知道在一个区域中最

UVA_437_The_Tower_of_the_Babylon_(DAG上动态规划/记忆化搜索)

描述 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=378 n种方块,给出每一种的长宽高,现在要落起来,上面的方块的长和宽要严格小于下面的方块,问最多落多高. ACM Contest Problems ArchiveUniversity of Valladolid (SPAIN)437 The Tower of BabylonPerhap

动态规划-记忆化搜索

1.数字三角形 学习链接:http://blog.csdn.net/zwhlxl/article/details/46225947 输入样例: 5 7 3 8 8 1 0 2 7 4 4 4 5 2 6 5 输出样例: 30 递归代码: #include <stdio.h> #include <memory.h> #include <math.h> #include <string> #include <vector> #include <

着色方案(动态规划+记忆化搜索)

题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1079 AC代码: 1 /* 2 直接状态压缩是显然是不可行的,我们考虑如果没有相邻颜色不相同的限制的话, 3 如果两种油漆能染的木块数目相同,我们就可以认为两种油漆无差别. 4 设dp[a1][a2][a3][a4][a5]为能染1个木块的油漆有a1种……的方案数. 5 但是有相邻颜色的限制,如果上一次用了颜色数为k的油漆, 6 那么这一次有一种颜色数为k-1的油漆就不能用了,转移的时

HDU 2364 (记忆化BFS搜索)

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2364 题目大意:走迷宫.从某个方向进入某点,优先走左或是右.如果左右都走不通,再考虑向前.绝对不能往后走,即使没走过. 解题思路: 还是一个关键: 每个点可以最多可以走4遍.可以从4个方向到达这个点.所以vis第三维要记录走的方向. 辅助一个route数组,用于当前方向时,应该走相对于正北坐标系的方向(即人看地图的上下左右). 这样就算迷宫中人的方向不同,但是我们给他标记的数却是统一的,都是以他的