题意:给定一个序列,找出长度为m的严格递增序列的个数。

思路:用dp[i][j]表示长度为i的序列以下标j结尾的总个数。三层for循环肯定超时,首先离散化,离散化之后就可以用树状数组来优化,快速查找下边在j之前,值比ary[j]小且长度为i-1 的个数

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = ;
const int mod = ;
int BIT[maxn];
int dp[maxn][maxn]; void add(int &a, int b)
{
a += b;
while (a >= mod)//加法取模
a -= mod;
}
int lowbit(int x)
{
return x & (-x);
}
void update(int pos, int val, int n)
{
for (int i = pos; i <= n; i += lowbit(i))
add(BIT[i], val);
}
int query(int pos)
{
int ans = ;
for (int i = pos; i >= ; i -= lowbit(i))
add(ans, BIT[i]);
return ans;
}
int tmp[maxn];
int ary[maxn];
int main()
{
int n, m, T;
int kase = ;
scanf("%d", &T);
while (T--)
{
scanf("%d %d", &n, &m);
for (int i = ; i < n; i++)
{
scanf("%d", &tmp[i]);
ary[i] = tmp[i];
}
//离散化
sort(tmp, tmp + n);
int num = unique(tmp, tmp + n) - tmp;
for (int i = ; i < n; i++)
ary[i] = lower_bound(tmp, tmp + num, ary[i]) - tmp + ;
memset(dp, , sizeof(dp));
for (int i = ; i < n; i++)
dp[][i] = ;//初始化dp
for (int i = ; i <= m; i++)
{
memset(BIT, , sizeof(BIT));//每步都要初始化,因为只保存长度为i - 1的,每次只是快速查询而已。
for (int j = ; j < n; j++)
{
dp[i][j] = query(ary[j] - );//找比他小的并且下标位置在他之前,长度为i - 1的
update(ary[j], dp[i - ][j], n);//继续更新长度为i - 1的个数。
}
}
int ans = ;
for (int i = ; i < n; i++)
add(ans, dp[m][i]);
printf("Case #%d: %d\n", ++kase, ans);
}
return ;
}

2015 南阳ccpc The Battle of Chibi (uestc 1217)的更多相关文章

  1. 2015 CCPC-C-The Battle of Chibi (UESTC 1217)(动态规划+树状数组)

    赛后当天学长就说了树状数组,结果在一个星期后赖床时才有了一点点思路…… 因为无法提交,不确定是否正确..嗯..有错希望指出,谢谢... 嗯..已经A了..提交地址http://acm.uestc.ed ...

  2. 2015南阳CCPC C - The Battle of Chibi DP

    C - The Battle of Chibi Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 无 Description Cao Cao made up a ...

  3. 2015南阳CCPC C - The Battle of Chibi DP树状数组优化

    C - The Battle of Chibi Description Cao Cao made up a big army and was going to invade the whole Sou ...

  4. 2015南阳CCPC F - The Battle of Guandu 多源多汇最短路

    The Battle of Guandu Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 无 Description In the year of 200, t ...

  5. ccpc_南阳 C The Battle of chibi dp + 树状数组

    题意:给你一个n个数的序列,要求从中找出含m个数的严格递增子序列,求能找出多少种不同的方案 dp[i][j]表示以第i个数结尾,形成的严格递增子序列长度为j的方案数 那么最终的答案应该就是sigma( ...

  6. 2015南阳CCPC E - Ba Gua Zhen 高斯消元 xor最大

    Ba Gua Zhen Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 无 Description During the Three-Kingdom perio ...

  7. 2015南阳CCPC L - Huatuo's Medicine 水题

    L - Huatuo's Medicine Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 无 Description Huatuo was a famous ...

  8. 2015南阳CCPC H - Sudoku 暴力

    H - Sudoku Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 无 Description Yi Sima was one of the best cou ...

  9. 2015南阳CCPC G - Ancient Go 暴力

    G - Ancient Go Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 无 Description Yu Zhou likes to play Go wi ...

随机推荐

  1. MySQL zip版安装配置

    文章出处:http://www.cnblogs.com/winstic/,请保留此连接 这段时间在学习Python 数据库操作知识,简单整理MySQL zip文件安装方法 下载 在MySQL官网htt ...

  2. uboot使用tftp下载时出现“checksum bad”问题原因分析

    一.问题 二.原因分析 你的虚拟机是不是这样设置的呢? 如果是的话,请看下边的解释: 使用NAT模式,就是让虚拟系统借助NAT(网络地址转换)功能,通过宿主机器所在的网络来访问公网.也就是说,使用NA ...

  3. SharePoint 2013 弹窗效果之本地HTML打开方式(二)

    上一篇我们主要讲述如何通过showModalDialog方法进行弹出窗体,同时弹出信息定义在新的页面(Application Page),使用 SP.UI.$create_DialogOptions( ...

  4. win7如何开启和关闭超级管理员账户

    激活命令: net user administrator /active:yes 关闭命令: net user administrator /active:no

  5. study note--(Education)

    some good temoplates: A child's education has never been about learning information and basic skills ...

  6. ubuntu 查本机 ip地址的命令是什么, 详细信息的?

    使用ifconfig命令即可.你一敲进去都出来了

  7. curl post传递json数据

    有时想在命令行使用post http提交一个表单,比较常用的是POST模式和GET模式 GET模式什么option都不用,只需要把变量写在url里面就可以了 比如:curl http://www.wa ...

  8. Buffer Sort

    BUFFER (SORT) Description Performs a memory sort on a row source CREATE TABLE t1 (c01 NUMBER); CREAT ...

  9. WordPress 3.5.1 crypt_private()远程拒绝服务漏洞(CVE-2013-2173)

    漏洞版本: WordPress 3.5.1 漏洞描述: BUGTRAQ ID: 60477 CVE(CAN) ID: CVE-2013-2173 WordPress是一种使用PHP语言和MySQL数据 ...

  10. dll文件已经引用,但using找不到命名空间

    一:问题截图 二:解决办法 1.没看到lz的代码中有Vancl.Server的dll. 2.确实有编译不过的问题,是Vancl.WindowsServices这个工程的target framework ...