The Battle of Chibi(数据结构优化dp,树状数组)
The Battle of Chibi
Cao Cao made up a big army and was going to invade the whole South China. Yu Zhou was worried about it. He thought the only way to beat Cao Cao is to have a spy in Cao Cao’s army. But all generals and soldiers of Cao Cao were loyal, it’s impossible to convince any of them to betray Cao Cao. 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.
Input
The first line of the input gives the number of test cases, T (1 ≤ 100). T test cases follow. Each test case begins with two numbers \(N (1 ≤ N ≤ 10^3 )\) 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 i-th number \(a_i (1 ≤ a_i ≤ 10^9 )\) indicates the value in Cao Cao’s opinion of the i-th information in happening order.
Output
For each test case, output one line containing ‘Case #x: y’, where x is the test case number (starting from 1) and y is the ways Gai Huang can select the information. The result is too large, and you need to output the result mod by 1000000007 \((10^9 + 7)\). Note: In the first case, 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 case, Gai Huang has no choice as selecting any 2 information is not in increasing order.
Sample Input
2
3 2
1 2 3
3 2
3 2 1
Sample Output
Case #1: 3
Case #2: 0
题意
给定一个长度为\(N\)的数列A,求A有多少个长度为M的严格递增子序列。输出对\(10^9+7\)取模后的结果。
暴力程序还是比较好写的,\(dp[i][j]\)表示到第\(i\)个数,长度为\(j\)的序列的个数。
转移也很好写:
\(dp[i][j]=\sum dp[k][j-1]\) \((k<i\) && \(a[k]<a[i])\)
时间复杂度:\(O(N^2M)\)
#include<bits/stdc++.h>
using namespace std;
int read()
{
int x=0,w=1;char ch=getchar();
while(ch>'9'||ch<'0') {if(ch=='-')w=-1;ch=getchar();}
while(ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+ch-'0',ch=getchar();
return x*w;
}
const int N=1010;
int n,m,mod=1e9+7,ans;
int a[N],dp[N][N];
int main()
{
int t=read();
for(int w=1;w<=t;w++)
{
memset(dp,0,sizeof(dp));ans=0;
n=read();m=read();
for(int i=1;i<=n;i++) a[i]=read();
dp[0][0]=1;a[0]=-1;
for(int k=1;k<=m;k++)
{
for(int i=1;i<=n;i++)
{
for(int j=1;j<i;j++)
{
if(a[i]>a[j])
dp[i][k]=(dp[i][k]+dp[j][k-1])%mod;
}
}
}
for(int i=m;i<=n;i++) ans=(ans+dp[i][m])%mod;
printf("Case #%d: %d\n",w,ans);
}
}
考虑如何对暴力算法进行优化。
由于状态的定义,前两重循环无法优化,所以我们对决策的选择上进行优化,将\(k\)视为定值,每当\(i\)增大\(1\),决策候选集合会增加一个元素\(dp[i][k-1]\),它可能对\([i+1,n]\)的决策作出贡献。我们需要维护一个决策集合,这个决策集合以\(a[i]\)为关键码,\(dp[i][k-1]\)为权值,那么这道题就转化为了一个支持插入和查询前缀和的问题。
于是我们可以对\(a\)数组进行离散化,通过树状数组维护前缀和。
时间复杂度:\(O(NMlogN)\)
注意:
1.树状数组不能处理0位置,需要整体往后移一位,在\(1\)~\(n+1\)上建树状数组。
2.关于线段树,它被卡常辣qwq
#include<bits/stdc++.h>
#define ll(x) (x<<1)
#define rr(x) (x<<1|1)
using namespace std;
int read()
{
int x=0,w=1;char ch=getchar();
while(ch>'9'||ch<'0') {if(ch=='-')w=-1;ch=getchar();}
while(ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+ch-'0',ch=getchar();
return x*w;
}
const int N=1010;
int n,m,mod=1e9+7,ans;
int dp[N][N],c[N],pos[N];
struct node{
int v,id;
}a[N];
bool cmp(node p,node q){return p.v<q.v;}
int lowbit(int x){return x&(-x);}
void add(int p,int v)
{
for(int i=p;i<=n;i+=lowbit(i))
c[i]=(c[i]+v)%mod;
}
int query(int x)
{
int sum=0;
for(int i=x;i>0;i-=lowbit(i))
sum=(sum+c[i])%mod;
return sum;
}
int main()
{
int t=read();
for(int w=1;w<=t;w++)
{
memset(dp,0,sizeof(dp));ans=0;
n=read();m=read();n++;
for(int i=2;i<=n;i++) a[i].v=read(),a[i].id=i;
sort(a+2,a+n+1,cmp);
for(int i=2;i<=n;i++) pos[a[i].id]=i;pos[1]=1;
add(1,1);
for(int k=1;k<=m;k++)
{
for(int i=2;i<=n;i++)
{
dp[i][k]+=query(pos[i]);dp[i][k]%=mod;
add(pos[i],dp[i][k-1]);
}
memset(c,0,sizeof(c));
}
for(int i=m+1;i<=n;i++) ans=(ans+dp[i][m])%mod;
printf("Case #%d: %d\n",w,ans);
}
}
The Battle of Chibi(数据结构优化dp,树状数组)的更多相关文章
- $HDOJ5542\ The\ Battle\ of\ Chibi$ 数据结构优化$DP$
$AcWing$ $Description$ $Sol$ 首先显然是是以严格递增子序列的长度为阶段,由于要单调递增,所以还要记录最后一位的数值 $F[i][j]$表示前$i$个数中以$A_i$结尾的长 ...
- NOI.AC#2139-选择【斜率优化dp,树状数组】
正题 题目链接:http://noi.ac/problem/2139 题目大意 给出\(n\)个数字的序列\(a_i\).然后选出一个不降子序列最大化子序列的\(a_i\)和减去没有任何一个数被选中的 ...
- ACM学习历程—UESTC 1217 The Battle of Chibi(递推 && 树状数组)(2015CCPC C)
题目链接:http://acm.uestc.edu.cn/#/problem/show/1217 题目大意就是求一个序列里面长度为m的递增子序列的个数. 首先可以列出一个递推式p(len, i) = ...
- 奶牛抗议 DP 树状数组
奶牛抗议 DP 树状数组 USACO的题太猛了 容易想到\(DP\),设\(f[i]\)表示为在第\(i\)位时方案数,转移方程: \[ f[i]=\sum f[j]\;(j< i,sum[i] ...
- 树形DP+树状数组 HDU 5877 Weak Pair
//树形DP+树状数组 HDU 5877 Weak Pair // 思路:用树状数组每次加k/a[i],每个节点ans+=Sum(a[i]) 表示每次加大于等于a[i]的值 // 这道题要离散化 #i ...
- bzoj 1264 [AHOI2006]基因匹配Match(DP+树状数组)
1264: [AHOI2006]基因匹配Match Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 793 Solved: 503[Submit][S ...
- 【bzoj2274】[Usaco2011 Feb]Generic Cow Protests dp+树状数组
题目描述 Farmer John's N (1 <= N <= 100,000) cows are lined up in a row andnumbered 1..N. The cows ...
- 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 ...
- ccpc_南阳 C The Battle of chibi dp + 树状数组
题意:给你一个n个数的序列,要求从中找出含m个数的严格递增子序列,求能找出多少种不同的方案 dp[i][j]表示以第i个数结尾,形成的严格递增子序列长度为j的方案数 那么最终的答案应该就是sigma( ...
- Codeforces 909 C. Python Indentation (DP+树状数组优化)
题目链接:Python Indentation 题意: Python是没有大括号来标明语句块的,而是用严格的缩进来体现.现在有一种简化版的Python,只有两种语句: (1)'s'语句:Simple ...
随机推荐
- 第三周课程总结&实验报告(一)
实验报告(一) 1.打印输出所有的"水仙花数",所谓"水仙花数"是指一个3位数,其中各位数字立方和等于该数本身.例如,153是一个"水仙花数" ...
- @清晰掉 Sizeof与字符串
Sizeof与字符串 1.以字符串形式出现的,编译器都会为该字符串自动添加一个0作为结束符 如在代码中写 "abc",那么编译器帮你存储的是"abc/0" 2 ...
- PM项目跟进护航文档模板
护航文档 版本需求列表 需求 开发责任人 MMDrawerController.GCDTimer.Speex_armv7s等11个库迁移 熊文杰 相关人员 职称 开发人员 开发 熊文杰 测试 xxx ...
- (转)深入理解Java:注解(Annotation)自定义注解入门
向作者致敬! 转自:http://www.cnblogs.com/peida/archive/2013/04/24/3036689.html 要深入学习注解,我们就必须能定义自己的注解,并使用注解,在 ...
- 阶段3 1.Mybatis_06.使用Mybatis完成DAO层的开发_3 Mybatis中编写dao实现类的使用-修改删除等其他操作
update和上面的Insert代码基本是一样的,只需要修改这里, 测试Update的方法 删除 findById 测试方法 findByName 测试方法 findTotal
- 测开之路一百四十六:WTForms之表单应用
WTForms主要是两个功能:1.生成HTML标签 2.对数据格式进行验证 官网:https://wtforms.readthedocs.io/en/stable/ 这篇介绍用wtform生成htm ...
- lazarus 给应用程序创建 配置文件哈哈
lazarus 给应用程序创建 配置文件哈哈procedure TForm1.Button2Click(Sender: TObject);beginForceDirectoriesUTF8(GetAp ...
- Delphi加密解密算法
// 加密方法一(通过密钥加密解密)function EncryptString(Source, Key: string): string;function UnEncryptString(Sourc ...
- 【释疑】tp99、单实例qps
tp99 tp99的定义 tp99 (top percentile 99),指一组数据从小到大排列,处于99%位置的数据的值.例如等差数列range(1,101),tp99=99 tp99优于平均值的 ...
- PHP开发一个简单的成绩录入系统
预览界面 源码: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> < ...