HDU 4783 Clumsy Algorithm
题意不提。
我们可以发现,可以将最终序列分为对于第i个位置i-pi>=0与i-pi<0种两个子序列。且如果f[n]==g[n],则有两个子序列都递增。
原因是f[n]表示1-n这个排列的逆序对个数,即冒泡排序的交换次数,而每个g[i]表示将p[i]从i位置移到它应当在的p[i]位置的交换次数。
考虑将每个满足i-p[i]>0的p[i]从i位置移到p[i]位置是正确的条件,显然对于i-p[i]>0的每个p[i]必须递增,否则,会产生p[i]与p[j]交换时的交叉,使冒泡的代价增大。
若 i-p[i]<0 的p[i]不递增,它们之间会产生新的冒泡,使冒泡的代价增加。
所以就是DP了,设f[i][j]表示已放了j个数,其中最大数为i的且满足限制的方案数,显然如果j+1的位置放i-p[i]<0的,直接枚举i+1-n的数字即可。
若j+1的位置放i-p[i]>=0的数字,由于i-p[i]>=0的数字必须递增,且i递增,因此有一个必选的数字直接填入即可。
直接转移即可。
#include<bits/stdc++.h>
#define MOD 1000000007
using namespace std;
#define FILE "chad"
set<int> S;
int n, k, f[105][105]; int main()
{
//freopen(FILE".in","r",stdin);
//freopen(FILE".out","w",stdout);
int T; scanf("%d",&T);
for(int tt = 0;T--;)
{
memset(f, 0, sizeof f);
S.clear(); scanf("%d%d",&n,&k);
for(int i = 1; i <= n; i++) S.insert(i);
int mx = 0, flag = 1;
for(int i = 1; i <= k; i++)
{
int x; scanf("%d",&x);
if(x > mx)
{
mx = x; }
else if(x != *S.begin()) flag = 0;
S.erase(x);
}
f[mx][k] = flag;
for(int i = 0; i <= n; i++)
{
for(int j = 0; j < n; j++)
{
if(i-j > 0) (f[i][j+1] += f[i][j]) %= MOD;
for(int k = i+1; k <= n; k++)
(f[k][j+1] += f[i][j]) %= MOD;
}
}
printf("Case #%d: %d\n",++tt,f[n][n]); }
} //代码来自某AC代码,侵删。
HDU 4783 Clumsy Algorithm的更多相关文章
- hdu 1052 (greedy algorithm) 分类: hdoj 2015-06-18 16:49 35人阅读 评论(0) 收藏
thanks to http://acm.hdu.edu.cn/discuss/problem/post/reply.php?action=support&postid=19638&m ...
- hdu 3221 Brute-force Algorithm(高速幂取模,矩阵高速幂求fib)
http://acm.hdu.edu.cn/showproblem.php?pid=3221 一晚上搞出来这么一道题..Mark. 给出这么一个程序.问funny函数调用了多少次. 我们定义数组为所求 ...
- hdu 1051 (greedy algorithm, how a little modification turn 15ms to 0ms) 分类: hdoj 2015-06-18 12:54 29人阅读 评论(0) 收藏
the 2 version are essentially the same, except version 2 search from the larger end, which reduce th ...
- SPOJ 5152 Brute-force Algorithm EXTREME && HDU 3221 Brute-force Algorithm 快速幂,快速求斐波那契数列,欧拉函数,同余 难度:1
5152. Brute-force Algorithm EXTREME Problem code: BFALG Please click here to download a PDF version ...
- HDU 3221 Brute-force Algorithm
题意:问funny被调用了多少次,结果ModP,P不一定为质数. 首先很容易发现递推公式fn=fn-1*fn-2;写出前几项a,b,a*b,a*b^2,a^2*b^3,a^3* ...
- hdu 1053 (huffman coding, greedy algorithm, std::partition, std::priority_queue ) 分类: hdoj 2015-06-18 19:11 22人阅读 评论(0) 收藏
huffman coding, greedy algorithm. std::priority_queue, std::partition, when i use the three commente ...
- hdu, KMP algorithm, linear string search algorithm, a nice reference provided 分类: hdoj 2015-07-18 13:40 144人阅读 评论(0) 收藏
reference: Rabin-Karp and Knuth-Morris-Pratt Algorithms By TheLlama– TopCoder Member https://www.top ...
- hdu, KMP algorithm, linear string search algorithm, a nice reference provided
reference: Rabin-Karp and Knuth-Morris-Pratt Algorithms By TheLlama– TopCoder Member https://www.top ...
- HDU 4889 Scary Path Finding Algorithm
其实这个题是抄的题解啦…… 题解给了一个图,按照那个图模拟一遍大概就能理解了. 题意: 有一段程序,给你一个C值(程序中某常量),让你构造一组数据,使程序输出"doge" 那段代码 ...
随机推荐
- tomcat本身的lib目录都有哪些jar包
1.tomcat下的lib目录,自己带有的jar包有:servlet.jar,tomcat-jdbc.jar,tomncat-dbcp.jar,jsp.jar等 2.tomcat下的lib目录,自己带 ...
- 初窥ElasticSearch
初窥ElasticSearch 官网上面的,不知道讲的是什么.. youtube上面有一个start with,内容是在windows以下跑这个elastic search,然后用一个fidler工具 ...
- 自己动手写ORM(01):解析表达式树生成Sql碎片
在EF中,我们查询数据时可能会用拉姆达表达式 Where(Func<T,ture> func)这个方法来筛选数据,例如,我们定义一个User实体类 public class User { ...
- hdu 1816(二分+2-sat)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1816 思路:首先将每把钥匙i拆成两个点i和i+2n,分别表示选与不选,对于被分成n对的钥匙,由于只能选 ...
- NoSQL-MongoDB with python
前言: MongoDB,文档存储型数据库(document store).NoSQL数据库中,它独占鳌头,碾压其他的NoSQL数据库. 使用C++开发的,性能仅次C.与redis一样,开源.高扩展.高 ...
- 【BZOJ4771】七彩树 主席树+树链的并
[BZOJ4771]七彩树 Description 给定一棵n个点的有根树,编号依次为1到n,其中1号点是根节点.每个节点都被染上了某一种颜色,其中第i个节点的颜色为c[i].如果c[i]=c[j], ...
- tomcat 配置自签名ssl证书
背景:据说17年苹果app必须走https协议与后台交互了,网上各种搜索最后还是发现有2篇写的比较全面,折腾后总结出2种实现方式.在这里记录,方便有共同需求的同学们参考 本文只介绍生成自签名ssl证书 ...
- border inset outset ,border-radius
1. 例子: div{ width:256px; height:256px; border: 10px inset #f00; margin:0 auto; border-radius:255px; ...
- 巨蟒python全栈开发django4:url反向解析图解&&模板渲染
第一部分: 1.(1)知识点回顾: django回顾: ()下载安装 输入网址,a,form表单get post,爬虫 (请求)==>django项目服务端的url(r"index/& ...
- 在Tomcat配置JNDI数据源的三种方式
最近使用到了在tomcat下配置数据源的内容,在这里转载一篇文章记录下 转载自: http://blog.csdn.net/dyllove98/article/details/7706218 在我过去 ...