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" 那段代码 ...
随机推荐
- 项模板选择器属性(ItemTemplateSelector属性)和样式选择器(ItemContainerStyleSelector)
3.4.5 共享尺寸组 样式选择器: 或者========================================
- 【转】火狐右键google搜索特别慢的解决办法
原网页:http://www.fatalist.im/blog/459.html google将谷歌中文网站google.cn的搜索服务转向到google.com.hk(香港)后,firefox右上角 ...
- Android无线测试之—UiAutomator UiSelector API介绍之五
对象搜索—文本与描述 一.文本属性定位对象: 返回值 API 描述 UiSelector test(String text) 文本完全匹配 UiSelector testContains(String ...
- Android UI开发第二十九篇——Android中五种常用的menu(菜单)
Android Menu在手机的应用中起着导航的作用,作者总结了5种常用的Menu. 1.左右推出的Menu 前段时间比较流行,我最早是在海豚浏览器中看到的,当时耳目一新.最早使用左右推出菜单的,听说 ...
- VS2012如何显示行号
Tools-Options-Text Editor-All Languages –General – Display
- Xamarin.Forms学习之位图(一)
在开始我的分享之前呢,让我先问下我的问题: 1.最近更新了Xamarin 4.1.1.3,我想问下版本更新信息在哪里看? 2.更新后我新建的项目没有UWP项目了(虽然没有用过,但是我想确认是4.1.1 ...
- Webshell清除-解决驱动级文件隐藏挂马
Webshell清除-解决驱动级文件隐藏挂马
- okhttp 防止自动删除url中的./和../
测试任意文件读取漏洞是需要在url中加上 ../../ 之类的字符,但是如果使用三方库Apache httpclient或okhttp,他们都会自动删除url中的 ../ ,通过修改okhttp的源 ...
- 剑指offer 面试53题
面试53题: 题目:统计一个数字在排序数组中出现的次数. 思路:二分查找法,分别找到此数字在排序数组中第一次和最后一次出现的位置,然后次数等于两个位置之差加1. 时间复杂度:O(log n) 解题代码 ...
- Java CLASSPATH
@1:CLASSPATH用途: #1:给import指路 -- import后面的包名(转换为相应的路径后)与CLASSPATH中的各项相连接. 然后解释器从这些路径(前面连接之后的结果)中查找程序中 ...