POJ 1221 UNIMODAL PALINDROMIC DECOMPOSITIONS
总时间限制: 1000ms 内存限制: 65536kB
描述
A sequence of positive integers is Palindromic if it reads the same forward and backward. For example:
23 11 15 1 37 37 1 15 11 23
1 1 2 3 4 7 7 10 7 7 4 3 2 1 1
A Palindromic sequence is Unimodal Palindromic if the values do not decrease up to the middle value and then (since the sequence is palindromic) do not increase from the middle to the end For example, the first example sequence above is NOT Unimodal Palindromic while the second example is.
A Unimodal Palindromic sequence is a Unimodal Palindromic Decomposition of an integer N, if the sum of the integers in the sequence is N. For example, all of the Unimodal Palindromic Decompositions of the first few integers are given below:
1: (1)
2: (2), (1 1)
3: (3), (1 1 1)
4: (4), (1 2 1), (2 2), (1 1 1 1)
5: (5), (1 3 1), (1 1 1 1 1)
6: (6), (1 4 1), (2 2 2), (1 1 2 1 1), (3 3),
(1 2 2 1), ( 1 1 1 1 1 1)
7: (7), (1 5 1), (2 3 2), (1 1 3 1 1), (1 1 1 1 1 1 1)
8: (8), (1 6 1), (2 4 2), (1 1 4 1 1), (1 2 2 2 1),
(1 1 1 2 1 1 1), ( 4 4), (1 3 3 1), (2 2 2 2),
(1 1 2 2 1 1), (1 1 1 1 1 1 1 1)
Write a program, which computes the number of Unimodal Palindromic Decompositions of an integer.
输入
Input consists of a sequence of positive integers, one per line ending with a 0 (zero) indicating the end.
输出
For each input value except the last, the output is a line containing the input value followed by a space, then the number of Unimodal Palindromic Decompositions of the input value. See the example on the next page.
样例输入
样例输出
提示
N < 250
解题思路
刚刚入门动规,果然踩坑了,这道题写了一个小时,调了一个小时,不过也算是独立完成的第一道动规题(虽然写着写着就写成递归了orz),纪念一下。
子问题划分见代码,每次只进行一次分解,比如将8分解为(4,4)或者(1,6,1)、(2,4,2)。
奇数情况进入下一次迭代,偶数情况则终止迭代,比如8中的(4,4)不用再次迭代,(2,2,2,2)的情况在(2,4,2)中会被考虑到。总而言之,分解之后中间有数就迭代,中间没数了就完成使命了。
因为要满足单峰条件,所以进入子问题之后要告知上一位数字以进行比较。
需要注意的问题和技巧
TLE:太多冗余的计算了,开一个二维upd数组记录结果,避免重复迭代和循环。
WA:N<250,还是超了int的范围,要用unsigned int。
AC代码
#include<iostream>
#include<cstring>
using namespace std; unsigned int upd[][];//保存结果,防止重复计算 int UPD(int s,int pre)//pre是当前位前一位的数字
{
if (upd[s][pre]) return upd[s][pre];
unsigned int cnt = ;
cnt++;//它自己也算是单峰
for (int j = ; j <= s / ; j++)
{
if (s - * j == && j >= pre) cnt++;
if ((s - * j) >= j && j >= pre) cnt += UPD(s - * j, j);
}
upd[s][pre] = cnt;
return cnt;
} int main()
{
int sum;
memset(upd, , sizeof(upd));
while (cin >> sum)
{
if (sum == )break;
unsigned int n_ = UPD(sum, );
cout << sum << " " << n_ << endl;
}
return ;
}
POJ 1221 UNIMODAL PALINDROMIC DECOMPOSITIONS的更多相关文章
- poj 1221 UNIMODAL PALINDROMIC DECOMPOSITIONS (母函数)
/* 给出一个数n,把它拆分成若干个数的和,要求最大的数在中间并向两边非递增.问拆法有多少种. 母函数.枚举中间的那一个数.由于左右对称.所以仅仅须要求左边部分的方案就可以. 注意,左右两部分的取数必 ...
- poj 3790 Recursively Palindromic Partitions
/*摘抄自博客:Recursively Palindromic Partitions Time Limit: 1000MS Memory Limit: 65536K Total Submissions ...
- poj 3790 Recursively Palindromic Partitions (递推)
题目 题意:求输入的数字的递归回文. 思路:答案等于这个数字一半之前的所有的 之和. #include <iostream> #include <cstdio> #includ ...
- POJ 1221
#include <iostream> #define MAXN 500 using namespace std; unsigned dp[MAXN][MAXN]; int main() ...
- POJ题目细究
acm之pku题目分类 对ACM有兴趣的同学们可以看看 DP: 1011 NTA 简单题 1013 Great Equipment 简单题 102 ...
- 【转】POJ百道水题列表
以下是poj百道水题,新手可以考虑从这里刷起 搜索1002 Fire Net1004 Anagrams by Stack1005 Jugs1008 Gnome Tetravex1091 Knight ...
- ACM/ICPC 之 DP-整数划分问题初探 (POJ1221)
写下这道题的原因很简单= =,因为这一题的状态转移方程不好找,另一方面,我看到很多针对这一题写的解题报告都把累加状态说得模棱两可,甚至直接说成了一个单一状态,弄得本是菜鸟的我硬生生折磨了一上午画了几个 ...
- POJ1221(整数划分)
UNIMODAL PALINDROMIC DECOMPOSITIONS Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 543 ...
- POJ 2823 UESTCoj 1221 Sliding Window 单调队列 经典入门题
题意:给出一个序列,求出每连续k个数字中最大的数和最小的数. 这是道单调队列裸题,直接写就行了. 本来用deque写出来后,发现在poj上硬是超时了,在discuss上看很多人也在抱怨超时的问题,据说 ...
随机推荐
- 大数据之路week07--day07 (Hive结构设计以及Hive语法)
Hive架构流程(十分重要,结合图进行记忆理解)当客户端提交请求,它先提交到Driver,Driver拿到这个请求后,先把表明,字段名拿出来,去数据库进行元数据验证,也就是Metasore,如果有,返 ...
- HDU4091:Zombie’s Treasure Chest (分类-数学)
题意:给两种宝石,体积S1,S2,价值V1,V2,背包容量n,求最大收益. 所有数据都在32位整数范围内. 思路:只有两种物品的背包,显然不是常见的背包,应该从背包之外的思路下手. 1:可以猜想其中一 ...
- Kafaka 总结
Kafka是一个分布式的Streaming处理平台,Kafka可以用于数据库中数据的导入导出,也可以用于实时流的处理,但是Kafka最核心的功能就是作为分布式的消息中间件. Kafka集群是由多个Br ...
- UFUN函数 UF_ATTR函数(UF_ATTR_assign ,UF_ATTR_read_value )
UF_initialize(); tag_t ; ]="零件名称"; UF_ATTR_value_t value; value.type=UF_ATTR_string; value ...
- MySQL limit 分页查询优化(百万级优化)
1)简单的查询分页:分每页5条 limit [offset],[rows] ,10; 2)建立id索引:查询索引id ,) limit ; 3)使用 between and 语句分页效率快N倍 ; 4 ...
- 石子合并(NOI1995)题解
题目描述 在一个圆形操场的四周摆放N堆石子,现要将石子有次序地合并成一堆.规定每次只能选相邻的2堆合并成新的一堆,并将新的一堆的石子数,记为该次合并的得分. 试设计出1个算法,计算出将N堆石子合并成1 ...
- 51Nod 1769 Clarke and math2
51Nod 1769 Clarke and math2 http://www.51nod.com/Challenge/Problem.html#!#problemId=1769 要算的是\(G=F*I ...
- ARC093F Dark Horse 【容斥,状压dp】
题目链接:gfoj 神仙计数题. 可以转化为求\(p_1,p_2,\ldots,p_{2^n}\),使得\(b_i=\min\limits_{j=2^i+1}^{2^{i+1}}p_j\)都不属于\( ...
- 框架 get 请求乱码
解决方案: 在 tomcat 配置文件中添加 URIEncoding="utf-8"
- koa 搭建模块化路由/层级路由
搭建node项目目录以及基本的文件 初始化package.json文件 执行下面命令生成package.json文件 npm init --yes 创建项目目录 创建路由目录routes,存放静态资源 ...