题意不提。

我们可以发现,可以将最终序列分为对于第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递增,因此有一个必选的数字直接填入即可。

直接转移即可。

  1. #include<bits/stdc++.h>
  2. #define MOD 1000000007
  3. using namespace std;
  4. #define FILE "chad"
  5. set<int> S;
  6. int n, k, f[105][105];
  7.  
  8. int main()
  9. {
  10. //freopen(FILE".in","r",stdin);
  11. //freopen(FILE".out","w",stdout);
  12. int T; scanf("%d",&T);
  13. for(int tt = 0;T--;)
  14. {
  15. memset(f, 0, sizeof f);
  16. S.clear();
  17.  
  18. scanf("%d%d",&n,&k);
  19. for(int i = 1; i <= n; i++) S.insert(i);
  20. int mx = 0, flag = 1;
  21. for(int i = 1; i <= k; i++)
  22. {
  23. int x; scanf("%d",&x);
  24. if(x > mx)
  25. {
  26. mx = x;
  27.  
  28. }
  29. else if(x != *S.begin()) flag = 0;
  30. S.erase(x);
  31. }
  32. f[mx][k] = flag;
  33. for(int i = 0; i <= n; i++)
  34. {
  35. for(int j = 0; j < n; j++)
  36. {
  37. if(i-j > 0) (f[i][j+1] += f[i][j]) %= MOD;
  38. for(int k = i+1; k <= n; k++)
  39. (f[k][j+1] += f[i][j]) %= MOD;
  40. }
  41. }
  42. printf("Case #%d: %d\n",++tt,f[n][n]);
  43.  
  44. }
  45. }
  46.  
  47. //代码来自某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. tomcat本身的lib目录都有哪些jar包

    1.tomcat下的lib目录,自己带有的jar包有:servlet.jar,tomcat-jdbc.jar,tomncat-dbcp.jar,jsp.jar等 2.tomcat下的lib目录,自己带 ...

  2. asp.net源程序编译为dll文件并调用的实现过程

    很多时候,我们需要将.cs文件单独编译成.dll文件,这就需要使用csc命令将.cs文件编译成.dll动态链接库文件.具体的操作步骤如下: 打开命令窗口->输入cmd到控制台->cd C: ...

  3. EasyUI怎么利用onBeforeRender事件

    onBeforeRender事件是view的属性,该事件发生在把ajax请求到的数据填充到表格内容中之前 将此段代码附加在DataGrid初始化后执行,即可完成在DataGrid渲染之前进行操作 // ...

  4. iOS学习笔记(四)——iOS应用程序生命周期

    开发应用程序都要了解其生命周期,开始接触android时也是从应用程序生命周期开始的,android的应用程序生命周期更多是其组件的生命周期,例如Activity.Service.今天我们接触一下iO ...

  5. 1714 B君的游戏(Nim博弈)

    1714 B君的游戏 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 B君和L君要玩一个游戏.刚开始有n个正整数  ai  . 双方轮流操作.每次操作,选一个正整数 ...

  6. 字符串 转 时间戳 unix_timestamp('2018-07-01 0:0:0')

    SELECT FROM_UNIXTIME(createtime,'%Y%m%d') AS d, s.app,SUM(o.val) FROM orders o LEFT JOIN app_type s ...

  7. junit5荟萃知识点(一):junit5的组成及安装

    1.什么是junit5? 和之前的junit版本不一样,junit5是由三个模块组成. JUnit 5 = JUnit Platform + JUnit Jupiter + JUnit Vintage ...

  8. MySQL版本与工具

    MySQL各个版本区别 MySQL 的官网下载地址:http://www.mysql.com/downloads/ 在这个下载界面会有几个版本的选择. 1. MySQL Community Serve ...

  9. Python 新手常犯错误

    Python 新手常犯错误(第二部分) 转发自:http://blog.jobbole.com/43826/ 作用域 在这篇文章里,我们来关注作用域在Python被误用的地方.通常,当我们定义了一个全 ...

  10. checkmarx使用笔记、原理

    checkmarks是一款商业的代码静态分析工具,和pmd类似的地方是他分析的是java文件,而非class文件.checkmarks使用 .net开发,必须安装在windows上,它的规则也是类似. ...