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 ...
随机推荐
- html5 DeviceOrientation来实现手机网站上的摇一摇功能
原文地址:http://www.cootm.com/?p=706 从网上转载看到的,感觉不错,就转过来了,特此感谢 cnblogs 的 幸福2胖纸的码农生活,直接转载了,不要介意!呵呵 以下是转载内容 ...
- python【第九篇】多线程、多进程
内容提要 paramiko模块 进程.与线程区别 python GIL全局解释器锁 多线程 语法 join 线程锁之Lock\Rlock\信号量 将线程变为守护进程 Event事件 queue队列 生 ...
- linux hadoop 集群安装步骤
http://blog.csdn.net/xjavasunjava/article/details/12013677 1,时间同步hadoop集群的每台机器的时间不能相差太大. 安装集群前最好进行一下 ...
- Important Programming Concepts (Even on Embedded Systems) Part V: State Machines
Earlier articles in this series: Part I: Idempotence Part II: Immutability Part III: Volatility Part ...
- ibatis报错
关键词:org.springframework.dao.DataIntegrityViolationException 在程序中进行数据库插入操作时报错如下: 未知异常:org.springframe ...
- STL容器介绍(转)
STL的容器可以分为以下几个大类: 一:序列容器, 有vector, list, deque, string. 二 : 关联容器, 有set, multiset, map, mulmap, h ...
- ext3文件系统,reiserfs,xfs,jsf那种性能好点
ext2 是一个旧的 Linux 档桉系统,没有日志功能. 启用的时间通常需要很久.目前有许多 日志型态 的档桉系统可以以更快的速度及更好的效率完成系统启用和检查. ext3 为 ext2 的日志版, ...
- POJ 2112 Optimal Milking(Floyd+多重匹配+二分枚举)
题意:有K台挤奶机,C头奶牛,每个挤奶机每天只能为M头奶牛服务,下面给的K+C的矩阵,是形容相互之间的距离,求出来走最远的那头奶牛要走多远 输入数据: 第一行三个数 K, C, M 接下来是 ...
- 【模拟】Vijos P1005 超长数字串
题目链接: https://vijos.org/p/1005 题目大意: 无限的正整数按顺序拼接成字符串S(S=12345678910111213...),给你一个字符串A(len<=200)求 ...
- HDOJ 2021 发工资咯:)(利用了一种取余的思想)
Problem Description 作为杭电的老师,最盼望的日子就是每月的8号了,因为这一天是发工资的日子,养家糊口就靠它了,呵呵 但是对于学校财务处的工作人员来说,这一天则是很忙碌的一天,财务处 ...