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" 那段代码 ...
随机推荐
- 安装Oracle后修改IP总结(转载)
转载自:http://blog.csdn.net/bleibo/article/details/5447198 安装Oracle后修改IP总结(转载) 针对ORACLE 10G 在安装完后,修改IP ...
- 忘记了Ubuntu的密码
今天装了个虚拟机,现在装起来顺手的不行,毕竟用了多少次VMware了,结果进去发现,以前用过的虚拟机密码忘了,于是就百度解决方法,总结如下,其实很简单. 我用的Ubuntu 11的版本,后续即使有改动 ...
- zxing 二维码扫描 配置和使用
本文转载至 http://blog.csdn.net/a6472953/article/details/8796501 二维码扫描使用最多的主要有两个库:zbarSDK 和zxing 关于zbar ...
- java解析xml字符串为实体(dom4j解析)
package com.smsServer.Dhst; import java.util.HashMap; import java.util.Iterator; import java.util.Ma ...
- Censor(KMP)
Censor frog is now a editor to censor so-called sensitive words (敏感词). She has a long text pp. Her j ...
- 九度OJ 1360:乐透之猜数游戏 (递归)
时间限制:2 秒 内存限制:32 兆 特殊判题:否 提交:955 解决:261 题目描述: 六一儿童节到了,YZ买了很多丰厚的礼品,准备奖励给JOBDU里辛劳的员工.为了增添一点趣味性,他还准备了一些 ...
- 关于Apache Shiro权限框架的一些使用误区的解释
多了不说了,进入正题,shiro是个权限框架提供权限管理等功能,网上的教程一般都是互相抄,比如<shiro:principal property="xxx"/>这个标签 ...
- 二维码及二维码接合短URL的应用
二维码 1.什么是二维码? 二维条形码,最早发明于日本,它是用某种特定的几何图形按一定规律在平面(二维方向上)分布的黑白相间的图形记录数据符号信息的,在代码编制上巧妙地利用构成计算机内部逻辑基础的“0 ...
- float 的先后顺序 理解流
<!DOCTYPE html><html><head><style> p{float:right;}</style></head> ...
- 解决Oracle安装时报错“SID已在使用”办法
1. 开始->设置->控制面板->管理工具->服务 停止所有Oracle服务. 2. 开始->程序->Oracle - OraHome81->Oracle I ...