南阳ccpc C题 The Battle of Chibi 树状数组+dp
题目:
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 NN information to be leaked, in happening order. Each of the information was estimated to has aiai 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 MM information with strict increasing value in happening order. In other words, Gai Huang will not change the order of the NN information and just select MM of them. Find out how many ways Gai Huang could do this.
InputThe first line of the input gives the number of test cases, T(1≤100)T(1≤100). TT test cases follow.
Each test case begins with two numbers N(1≤N≤103)N(1≤N≤103) and M(1≤M≤N)M(1≤M≤N), indicating the number of information and number of information Gai Huang will select. Then NN numbers in a line, the ithith number ai(1≤ai≤109)ai(1≤ai≤109) indicates the value in Cao Cao's opinion of the ithith information in happening order.OutputFor each test case, output one line containing Case #x: y, where xx is the test case number (starting from 1) and yy is the ways Gai Huang can select the information.
The result is too large, and you need to output the result mod by 1000000007(109+7)1000000007(109+7).Sample Input
2
3 2
1 2 3
3 2
3 2 1
Sample Output
Case #1: 3
Case #2: 0
Hint
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.
题意:
给你n个有序的数,问你能找到多少个m长度的严格递增子序列
题解:
我们设dp[i][j]表示:截至于第i个数(使用了第i个数),所能构成的严格递增子序列长度为j的个数为dp[i][j]
那么dp[i][j]的值肯定是:dp[k][j-1]之和,k属于[1,i-1]。且要满足,那么输入的第k个数要小于第i个数才可以加上dp[k][j-1]
我们看数据n=1e3,如果暴力去写,复杂度就是O(n3),所以我们肯定需要优化(我那时也不知道咋弄)
看其他题解发现,使用树状数组来优化,但是树状数组求得前缀和,而我们只是需要前缀和的一部分,这可怎么办。。
然后我们就可以处理一下输入,对于第k个数不小于第i个数的,我们可以先不处理它,那么这个dp[k][j-1]就是0
那么我们的前缀和相当于没有加上它,就可以达到满足我们的需要。这个处理只需要一个排序就可以
代码:
1 #include<iostream>
2 #include<algorithm>
3 #include<cstdio>
4 #include<queue>
5 #include<map>
6 #include<vector>
7 #include<cstring>
8 using namespace std;
9 const int mod=1e9+7;
10 const int maxn=1e3+5;
11 #define mem(a) memset(a,0,sizeof(a))
12 //求sum(dp[1-x][j])
13 int n,m,dp[maxn][maxn];
14 struct shudui
15 {
16 int id,val;
17 }que[maxn];
18 bool cmp(shudui x,shudui y)
19 {
20 if(x.val!=y.val)
21 return x.val<y.val;
22 return x.id>y.id; //如果两个val相等,因为题目要求严格递增,所以这样排序就可以满足题意
23 }
24 int lowbit(int x)
25 {
26 return x&(-x);
27 }
28 void update(int x,int y,int val) //更新包含dp[x][y]的
29 { //后缀数组项
30 while(x<=n)
31 {
32 dp[x][y]=(dp[x][y]+val)%mod;
33 x+=lowbit(x);
34 }
35 }
36 int get_sum(int x,int y)
37 {
38 int sum=0;
39 while(x>0)
40 {
41 sum=(sum+dp[x][y])%mod;
42 x-=lowbit(x);
43 }
44 return sum;
45 }
46 int main()
47 {
48 int t,p=0;
49 scanf("%d",&t);
50 while(t--)
51 {
52 mem(dp);
53 scanf("%d%d",&n,&m);
54 for(int i=1;i<=n;++i)
55 {
56 scanf("%d",&que[i].val);
57 que[i].id=i;
58 }
59 sort(que+1,que+1+n,cmp);
60 for(int i=1;i<=n;++i)
61 {
62 for(int j=1;j<=m;++j)
63 {
64 if(j==1)
65 update(que[i].id,j,1);
66 else //因为我们按照val排过序了,所以我们可以加上前缀和就行
67 {
68 int sum=get_sum(que[i].id-1,j-1);
69 update(que[i].id,j,sum);
70 }
71 }
72 }
73 printf("Case #%d: %d\n",++p,get_sum(n,m));
74 }
75 return 0;
76 }
南阳ccpc C题 The Battle of Chibi 树状数组+dp的更多相关文章
- 南阳ccpc C题 The Battle of Chibi && hdu5542 The Battle of Chibi (树状数组优化+dp)
题意: 给你一个长度为n的数组,你需要从中找一个长度为m的严格上升子序列 问你最多能找到多少个 题解: 我们先对原序列从小到大排序,排序之后的序列就是一个上升序列 这里如果两个数相等的话,那么因为题目 ...
- BZOJ_2683_简单题&&BZOJ_1176_[Balkan2007]Mokia_CDQ分治+树状数组
BZOJ_2683_简单题&&BZOJ_1176_[Balkan2007]Mokia_CDQ分治+树状数组 Description 维护一个W*W的矩阵,初始值均为S.每次操作可以增加 ...
- HDU 6240 Server(2017 CCPC哈尔滨站 K题,01分数规划 + 树状数组优化DP)
题目链接 2017 CCPC Harbin Problem K 题意 给定若干物品,每个物品可以覆盖一个区间.现在要覆盖区间$[1, t]$. 求选出来的物品的$\frac{∑a_{i}}{∑b_ ...
- tyvj P1716 - 上帝造题的七分钟 二维树状数组区间查询及修改 二维线段树
P1716 - 上帝造题的七分钟 From Riatre Normal (OI)总时限:50s 内存限制:128MB 代码长度限制:64KB 背景 Background 裸体就意味着 ...
- 【BZOJ3132】【TYVJ1716】上帝造题的七分钟 二维树状数组
题目大意 维护一个\(n\times m\)的矩阵,有两种操作: \(1~x_1~y_1~x_2~y_2~v\):把\((a,b),(c,d)\)为顶点的矩形区域内的所有数字加上\(v\). \(2~ ...
- POJ 2029 Get Many Persimmon Trees (模板题)【二维树状数组】
<题目链接> 题目大意: 给你一个H*W的矩阵,再告诉你有n个坐标有点,问你一个w*h的小矩阵最多能够包括多少个点. 解题分析:二维树状数组模板题. #include <cstdio ...
- 【bzoj3132】上帝造题的七分钟 二维树状数组区间修改区间查询
题目描述 “第一分钟,X说,要有矩阵,于是便有了一个里面写满了0的n×m矩阵. 第二分钟,L说,要能修改,于是便有了将左上角为(a,b),右下角为(c,d)的一个矩形区域内的全部数字加上一个值的操作. ...
- [bzoj3132]上帝造题的七分钟——二维树状数组
题目大意 你需要实现一种数据结构,支援以下操作. 给一个矩阵的子矩阵的所有元素同时加一个数. 计算子矩阵和. 题解 一看这个题,我就首先想到用线段树套线段树做. 使用二维线段树的错误解法 其实是第一次 ...
- P4514 上帝造题的七分钟——二维树状数组
P4514 上帝造题的七分钟 求的是矩阵里所有数的和: 维护四个树状数组: #include<cstdio> #include<cstring> #include<alg ...
随机推荐
- Azure Table Storage(一) : 简单介绍
Azure Table Storage是什么: Azure Table Storage是隶属于微软Azure Storage这个大服务下的一个子服务, 这个服务在Azure上算是老字号了, 个人大概在 ...
- wpf 通过为DataGrid所绑定的数据源类型的属性设置Attribute改变DataGrid自动生成列的顺序
环境Win10 VS2019 .Net Framework4.8 在wpf中,如果为一个DataGrid绑定到一个数据源,默认情况下DataGrid会为数据源类型的每个属性生成一个列(Column)对 ...
- MySQL的CURD 增删改查
添加 insert 语法: 单条:insert into 表名('字段1', '字段2', ...) values('值1', '值2', ...) 多条:insert into 表名('字段1', ...
- 【Linux】rsync错误解析
rsync: Failed to exec ssh: No such file or directory (2) rsync error: error in IPC code (code 14) at ...
- os.walk() 遍历目录下的文件夹和文件
os.walk(top, topdown=True, onerror=None, followlinks=False) top:顶级目录 os.walk()返回一个三元tupple(dirpath, ...
- 实现所有SAP设备打印机并行打印
SAP版本:ECC 6.0 701 1.如何实现所有SAP设备打印机并行打印? I.通过事务码:SPAD,进入假脱机管理初始屏幕.点击左上角的菜单中 配置(c)=>输出设备,进入SAP系统 输出 ...
- 【Redis系列】Spring boot实现监听Redis key失效事件
talk is cheap, show me the code. 一.开启Redis key过期提醒 方式二:修改配置文件 redis.conf # 默认 notify-keyspace-events ...
- U盘UEFI+GPT模式安装CentOS7.X系统
1.制作CentOS7安装盘 还是老套路,开局先制作安装盘,UltraISO软碟通,上图 (1) 打开UltraISO软件,选择"文件"-> "打开" ...
- 大数据系列2:Hdfs的读写操作
在前文大数据系列1:一文初识Hdfs中,我们对Hdfs有了简单的认识. 在本文中,我们将会简单的介绍一下Hdfs文件的读写流程,为后续追踪读写流程的源码做准备. Hdfs 架构 首先来个Hdfs的架构 ...
- 从软件(Java/hotspot/Linux)到硬件(硬件架构)分析互斥操作的本质
先上结论: 一切互斥操作的依赖是 自旋锁(spin_lock),互斥量(semaphore)等其他需要队列的实现均需要自选锁保证临界区互斥访问. 而自旋锁需要xcmpchg等类似的可提供CAS操作的硬 ...