dp(第一次尝试)
2016-05-21 19:46
225 查看
题目:点击打开链接
代码:
的值的位置,其实也不是 特别懂,反正是看了别人的代码,总体上思路也不是特别的明确。
代码:
#include<iostream> #include<cstdio> #include<cstring> #include <cmath> #include<stack> #include<vector> #include<map> #include<set> #include<queue> #include<algorithm> using namespace std; typedef long long LL; const int INF = (1<<30)-1; const int mod=1000000007; const int maxn=1000005; int a[maxn],f[maxn]; int main() { int T; scanf("%d",&T); int kase=0; while(T--) { int n; scanf("%d",&n); for(int i=1; i<=n; i++) { scanf("%d",&a[i]); a[i]=a[i]-i; } int len=1; f[1]=a[1]; for(int i=2; i<=n; i++) { if(a[i]>=f[len]) f[++len]=a[i];//这里也和求最长上升子序列不同,是大于等于 else { int pos=upper_bound(f+1,f+len+1,a[i])-f;//upper_bound返回的是序列中严格大于key值的第一个数 f[pos]=a[i]; } } printf("Case #%d:\n",++kase); printf("%d\n",n-len); } return 0; }其实这个dp我并不是特别擅长,但是这个代码我看的有点懂,但不是特别的懂,只是能够推出来这是对的,反正就是感觉比较厉害,这里面还有一个函数,就是lower_bound返回的是第一个大于等于该值的位置,而upper_bound是返回第一个大于该值的
的值的位置,其实也不是 特别懂,反正是看了别人的代码,总体上思路也不是特别的明确。
相关文章推荐
- 关于java中转意字符\\\\的用法探究
- Spring之Referenced file contains error问题的解决
- Html(二)
- 求阶层的优化
- POJ-2421-Constructing Roads(最小生成树 普利姆)
- HDU 5690 查找循环节 数学公式快速幂+乘法逆元(除法取模)
- MySQL数据库解压缩版(免安装版或zip版)无法输入中文,以及与Navicat中文显示一致的问题
- codeforces 670D1 Magic Powder - 1
- 九度OJ 1001:A+B for Matrices
- 在Android开发中使用MVP模式
- 中继器、集线器、网桥、交换机各个组件作用(转))
- 快速排序的分析与实现
- JS学习15(HTML5脚本编程)
- PAT 1015 德才论
- java抽象类练习
- asp.net发布到IIS中出现错误:处理程序“PageHandlerFactory-Integrated”在其模块列表中有一个错误模块“ManagedPipelineHandler”
- 解除百度云浏览器端对下载大文件的限制
- 坐标转换-终结者--OpenCoord提供下载了
- 2016年上半年软考网络工程师考试下午试题参考答案第一时间发布
- mina解决粘包,找不到解码器,数据帧重传的问题