2015 南阳ccpc The Battle of Chibi (uestc 1217)
题意:给定一个序列,找出长度为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)的更多相关文章
- 2015 CCPC-C-The Battle of Chibi (UESTC 1217)(动态规划+树状数组)
赛后当天学长就说了树状数组,结果在一个星期后赖床时才有了一点点思路…… 因为无法提交,不确定是否正确..嗯..有错希望指出,谢谢... 嗯..已经A了..提交地址http://acm.uestc.ed ...
- 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 ...
- 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 ...
- 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 ...
- ccpc_南阳 C The Battle of chibi dp + 树状数组
题意:给你一个n个数的序列,要求从中找出含m个数的严格递增子序列,求能找出多少种不同的方案 dp[i][j]表示以第i个数结尾,形成的严格递增子序列长度为j的方案数 那么最终的答案应该就是sigma( ...
- 2015南阳CCPC E - Ba Gua Zhen 高斯消元 xor最大
Ba Gua Zhen Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 无 Description During the Three-Kingdom perio ...
- 2015南阳CCPC L - Huatuo's Medicine 水题
L - Huatuo's Medicine Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 无 Description Huatuo was a famous ...
- 2015南阳CCPC H - Sudoku 暴力
H - Sudoku Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 无 Description Yi Sima was one of the best cou ...
- 2015南阳CCPC G - Ancient Go 暴力
G - Ancient Go Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 无 Description Yu Zhou likes to play Go wi ...
随机推荐
- Article及ArticleList模板
HTML5滑动条: <input type="range" min="0" max="100" value="55" ...
- 清北第一套题(zhx)
死亡 [问题描述] 现在有个位置可以打sif,有个人在排队等着打sif.现在告诉你前个人每个人需要多长的时间打sif,问你第个人什么时候才能打sif.(前个人必须按照顺序来) [输入格式] 第一行两个 ...
- CSS HACK的方法
所有浏览器 通用 height: 100px; IE6 专用 _height: 100px; IE7 专用 *+height: 100px; IE6.IE7 共用 *height: 100px; IE ...
- Java增强的泛型
尽管Java 8是2014年年初才发布的,而Java 9要等到2016年年中,但是目前有一些计划放到某个未来版本(希望是Java 10)中的特性已经合并了进来. 具体而言,有两个比较大的特性已经开始原 ...
- BAT等互联网公司薪资分享
- centos 6.5 安装weixin
下载cpanm wget http://xrl.us/cpanm --no-check-certificate -O /sbin/cpanm && chmod +x /sbin/cpa ...
- uboot从SD卡烧写内核和文件系统
环境:ubuntu 13.04一.首先制作sd启动盘: 插入SD卡 sudo dd iflag=dsync oflag=dsync if=tiny210v2-uboot.binof=/dev/m ...
- 【高精度】NCPC 2014 C catalansqure
题目链接: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1789 题目大意: 求大卡特兰数..公式如下.输入n求Sn(n<=5000) 题目 ...
- 连接Oracle的几种方式
如何引用Data.OracleClient.dll 由于从.net 4.0之后,微软将OracleClient.dll从框架里去除了,所以要使用,需要在VS2010里面去把项目的.net框架从.net ...
- HDU4003 Find Metal Mineral
看别人思路的 树形分组背包. 题意:给出结点数n,起点s,机器人数k,然后n-1行给出相互连接的两个点,还有这条路线的价值,要求最小花费 思路:这是我从别人博客里找到的解释,因为很详细就引用了 dp[ ...