南阳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 ...
随机推荐
- JavaScript 获取当天0点以及当前时间方法
js 取得今天0点: const start = new Date(new Date(new Date().toLocaleDateString()).getTime()); console.log( ...
- LeetCode141-环形链表检测
题目 给定一个链表,判断链表中是否有环. 如果链表中有某个节点,可以通过连续跟踪 next 指针再次到达,则链表中存在环. 为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置 ...
- 串的模式匹配算法1 BF算法
BF算法 字符串的模式匹配不一定要从主串的第一个位置开始,可以指定主串中查找的起始位置 pos. 2. 算法步骤: 1)分别利用计数器指针 i 和 j 指定主串和模式串即小字符串待比较的位置,初始化为 ...
- Hive Query生命周期 —— 钩子(Hook)函数篇
无论你通过哪种方式连接Hive(如Hive Cli.HiveServer2),一个HQL语句都要经过Driver的解析和执行,主要涉及HQL解析.编译.优化器处理.执行器执行四个方面. 以Hive目前 ...
- 【MySQL】一台服务器上搭建两个mysql节点
环境: CentOS 6.8 memory:1G Mysql 5.7 二进制安装包 1.安装相关的环境包 yum -y install gcc glibc libaio libstdc++ libs ...
- 【Oracle】CBO优化详解
SQL优化是数据优化的重要方面,本文将分析Oracle自身的CBO优化,即基于成本的优化方法.Oracle为了自动的优化sql语句需要各种统计数据作为优化基础.外面会通过sql的追踪来分析sql的执行 ...
- mysql锁表问题
查看当前所有的进程信息: show full processlist; 查看当前所有的事务 select * from information_schema.innodb_trx; 查看当前出现的锁 ...
- [Usaco2007 Jan]Balanced Lineup 飞盘比赛
题目描述 每天,农夫 John 的N(1 <= N <= 50,000)头牛总是按同一序列排队. 有一天, John 决定让一些牛们玩一场飞盘比赛. 他准备找一群在对列中为置连续的牛来进行 ...
- 打包遇到错误Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.22.2:test
引自:https://blog.csdn.net/xiexiangyan/article/details/107936774 遇到的问题 有一个maven项目,我clone一下最新的代码.准备打包(m ...
- JVM学习-运行时数据区域
目录 前言 运行时数据区 程序计数器 Java虚拟机栈 局部变量表 基础数据类型 对象引用 returnAddress 操作数栈 动态链接 方法返回地址 Java堆 方法区 类型信息 字段描述符 方法 ...