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 ...
随机推荐
- 使用Raphael 画图(一) 基本图形 (javascript)
Raphael是什么? Raphael 是一个用于在网页中绘制矢量图形的 Javascript 库.它使用 SVG W3C 推荐标准和 VML 作为创建图形的基础,你可以通过 JavaScript 操 ...
- tomcat架构分析-索引
出处:http://gearever.iteye.com tomcat架构分析 (概览) tomcat架构分析 (容器类) tomcat架构分析 (valve机制) tomcat架构分析 (valve ...
- 转:关于数据库压缩技术的Survey
原文来自于:http://outofmemory.cn/mysql/database-compression-tech 昨天给团队内的小伙伴做了一个关于数据库压缩技术的Survey,现将其中可以公开的 ...
- 疯狂java讲义笔记 2.3.7
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools=&q ...
- Unity 技能按钮触发特效
unity 版本:4.5.1 NGUI版本:3.6.5 首先,要导入特效资源包,导入应该是基本中的基础,但是对于初学者来说好像很少有这方面的介绍,也许是我现学现用书看的不够认真,因为导入这个问题卡了好 ...
- Monthly Expense(二分)
Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 11196 Accepted: 4587 Description Farm ...
- COJ 0047 20702最大乘积
20702最大乘积 难度级别:B: 运行时间限制:1000ms: 运行空间限制:51200KB: 代码长度限制:2000000B 试题描述 输入n个元素组成的序列s,你需要找出一个乘积最大的连续子序列 ...
- 【效率】FIND
文档 HTML Flash CSS 字体 命名颜色 工具 IMG
- 安装zabbix2.2.3
系统版本:CentOS 6.3_x86_64 zabbix版本:zabbix-2.2.3 zabbix服务端IP:172.16.10.72 1.yum安装LAMP环境 # yum -y install ...
- 高等数学(拉格朗日乘子法):NOI 2012 骑行川藏
[NOI2012] 骑行川藏 输入文件:bicycling.in 输出文件:bicycling.out 评测插件 时间限制:1 s 内存限制:128 MB NOI2012 Day1 Des ...