题意不提。

我们可以发现,可以将最终序列分为对于第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的更多相关文章

  1. 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 ...

  2. hdu 3221 Brute-force Algorithm(高速幂取模,矩阵高速幂求fib)

    http://acm.hdu.edu.cn/showproblem.php?pid=3221 一晚上搞出来这么一道题..Mark. 给出这么一个程序.问funny函数调用了多少次. 我们定义数组为所求 ...

  3. 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 ...

  4. 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 ...

  5. 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* ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. HDU 4889 Scary Path Finding Algorithm

    其实这个题是抄的题解啦…… 题解给了一个图,按照那个图模拟一遍大概就能理解了. 题意: 有一段程序,给你一个C值(程序中某常量),让你构造一组数据,使程序输出"doge" 那段代码 ...

随机推荐

  1. diamond源码阅读-diamond-client

    读取数据 DiamondManager manager = new DefaultDiamondManager("DEFAULT_GROUP", "zml", ...

  2. js事件绑定的方法

    废话不多少,直接上代码 第一种 <body> <div style="width:400px;height:400px;background:blueviolet" ...

  3. 从设计到实现,一步步教你实现Android-Universal-ImageLoader-缓存

    转载请标明出处,本文出自:chaossss的博客 Android-Universal-ImageLoader Github 地址 Cache 我们要对图片进行缓存.有两种方式:内存缓存和本地缓存. 这 ...

  4. Android开发:《Gradle Recipes for Android》阅读笔记1.7——仓库配置

    repositories块告诉gradle哪里去寻找依赖,默认的android studio使用jcenter或者mavenCentral.jcenter仓库位于https://jcenter.bin ...

  5. convex hull trick CF344.E

    类似于斜率优化的东西,果真CF的E以后才会考点算法啊. 感觉这种优化应该很常见,但这题直线只有第一象限的,但是插入,和查找操作是不变的,按极角排序后就可以直接用这个模板了. #include < ...

  6. 【BZOJ3158】千钧一发 最小割

    [BZOJ3158]千钧一发 Description Input 第一行一个正整数N. 第二行共包括N个正整数,第 个正整数表示Ai. 第三行共包括N个正整数,第 个正整数表示Bi. Output 共 ...

  7. django database relations

    注意Django的生成的默认api from django.db import models class Place(models.Model): ''' pass class Restaurant( ...

  8. Martin Fowler’s Active Record design pattern.

    P of EAA: Active Record https://www.martinfowler.com/eaaCatalog/activeRecord.html Active Record An o ...

  9. C#反射Assembly 详细说明(转)

    1.对C#反射机制的理解2.概念理解后,必须找到方法去完成,给出管理的主要语法3.最终给出实用的例子,反射出来dll中的方法 反射是一个程序集发现及运行的过程,通过反射可以得到*.exe或*.dll等 ...

  10. 使用mybatis向oracle数据库插入数据异常

    遇到了使用mybatis向oracle数据库插入数据异常的问题, 具体的报错如下:org.springframework.jdbc.UncategorizedSQLException: ### Err ...