南阳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 ...
随机推荐
- 【JS学习】var let const声明变量的异同点
[JS学习]var let const声明变量的异同点 前言: 本博客系列为学习后盾人js教程过程中的记录与产出,如果对你有帮助,欢迎关注,点赞,分享.不足之处也欢迎指正,作者会积极思考与改正. 总述 ...
- python模块详解 | psutil
目录 psutil 简介 psutil的功能函数 cpu memory_内存 disk_磁盘 net_网络 pid_进程管理 sensors_传感器 其他(用户,启动时间) psutil简介 psut ...
- 目录遍历 - Pikachu
概述: 在web功能设计中,很多时候我们会要将需要访问的文件定义成变量,从而让前端的功能便的更加灵活. 当用户发起一个前端的请求时,便会将请求的这个文件的值(比如文件名称)传递到后台,后台再执行其对应 ...
- ctfhub技能树—RCE—过滤cat
打开靶机 查看页面信息 构造payload 127.0.0.1 || ls 题目提示过滤了cat,但我还是想试试 果然不行 网页访问没有结果,应该和上题一样被注释了,使用和同样的方法进行解题 利用命令 ...
- 【葵花宝典】kolla部署OpenStack-AllinOne
1.关闭防火墙以及内核安全机制 systemctl stop firewalld systemctl disable firewalld ##永久性关闭 setenforce 0 sed -i 's/ ...
- Ribbon负载均衡服务调用
1.在听周阳老师讲解时,使用Ribbon核心组件IRule时是这样用的: ribbon版本 : 自定义配置类不能放在@ComponentScan所扫描的当前包下以及子包下,项目结构如下 MySelfR ...
- 【中文】【deplearning.ai】【吴恩达课后作业目录】
[目录][吴恩达课后作业目录] 吴恩达深度学习相关资源下载地址(蓝奏云) 课程 周数 名称 类型 语言 地址 课程1 - 神经网络和深度学习 第1周 深度学习简介 测验 中英 传送门 无编程作业 编程 ...
- uni-app开发经验分享十一: uniapp iOS云打包修改权限提示语
打包提交appstore如果用到了如下权限需要修改提示语,详细描述使用这个权限的原因,如不修改提示语appstore审核可能会被拒绝.Apple的原则是,如果一个app想要申请用户同意某个隐私信息访问 ...
- ORB-SLAM2-tracking线程
tracking线程 Tracking线程的主要工作是从图像中提取ORB特征,根据上一帧进行姿态估计或者进行通过全局重定位初始化位姿,然后跟踪已经重建的局部地图,优化位姿,再根据一些规则确定新的关键帧 ...
- Python_ 1生成器(上)初识生成器
引言:列表生成式 现在有个需求,给定列表[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],现在要求你把列表里的每个值加1,你怎么实现?你可能会想到2种方式 1 >>> a ...