【HDOJ5542】The Battle of Chibi
我们定义f[i][j]表示前j个数组成的长度为i的合法序列的个数,那么有
因此我们不难写出朴素dp的代码
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 using namespace std; 6 typedef long long ll; 7 int T,n,m,a[1100],ans; 8 int f[1100][1100]; 9 const int mod=1000000007; 10 inline int read() { 11 int ret=0; 12 int op=1; 13 char c=getchar(); 14 while(c<'0'||c>'9') {if(c=='-') op=-1; c=getchar();} 15 while(c<='9'&&c>='0') ret=ret*10+c-'0',c=getchar(); 16 return ret*op; 17 } 18 int main() { 19 T=read(); 20 for(int t=1;t<=T;t++) { 21 memset(a,0,sizeof(a)); 22 memset(f,0,sizeof(f)); 23 n=read(); m=read(); 24 for(int i=1;i<=n;i++) a[i]=read(); 25 f[0][0]=1; 26 a[0]=-(1<<30); 27 ans=0; 28 for(int i=1;i<=m;i++) 29 for(int j=1;j<=n;j++) 30 for(int k=0;k<j;k++) 31 if(a[k]<a[j]) f[i][j]=(f[i][j]+f[i-1][k])%mod; 32 for(int i=1;i<=n;i++) ans=(ans+f[m][i])%mod; 33 printf("Case #%d: %d\n",t,ans); 34 } 35 return 0; 36 }
TLE Code
然而,这种做法的时间复杂度较大,无法通过本题,因此我们考虑优化。
我们将序列A的值离散化,用num[i]表示i离散化后的值,另外,我们令A0=-∞,num[A0]=1,然后建立树状数组维护即可。
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 using namespace std; 6 inline int read() { 7 int ret=0; 8 int op=1; 9 char c=getchar(); 10 while(c<'0'||c>'9') {if(c=='-') op=-1; c=getchar();} 11 while(c<='9'&&c>='0') ret=ret*10+c-'0',c=getchar(); 12 return ret*op; 13 } 14 int T,n,m,a[1100],b[1100],num[1100]; 15 int f[1100][1100]; 16 int sum[1100]; 17 const int mod=1000000007; 18 #define lowbit(x) ((-x)&(x)) 19 int query(int x) { 20 int ret=0; 21 while(x>0) { 22 ret=(ret+sum[x])%mod; 23 x-=lowbit(x); 24 } 25 return ret; 26 } 27 void add(int x,int val) { 28 while(x<=n) { 29 sum[x]=(sum[x]+val)%mod; 30 x+=lowbit(x); 31 } 32 } 33 int main() { 34 T=read(); 35 for(int t=1;t<=T;t++) { 36 memset(a,0,sizeof(a)); 37 memset(f,0,sizeof(f)); 38 memset(b,0,sizeof(b)); 39 memset(num,0,sizeof(num)); 40 n=read(); m=read(); 41 for(int i=1;i<=n;i++) { 42 a[i]=read(); 43 b[i]=a[i]; 44 } 45 a[0]=b[n+1]=-(1<<30); 46 sort(b+1,b+n+1+1); 47 for(int i=0;i<=n;i++) num[i]=lower_bound(b+1,b+n+1,a[i])-b; 48 f[0][0]=1; 49 for(int i=1;i<=m;i++) { 50 memset(sum,0,sizeof(sum)); 51 add(num[0],f[i-1][0]); 52 for(int j=1;j<=n;j++) { 53 f[i][j]=query(num[j]-1); 54 add(num[j],f[i-1][j]); 55 } 56 } 57 int ans=0; 58 for(int i=1;i<=n;i++) ans=(ans+f[m][i])%mod; 59 printf("Case #%d: %d\n",t,ans); 60 } 61 return 0; 62 }
AC Code
转载于:https://www.cnblogs.com/shl-blog/p/10988268.html
【HDOJ5542】The Battle of Chibi相关推荐
- 【HDU 5542】The Battle of Chibi (dp,树状数组优化)
目录 题目 Description Input Output Sample Input Sample Output Hint Source 思路 朴素dp 优化 代码 题目 Description C ...
- 【解析】1013 Battle Over Cities (25 分)_31行代码AC
立志用最少的代码做最高效的表达 PAT甲级最优题解-->传送门 It is vitally important to have all the cities connected by highw ...
- HDU-5542-The Battle of Chibi【树状数组+dp】
HDU-5542-The Battle of Chibi[树状数组+dp] Time Limit: 6000/4000 MS (Java/Others) Memory Limit: 65535/655 ...
- 【HDOJ图论题集】【转】
1 =============================以下是最小生成树+并查集====================================== 2 [HDU] 3 1213 How ...
- 【转载】图论 500题——主要为hdu/poj/zoj
转自--http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并 ...
- 【机器学习】深入理解CatBoost
本文主要内容概览: 1. CatBoost简介 CatBoost是俄罗斯的搜索巨头Yandex在2017年开源的机器学习库,是Boosting族算法的一种.CatBoost和XGBoost.Light ...
- 293、Java中级10 -【多线程】 2020.03.31
0.目录 1.多线程 2.线程概念 3.创建多线程-继承线程类 4.创建多线程-实现Runnable接口 5.创建多线程-匿名类 6.创建多线程的三种方式 7.参考链接 1.多线程 多线程即在同一时间 ...
- 小啊呜产品读书笔记001:《邱岳的产品手记-07》第13讲 无用却必要:产品规划【上】 第14讲 留白与节奏:产品规划【下】
小啊呜产品读书笔记001:<邱岳的产品手记-07>第13讲 无用却必要:产品规划[上] & 第14讲 留白与节奏:产品规划[下] 一.今日阅读计划 二.泛读&知识摘录 1. ...
- POJ前面的题目算法思路【转】
1000 A+B Problem 送分题 49% 2005-5-7 1001 Exponentiation 高精度 85% 2005-5-7 1002 487-3279 n/a 90% 2005-5- ...
最新文章
- C#实现的18位×××格式验证算法
- postman的使用方法详解!最全面的教程
- db2 如何导出insert语句_MySQL 执行脚本/导入/导出数据
- 【转载】分布式事务 介绍
- [转]js判断url是否有效
- Logtail 混合模式:使用插件处理文件日志
- JavaFx 实现画图工具
- 14、数 据 库:破解还原Access数据库密码
- 深入浅出MFC:动态创建控件
- 快手滑块验证码分析 2022/03/17
- 主析取范式和主合取范式
- Excel知识技能汇总
- 安装windows XP或者2003时提示找不到硬盘驱动器的解决办法(总结)转载
- 应届生面试的5大技巧,附600字自我介绍范文
- 在平板电脑与移动3G大爆炸的时代,昔日霸主微软的反击
- 7.Android常用第三方支付
- 直击文印痛点 中小企业需要这样一台复合机
- HTML5 中 40 个最重要的技术点
- Linux Chromium源码编译
- 机器学习:随机森林原理 OOB等
热门文章
- linux websocket服务安全组,在 linux 下安装并使用 websocket
- php短网址案例,php 短网址小例子
- Android 解锁app,应用锁「AppLock」v3.3.0 for Android 完美解锁版
- 多校 HDU 6313 Hack It——构造
- 多校训练 Naive Operations线段树区间更新
- mysql_ init数据类型_mysql数据类型
- OpenGL基础10:变换
- 2017百度之星初赛:A-1005. 今夕何夕
- 最大-最小 滤波器实现 及用于边缘检测
- [深度学习] 深度学习常见概念