BNUOJ 14381 Wavio Sequence
Wavio Sequence
This problem will be judged on UVA. Original ID: 10534
64-bit integer IO format: %lld Java class name: Main
Wavio is a sequence of integers. It has some interesting properties.
Wavio is of odd length i.e. L = 2*n + 1.
The first (n+1) integers of Wavio sequence makes a strictly increasing sequence.
The last (n+1) integers of Wavio sequence makes a strictly decreasing sequence.
No two adjacent integers are same in a Wavio sequence.
For example 1, 2, 3, 4, 5, 4, 3, 2, 0 is an Wavio sequence of length 9. But 1, 2, 3, 4, 5, 4, 3, 2, 2 is not a valid wavio sequence. In this problem, you will be given a sequence of integers. You have to find out the length of the longest Wavio sequence which is a subsequence of the given sequence. Consider, the given sequence as :
1 2 3 2 1 2 3 4 3 2 1 5 4 1 2 3 2 2 1.
Here the longest Wavio sequence is : 1 2 3 4 5 4 3 2 1. So, the output will be 9.
Input
The input file contains less than 75 test cases. The description of each test case is given below: Input is terminated by end of file.
Each set starts with a postive integer, N(1<=N<=10000). In next few lines there will be N integers.
Output
For each set of input print the length of longest wavio sequence in a line.
Sample Input
- 10
- 1 2 3 4 5 4 3 2 1 10
- 19
- 1 2 3 2 1 2 3 4 3 2 1 5 4 1 2 3 2 2 1
- 5
- 1 2 3 4 5
Sample Output
- 9
- 9
- 1
解题:最长上升子序列加强版。单调队列优化!!!重点。先顺着求最长上升子序列,再逆着求最长上升子序列。
- #include <iostream>
- #include <cstdio>
- #include <cstring>
- #include <cmath>
- #include <algorithm>
- #include <climits>
- #include <vector>
- #include <queue>
- #include <cstdlib>
- #include <string>
- #include <set>
- #include <stack>
- #define LL long long
- #define pii pair<int,int>
- #define INF 0x3f3f3f3f
- using namespace std;
- const int maxn = ;
- int dp1[maxn],dp2[maxn],d[maxn],q[maxn];
- int bsearch(int lt,int rt,int val) {
- while(lt <= rt) {
- int mid = (lt+rt)>>;
- if(q[mid] < val) lt = mid+;//严格上升单调取少于符号,上升的取少于等于
- else rt = mid-;
- }
- return lt;
- }
- int main() {
- int n,i,j,head,tail;
- while(~scanf("%d",&n)) {
- for(i = ; i < n; i++)
- scanf("%d",d+i);
- head = tail = ;
- for(i = ; i < n; i++) {
- if(head == tail) {
- q[head++] = d[i];
- dp1[i] = head-tail;
- }else if(d[i] > q[head-]){
- q[head++] = d[i];
- dp1[i] = head-tail;
- }else{
- int it = bsearch(tail,head-,d[i]);
- dp1[i] = it - tail + ;
- q[it] = d[i];
- }
- }
- head = tail = ;
- for(i = n-; i >= ; i--) {
- if(head == tail) {
- q[head++] = d[i];
- dp2[i] = head-tail;
- }else if(d[i] > q[head-]){
- q[head++] = d[i];
- dp2[i] = head-tail;
- }else{
- int it = bsearch(tail,head-,d[i]);
- dp2[i] = it - tail + ;
- q[it] = d[i];
- }
- }
- int ans = ;
- for(i = ; i < n; i++){
- ans = max(ans,min(dp1[i],dp2[i])*-);
- }
- printf("%d\n",ans);
- }
- return ;
- }
BNUOJ 14381 Wavio Sequence的更多相关文章
- UVA 10534 三 Wavio Sequence
Wavio Sequence Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit Sta ...
- HOJ 2985 Wavio Sequence(最长递增子序列以及其O(n*logn)算法)
Wavio Sequence My Tags (Edit) Source : UVA Time limit : 1 sec Memory limit : 32 M Submitted : 296, A ...
- UVa 10534 Wavio Sequence (最长递增子序列 DP 二分)
Wavio Sequence Wavio is a sequence of integers. It has some interesting properties. · Wavio is of ...
- LIS UVA 10534 Wavio Sequence
题目传送门 题意:找对称的,形如:123454321 子序列的最长长度 分析:LIS的nlogn的做法,首先从前扫到尾,记录每个位置的最长上升子序列,从后扫到头同理.因为是对称的,所以取较小值*2-1 ...
- uva 10534 Wavio Sequence LIS
// uva 10534 Wavio Sequence // // 能够将题目转化为经典的LIS. // 从左往右LIS记作d[i],从右往左LIS记作p[i]; // 则最后当中的min(d[i], ...
- UVA10534:Wavio Sequence(最长递增和递减序列 n*logn)(LIS)好题
题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=68553#problem/B 题目要求: Wavio是一个整数序列,具有以下特性 ...
- BNUOJ 1260 Brackets Sequence
Brackets Sequence Time Limit: 1000ms Memory Limit: 65536KB This problem will be judged on PKU. Origi ...
- UVa10534 - Wavio Sequence(LIS)
题目大意 给定一个长度为n的整数序列,求个最长子序列(不一定连续),使得该序列的长度为奇数2k+1,前k+1个数严格递增,后k+1个数严格递减.注意,严格递增意味着该序列中的两个相邻数不能相同.n&l ...
- 1421 - Wavio Sequence
题目大意:求一个序列中 先严格递增后严格递减的子序列的数目(要求这个子序列对称). 题目思路:正一遍DP,反一遍DP,因为n<=1e5,dp要把时间压缩到nlogn #include<st ...
随机推荐
- centos7的systemd
系统启动流程 POST --> Boot Sequence --> Bootloader --> kernel+initramfs(initrd) --> rootfs --& ...
- Linux 进程间通讯方式 pipe()函数 (转载)
转自:http://blog.csdn.net/ta893115871/article/details/7478779 Linux 进程间通讯方式有以下几种: 1->管道(pipe)和有名管道( ...
- 在sql语句中使用关键字
背景 开发过程中遇到了遇到了一句sql语句一直报错,看了一下字段名和表名都对应上了,但是还是一直报错 sql语句如下: update table set using = ""hh ...
- 第四代增强 源代码增强(ABAP Source Code Enhancements)
显式代码增强的创建 se38打开你要增强的程序 进入编辑状态 在菜单栏选择: Edit->Enhancement Opreations->Create option. 此时弹出Create ...
- ssm lodop打印图片不显示
在打印预览的时候图片就是不显示 最终解决方案就是修改过滤器
- MVC、MVP和MVVM的图示
一.MVC MVC模式的意思是,软件可以分成三个部分. 视图(View):用户界面. 控制器(Controller):业务逻辑 模型(Model):数据保存 各部分之间的通信方式如下. View 传送 ...
- 莫队算法/二分查找 FZU 2072 Count
题目传送门 题意:问区间内x的出现的次数分析:莫队算法:用一个cnt记录x的次数就可以了.还有二分查找的方法 代码: #include <cstdio> #include <algo ...
- 模拟 URAL 1149 Sinus Dances
题目传送门 /* 模拟:找到规律分别输出就可以了,简单但是蛮有意思的 */ #include <cstdio> #include <algorithm> #include &l ...
- python使用mysql connection获取数据感知不到数据变化问题
在做数据同步校验的时候,需要从mysql fetch数据和hbase的数据进行对比,发现即使mysql数据变化了,类似下面的代码返回的值还是之前的数据.抽取的代码大概如下: import MySQL ...
- Scala-基础-运算符
import junit.framework.TestCase /** * 运算符 */ class Demo3 extends TestCase { def test_+ { var x = 10; ...