南阳ccpc C题 The Battle of Chibi && hdu5542 The Battle of Chibi (树状数组优化+dp)
题意:
给你一个长度为n的数组,你需要从中找一个长度为m的严格上升子序列
问你最多能找到多少个
题解:
我们先对原序列从小到大排序,排序之后的序列就是一个上升序列
这里如果两个数相等的话,那么因为题目要我们求严格上升子序列,所以我们让这个数在数组中原来位置靠后的排序之后让它靠前(靠前也就是下标小)
我们dp方程:dp[i][j]表示截至到第i(这个i是按照没排序之前的下标)个元素,上升子序列长度为j的子序列能找到dp[i][j]个
dp转移方程:dp[i][j]=dp[1--i-1][j-1]
dp[1--i-1][j-1]就表示dp[1][j-1]+dp[2][j-1]+...+dp[i-1][j-1]
可以说就是求前缀和,这里用的是树状数组维护的
比如原序列为:3 11 5 2 6
排序后序列为:2 3 5 6 11
按照排序后这个顺序进行dp,当dp到11的时候,是dp[2][j]的值改变,序列中3,5,6的dp[i][j]中的i大于2,所以不会多求或者少求
而对于dp到6这个数,因为3,5,2这三个数在原序列中的位置就比它靠前,所以轮到求dp[5][j]的时候,2,3,5的dp值都已经求出来了
那么这个时候求出来的前缀和dp[1--4][j]就是正确的
这个自己可以模拟看下
代码:
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<queue>
#include<map>
#include<vector>
#include<cstring>
using namespace std;
const int mod=1e9+7;
const int maxn=1e3+5;
#define mem(a) memset(a,0,sizeof(a))
//求sum(dp[1-x][j])
int n,m,dp[maxn][maxn];
struct shudui
{
int id,val;
}que[maxn];
bool cmp(shudui x,shudui y)
{
if(x.val!=y.val)
return x.val<y.val;
return x.id>y.id; //如果两个val相等,因为题目要求严格递增,所以这样排序就可以满足题意
}
int lowbit(int x)
{
return x&(-x);
}
void update(int x,int y,int val) //更新包含dp[x][y]的
{ //后缀数组项
while(x<=n)
{
dp[x][y]=(dp[x][y]+val)%mod;
x+=lowbit(x);
}
}
int get_sum(int x,int y)
{
int sum=0;
while(x>0)
{
sum=(sum+dp[x][y])%mod;
x-=lowbit(x);
}
return sum;
}
int main()
{
int t,p=0;
scanf("%d",&t);
while(t--)
{
mem(dp);
scanf("%d%d",&n,&m);
for(int i=1;i<=n;++i)
{
scanf("%d",&que[i].val);
que[i].id=i;
}
sort(que+1,que+1+n,cmp);
for(int i=1;i<=n;++i)
{
for(int j=1;j<=m;++j)
{
if(j==1)
update(que[i].id,j,1);
else //因为我们按照val排过序了,所以我们可以加上前缀和就行
{
int sum=get_sum(que[i].id-1,j-1);
update(que[i].id,j,sum);
}
}
}
printf("Case #%d: %d\n",++p,get_sum(n,m));
}
return 0;
}
南阳ccpc C题 The Battle of Chibi && hdu5542 The Battle of Chibi (树状数组优化+dp)的更多相关文章
- HDU 6240 Server(2017 CCPC哈尔滨站 K题,01分数规划 + 树状数组优化DP)
题目链接 2017 CCPC Harbin Problem K 题意 给定若干物品,每个物品可以覆盖一个区间.现在要覆盖区间$[1, t]$. 求选出来的物品的$\frac{∑a_{i}}{∑b_ ...
- HDU 5542 - The Battle of Chibi - [离散化+树状数组优化DP]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5542 Problem DescriptionCao Cao made up a big army an ...
- 2018 CCPC网络赛 1010 hdu 6447 ( 树状数组优化dp)
链接:http://acm.hdu.edu.cn/showproblem.php?pid=6447 思路:很容易推得dp转移公式:dp[i][j] = max(dp[i][j-1],dp[i-1][j ...
- HDU 6447 - YJJ's Salesman - [树状数组优化DP][2018CCPC网络选拔赛第10题]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6447 Problem DescriptionYJJ is a salesman who has tra ...
- 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 ...
- HDU - 5542 The Battle of Chibi(LIS+树状数组优化)
The Battle of Chibi Cao Cao made up a big army and was going to invade the whole South China. Yu Zho ...
- The 2015 China Collegiate Programming Contest -ccpc-c题-The Battle of Chibi(hdu5542)(树状数组,离散化)
当时比赛时超时了,那时没学过树状数组,也不知道啥叫离散化(貌似好像现在也不懂).百度百科--离散化,把无限空间中无限的个体映射到有限的空间中去,以此提高算法的时空效率. 这道题是dp题,离散化和树状数 ...
- hdu5542 The Battle of Chibi【树状数组】【离散化】
The Battle of Chibi Time Limit: 6000/4000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Othe ...
- 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 Zho ...
随机推荐
- VRay for SketchUp渲染图黑原因及解决方案
很多人都遇到用Vray for SketchUp云渲染的时候,渲染出来的图片是全黑或者是局部是黑色, 这是什么原因呢? 1.有一种情况是,SketchUp的文件储存机制和其他的软件有些不同,它是把模型 ...
- JavaFX之班级未交作业统计
前言 最近转移了系统平台,用上了Ubuntu1804版本系统,原来用C#写的Windows窗体软件也不能用了,而且自己在班级上每周都需要收作业,所以写了这个软件.这篇博客主要记录这个JavaFX应用的 ...
- QA职责
- python函数3-函数嵌套/递归/匿名函数
2 .函数递归: 3.匿名函数
- ctfhub技能树—web前置技能—http协议—Cookie
打开靶机环境 查看显示内容 根据提示,需要admin登录才能得到flag 题目介绍为Cookie欺骗.认证.伪造 介绍一下cookie和session 一.cookie: 在网站中,http请求是无状 ...
- 分布式 ID 生成算法 — SnowFlake
一.概述 分布式 ID 生成算法的有很多种,Twitter 的 SnowFlake 就是其中经典的一种. SnowFlake 算法生成 ID 的结果是一个 64bit 大小的整数,它的结构如下图: 1 ...
- PeleeNet:精修版DenseNet,速度猛增至240FPS | NeurIPS 2018
PeleeNet是DenseNet的一个变体,没有使用流行的深度可分离卷积,PeleeNet和Pelee仅通过结构上的优化取得了很不错的性能和速度,读完论文可以学到很多网络设计的小窍门. 来源:晓 ...
- RecyclerView 源码分析(二) —— 缓存机制
在前一篇文章 RecyclerView 源码分析(一) -- 绘制流程解析 介绍了 RecyclerView 的绘制流程,RecyclerView 通过将绘制流程从 View 中抽取出来,放到 Lay ...
- 微服务网关2-搭建Gateway服务
一.创建父模块infrastructure 1.创建模块 在guli_parent下创建普通maven模块 Artifact:infrastructure 2.删除src目录 二.创建模块api_ga ...
- RPC 实战与原理 精简版
什么是 RPC? RPC 有什么作用? RPC 步骤 为什么需要序列化? 零拷贝 什么是零拷贝? 为什么需要零拷贝? 如何实现零拷贝? Netty 的零拷贝有何不同? 动态代理实现 HTTP/2 特性 ...