南阳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 ...
随机推荐
- Docker学习笔记之基本命令使用
测试的环境为Ubuntu1804. 1. search命令搜索镜像 sudo docker search centos 搜索centos相关的镜像,可以看到第一个最多星的的centos是官方的镜像,而 ...
- mybatis入门教程之搭建一个简单的mybatis项目并启动它
一.准备条件: 1.依赖jar包:mybatis核心包(必须).lombok插件包(非必须)以及MySQL数据库连接驱动包(必须) <dependency> <groupId> ...
- 【Shell】使用awk sed获取一行内容的两个值
突然有需求需要一个脚本,同时获取到每一行数据的两个值,下面做了一个例子模板,仅供记录参考 cat test.txt id=1,name=zclinux1 id=2,name=zclinux2 id= ...
- 【Linux】快速创建文件的命令方法
[root@centos7 dir1]# ll total 0 -rw-r--r-- 1 root root 0 Aug 15 02:39 file1 -rw-r--r-- 1 root root 0 ...
- fileinput模块用法
fileinput模块功能: 提供拼接一个或多个文本文件的功能,可以通过使用for循环来读取一个或多个文本文件的所有行,从而进行逐行处理(如进行显示.替换.添加行号等). 其功能类似于linux命令的 ...
- python中json模块的使用
Python自带json模块,它有loads.dumps.load和dump这4个功能,用于Json格式字符串和Python数据类型间进行转换. 一.json.loads() 把Json格式字符串解码 ...
- ElasticSearch-IK分词器和集成使用
1.查询存在问题分析 在进行字符串查询时,我们发现去搜索"搜索服务器"和"钢索"都可以搜索到数据: 而在进行词条查询时,我们搜索"搜索"却没 ...
- Linux防火墙和iptables
1. CentOS 查看防火墙状态: systemctl status firewalld firewall-cmd --state 启停防火墙: # 开启 systemctl start firew ...
- JMeter性能测试9:阿里云服务器压测
第一步准备好jmeter脚本 第二步 将本地的jmeter脚本上传到阿里云进行验证 检验阿里云的jmeter是否能正常运行 将本地的脚本上传到阿里云使用xshell进入到该目录下 使用命令运行刚才上传 ...
- javax.servlet.ServletException: No adapter for handler
问题描述: 我的web.xml如下: <?xml version="1.0" encoding="UTF-8"?> <web-app xmln ...