HDOJ5256 序列变换(dp)
2015-08-22 00:39
260 查看
序列变换
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 854 Accepted Submission(s): 345
Problem Description
我们有一个数列A1,A2...An,你现在要求修改数量最少的元素,使得这个数列严格递增。其中无论是修改前还是修改后,每个元素都必须是整数。
请输出最少需要修改多少个元素。
Input
第一行输入一个T(1≤T≤10),表示有多少组数据
每一组数据:
第一行输入一个N(1≤N≤105),表示数列的长度
第二行输入N个数A1,A2,...,An。
每一个数列中的元素都是正整数而且不超过106。
Output
对于每组数据,先输出一行
Case #i:
然后输出最少需要修改多少个元素。
Sample Input
2 2 1 10 3 2 5 4
Sample Output
Case #1: 0 Case #2: 1
LIS题目,运用了STL中的upper_bound(),返回第一个>=key的位置。
AC代码:
#include "iostream" #include "cstdio" #include "cstring" #include "algorithm" using namespace std; const int MAXN = 100005; const int INF = 0x3f3f3f3f; int n, a[MAXN], b[MAXN]; int main(int argc, char const *argv[]) { int t; scanf("%d", &t); for(int cas = 1; cas <= t; ++cas) { memset(b, INF, sizeof(b)); scanf("%d", &n); for(int i = 1; i <= n; ++i) { scanf("%d", &a[i]); a[i] -= i; } int ans = 0; for(int i = 1; i <= n; ++i) { int x = upper_bound(b + 1, b + 1 + n, a[i]) - b; b[x] = a[i]; ans = max(ans, x); } printf("Case #%d:\n%d\n", cas, n - ans); } return 0; }
相关文章推荐
- Python pip – error: invalid command ‘bdist_wheel’
- JQuery选择器操作
- 兄弟间的事,筹栎间的那点事
- Sublime Text 快捷键
- 摩天大厦不可伸缩
- [IOS]CoreAnimation基础[翻译6部分]
- git——OS X创建仓库以及链接github
- java中volatile关键字的含义
- 面试题:冒泡排序,选择排序,插入排序,快速排序,归并排序,堆排序
- ASP.NET——生成验证码
- 在VirtualBox上面安装Ubuntu 14.04遇到的问题及对策
- Java内存模型
- MySQL安装图解_学习笔记
- Android应用界面组件
- UEFI+GPT安装kila linux2.0和win8.1双系统
- Leetcode_Palindrome Partitioning
- linux系统硬件配置查看方法
- Mini-project # 4 - "Pong"
- jquery的children方法和css3选择器配合使用
- javadbf中文乱码