hdu5542 The Battle of Chibi【树状数组】【离散化】
The Battle of Chibi
Time Limit: 6000/4000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 2899 Accepted Submission(s): 1043
So there is only one way left for Yu Zhou, send someone to fake surrender Cao Cao. Gai Huang was selected for this important mission. However, Cao Cao was not easy to believe others, so Gai Huang must leak some important information to Cao Cao before surrendering.
Yu Zhou discussed with Gai Huang and worked out N information to be leaked, in happening order. Each of the information was estimated to has ai value in Cao Cao's opinion.
Actually, if you leak information with strict increasing value could accelerate making Cao Cao believe you. So Gai Huang decided to leak exact M information with strict increasing value in happening order. In other words, Gai Huang will not change the order of the N information and just select M of them. Find out how many ways Gai Huang could do this.
Each test case begins with two numbers N(1≤N≤103) and M(1≤M≤N), indicating the number of information and number of information Gai Huang will select. Then N numbers in a line, the ith number ai(1≤ai≤109) indicates the value in Cao Cao's opinion of the ith information in happening order.
The result is too large, and you need to output the result mod by 1000000007(109+7).
3 2
1 2 3
3 2
3 2 1
Case #2: 0
In the first cases, Gai Huang need to leak 2 information out of 3. He could leak any 2 information as all the information value are in increasing order.
In the second cases, Gai Huang has no choice as selecting any 2 information is not in increasing order.
题意:
问一个序列之中有多少个长度为M的严格递增子序列。
思路:
我们用dp[i][j]表示前j个数中,以Aj为结尾的长度为i的严格递增子序列的个数。
那么对于dp[i][j],我们只需要枚举所有小于j的k,并且Ak < Aj,将所有的dp[i-1][k]求和,可以得到dp[i][j]
很容易想到O(n^3)的算法,但是显然会超时,所以我们需要进行一些优化。
当我们枚举内层循环j,k时,外层循环i就可以被当成是定值。当j增加1,k的取值只是多了k = j这个新的决策。
因此我们用树状数组维护一个前缀和,表示1~j区间,长度为i-1时的方案数。
由于add时第一个参数放的是a[j],也就是说现在直接用了他的权值作为了下标,所以只要下标比他小的,权值一定比他小,也就不需要进行比较了。直接去前缀和就可以了。【日常感谢家庭教师】
最开始离散化用的set和map TLE了
后来改用了另一个数组,先排序然后lower_bound,并且直接把原数组的值改掉
#include <iostream>
#include <set>
#include <cmath>
#include <stdio.h>
#include <cstring>
#include <algorithm>
#include <map>
using namespace std;
typedef long long LL;
#define inf 0x7f7f7f7f int t, n, m, cnt;
const int maxn = ;
const LL mod = 1e9 + ;
int a[maxn], b[maxn];
LL sum[maxn], dp[maxn][maxn];
map<LL, int>discrete;
set<LL>sss;
set<LL>::iterator iter; void add(int pos, LL x)
{
while(pos <= n + ){
sum[pos] = (sum[pos] + x) % mod;
pos += (pos & -pos);
}
} LL ask(int pos)
{
LL ans = ;
while(pos){
ans = (ans + sum[pos]) % mod;;
pos -= (pos & -pos);
}
return ans;
} void init()
{
discrete.clear();
sss.clear();
//memset(dp, 0, sizeof(dp));
for(int i = ; i <= m; i++){
for(int j = ; j <= n; j++){
dp[i][j] = ;
}
}
cnt = ;
} int main()
{
scanf("%d", &t);
for(int cas = ; cas <= t; cas++){
init();
scanf("%d%d", &n, &m);
a[] = b[] = -inf;
dp[][] = ;
//sss.insert(a[0]);
for(int i = ; i <= n; i++){
scanf("%d", &a[i]);
b[i] = a[i];
//sss.insert(a[i]);
}
sort(b, b + n + );
for(int i = ; i <= n; i++){
a[i] = lower_bound(b, b + + n, a[i]) - b + ;
//cout<<a[i]<<endl;
}
/*for(iter = sss.begin(); iter != sss.end(); iter++){
discrete[*iter] = ++cnt;
}*/
//cout<<a[0]<<endl;
for(int i = ; i <= m; i++){
//memset(sum, 0, sizeof(sum));
for(int j = ; j <= n + ; j++){
sum[j] = ;
}
add(a[], dp[i - ][]); for(int j = ; j <= n; j++){
dp[i][j] = ask(a[j] - );
//if(discrete[a[j]] < discrete[a[j + 1]])
add(a[j], dp[i - ][j]);
}
} int ans = ;
for(int i = ; i <= n; i++){
ans = (ans + dp[m][i]) % mod;
}
printf("Case #%d: %d\n", cas, ans);
}
}
hdu5542 The Battle of Chibi【树状数组】【离散化】的更多相关文章
- 南阳ccpc C题 The Battle of Chibi && hdu5542 The Battle of Chibi (树状数组优化+dp)
题意: 给你一个长度为n的数组,你需要从中找一个长度为m的严格上升子序列 问你最多能找到多少个 题解: 我们先对原序列从小到大排序,排序之后的序列就是一个上升序列 这里如果两个数相等的话,那么因为题目 ...
- 南阳ccpc C题 The Battle of Chibi 树状数组+dp
题目: Cao Cao made up a big army and was going to invade the whole South China. Yu Zhou was worried ab ...
- hdu4605 树状数组+离散化+dfs
Magic Ball Game Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- BZOJ_5055_膜法师_树状数组+离散化
BZOJ_5055_膜法师_树状数组+离散化 Description 在经历过1e9次大型战争后的宇宙中现在还剩下n个完美维度, 现在来自多元宇宙的膜法师,想偷取其中的三个维度为伟大的长者续秒, 显然 ...
- POJ 2299 【树状数组 离散化】
题目链接:POJ 2299 Ultra-QuickSort Description In this problem, you have to analyze a particular sorting ...
- HDU 2227 Find the nondecreasing subsequences (DP+树状数组+离散化)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2227 Find the nondecreasing subsequences ...
- BZOJ-1227 虔诚的墓主人 树状数组+离散化+组合数学
1227: [SDOI2009]虔诚的墓主人 Time Limit: 5 Sec Memory Limit: 259 MB Submit: 914 Solved: 431 [Submit][Statu ...
- POJ 2299 树状数组+离散化求逆序对
给出一个序列 相邻的两个数可以进行交换 问最少交换多少次可以让他变成递增序列 每个数都是独一无二的 其实就是问冒泡往后 最多多少次 但是按普通冒泡记录次数一定会超时 冒泡记录次数的本质是每个数的逆序数 ...
- [HDOJ4325]Flowers(树状数组 离散化)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4325 关于离散化的简介:http://blog.csdn.net/gokou_ruri/article ...
- HDU4456-Crowd(坐标旋转+二位树状数组+离散化)
转自:http://blog.csdn.net/sdj222555/article/details/10828607 大意就是给出一个矩阵 初始每个位置上的值都为0 然后有两种操作 一种是更改某个位置 ...
随机推荐
- 64位程序,long*转long 出错
原因: long*在64位程序中占8个字节,long占4个字节.强转会出错. 解决方法: 把long用long long替换,long long 占8个字节
- e672. 缩放,剪取,移动,翻转缓冲图像
AffineTransform tx = new AffineTransform(); tx.scale(scalex, scaley); tx.shear(shiftx, shifty); tx.t ...
- 多媒体开发之rtcp详解---rtcp数据包
http://www.360doc.com/content/13/0606/10/1317564_290865866.shtml http://blog.csdn.net/hrbeuwhw/artic ...
- (转)platform_driver_register,什么时候调用PROBE函数 注册后如何找到驱动匹配的设备
platform_driver_register,什么时候调用PROBE函数 注册后如何找到驱动匹配的设备 2011-10-24 19:47:07 分类: LINUX kernel_init中d ...
- VC++使用CSocket发送HTTP Request时需要注意发送数据的编码格式
VS2010以及更高版本中新建的MFC项目字符集默认是Unicode,CString创建的字符串默认是Unicode. 使用CSocket时,若以CString组织需要发送的HTTP Head时,那么 ...
- Js Object转化为json,json转Object
var obj={x:10,y:50};var t= JSON.stringify(obj);console.log(typeof t);var gg= JSON.parse(t);console.l ...
- Win10開始菜单打不开
一.前言 自从用Win10之后(附上<我的Win10之旅>).用清理软件.总是深度清理,导致rt问题. 每次百度都是没用的解决方法: 今天,再一次清理(Wise Care 365 注冊表深 ...
- struts2的零配置
最近开始关注struts2的新特性,从这个版本开始,Struts开始使用convention-plugin代替codebehind-plugin来实现struts的零配置.配置文件精简了,的确是简便了 ...
- asp.net session的使用与过期实例代码
Session的使用 <head runat="server"> <title></title> <script src=&q ...
- mysql中,查看当前数据库下所有的基表,不包括视图
环境描述: mysql版本:5.5.57-log 操作系统版本:Red Hat Enterprise Linux Server release 6.6 (Santiago) 需求描述: 查看当前使用的 ...