http://acm.hdu.edu.cn/showproblem.php?pid=4996

直接贴bc题解

按数字1-N的顺序依次枚举添加的数字,用2N的状态保存在那个min数组中的数字,每次新添加数字可以根据位置计算出新的min数组。
怎么快速计算呢?这里如果枚举N的位置是不可行的,这样2n的state记录的信息不够。很巧妙的思路是枚举放在当前位置的数字,比如说1-N-1的排列状态下,枚举第N位为K,那么1-N-1位的>=k的数字全加1,就得到了一个1-N的排列。
当然这个算法是有问题的,但是由于求的是长度,所以所有j代表的状态并不能正确表示对应的上升序列,因为每个有值的dp[j]必然有j&1 == 1,但是却奇迹般的AC了..所以大致理解算法思想和AC原因但是这个算法的解释略有疑问...
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <string>
#include <queue>
#include <map>
#include <iostream>
#include <algorithm>
using namespace std;
#define RD(x) scanf("%d",&x)
#define RD2(x,y) scanf("%d%d",&x,&y)
#define RD3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define clr0(x) memset(x,0,sizeof(x))
typedef long long LL;
const int maxn = 1<<20;
int n,k;
LL f[20][20],dp[maxn],tmp[maxn]; int cal(int x)
{
int ans = 0;
for(int i = 0;i < 20;++i)
if((1<<i)&x)
ans++;
return ans;
}
void init () {
dp[1] = f[1][1] = 1;
for(int i = 1;i < 18;++i){
for(int j = 0;j < (1<<i);++j)
tmp[j] = dp[j];
for(int j = 0;j < (1<<(i+1));++j)
dp[j] = 0;
for(int j = 0;j < (1<<i);++j)if(tmp[j]){
for(int k = 0;k <= i;++k){
int tot = 0,c[20],st = 0;
for(int l = 0;l < i;++l){
if((1<<l) & j){
c[tot] = l;
if(c[tot] >= k)
c[tot]++;
tot++;
}
}
c[tot++] = k;
for(int l = 0;l < tot;++l){
if(c[l] > k){
c[l] = k;
break;
}
}
for(int l = 0;l < tot;++l){
st |= (1<<c[l]);
}
dp[st] += tmp[j];
}
}
for(int j = 0;j < (1<<(i+1));++j)
f[i+1][cal(j)] += dp[j];
}
}
int main() {
init();
int _;RD(_);while(_--){
RD2(n,k);
printf("%I64d\n",f[n][k]);
}
return 0;
}

hdu 4996 1~n排列LIS值为k个数的更多相关文章

  1. hdu 5727 二分图+环排列

    Necklace Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Su ...

  2. hdu 3664 1~n排列(ai>i ) 为k个数

    http://acm.hdu.edu.cn/showproblem.php?pid=3664 求1~n的排列个数,使得逆序数(ai>i ) 为给定的k. dp[i][j]表示前1~i的排列中,有 ...

  3. hdu5592 倒序求排列+权值线段树

    这种题为什么要用到主席树啊..8说了,直接上代码 /* 1-n的排列,给定所有前缀的逆序对数量,要求恢复排列 首先能确定最后一个数是什么,然后倒序确定即可 开线段树找空位:如果Ai-Ai-1=k,说明 ...

  4. hdu 4638 树状数组 区间内连续区间的个数(尽可能长)

    Group Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Subm ...

  5. 剑值offer:最小的k个数

    这是在面试常遇到的topk问题. 题目描述: 输入n个整数,找出其中最小的K个数.例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4,. 解题思路: 思路一:用快排对数 ...

  6. Python算法题(二)——国际象棋棋盘(排列组合问题,最小的K个数)

    题目一(输出国际象棋棋盘)  分析: 用i控制行,j来控制列,根据i+j的和的变化来控制输出黑方格,还是白方格.   主要代码: for i in range(8): for j in range(8 ...

  7. [LeetCode] Count Univalue Subtrees 计数相同值子树的个数

    Given a binary tree, count the number of uni-value subtrees. A Uni-value subtree means all nodes of ...

  8. scala 学习笔记(03) 参数缺省值、不定个数参数、类的属性(Property)、泛型初步

    继续学习,这一篇主要是通过scala来吐槽java的,同样是jvm上的语言,差距咋就这么大呢? 作为一个有.NET开发经验的程序员,当初刚接触java时,相信很多人对java语言有以下不爽(只列了极小 ...

  9. 删除链表中全部值为k的节点

    1. 问题描写叙述 给定一个单链表,删除当中值为k的全部节点.比如:1→2→6→3→4→5→61 \to 2 \to 6 \to 3 \to 4 \to 5 \to 6,删除当中值为6的节点,返回:1 ...

随机推荐

  1. 处理【由于 Web 服务器上的“ISAPI 和 CGI 限制”列表设置,无法提供您请求的页面】

    处理[由于 Web 服务器上的“ISAPI 和 CGI 限制”列表设置,无法提供您请求的页面] 详细错误:HTTP 错误 404.2 - Not Found. 由于 Web 服务器上的“ISAPI 和 ...

  2. nyoj528-找球号(三) 【位运算】

    http://acm.nyist.net/JudgeOnline/problem.php?pid=528 找球号(三) 时间限制:2000 ms  |  内存限制:3000 KB 难度:2   描述 ...

  3. manacher最长回文序列c++

    算法真心读不懂 #include <iostream>#include<string>#include<cstring> using namespace std;c ...

  4. mvc EF 从数据库更新实体,添加视图实体时添加不上的问题

    视图对象没有一列为非null的,解决办法,在视图中,将某一列排除为null的可能,比如:isnull(te,1),即可.

  5. No handlers could be found for logger “apscheduler.executors.default”?

    Call logging.basicConfig() before instantiating the scheduler. That lets you see what the real probl ...

  6. HDU_1022

    题目: As the new term comes, the Ignatius Train Station is very busy nowadays. A lot of student want t ...

  7. OpenSource.iOS.ProtobufWithObjective-C

    2. 在iOS(Mac OS X)中使用protobuf 2.0 构建protoc A) 下载最新的protobuf版本 B) 依据README中的步骤依次进行 2.1 添加protobuf到工程中 ...

  8. [SoapUI] 配置默认环境的properties

    <Envs> <Env id="Live,Default environment"> <Project> <CusProperty nam ...

  9. 10 Maven 版本管理

    Maven 版本管理 一个健康的项目通常有一个长期.合理的版本演变过程.例如 Maven 本身的版本也比较多,如最早的 Maven1:Maven2 有 2.0.9.2.0.10.2.1.0.2.2.0 ...

  10. 1、HttpClient初探

    HttpClient是它的核心接口.可以理解为一个简单的浏览器. 主要方法有: getParams();   获取运行参数 getConnectionManager(); 获取连接管理器.连接管理器中 ...