CSUOJ 1551 Longest Increasing Subsequence Again

1551: Longest Increasing Subsequence Again

Time Limit: 2 Sec  Memory Limit: 256 MB
Submit: 75  Solved: 52

Description

Give you a numeric sequence. If you can demolish arbitrary amount of numbers, what is the length of the longest increasing sequence, which is made up of consecutive numbers? It sounds like Longest Increasing Subsequence at first sight. So, there is another limitation: the numbers you deleted must be consecutive.

Input

There are several test cases.
For each test case, the first line of input contains the length of sequence N(1≤N≤10^4). The second line contains the elements of sequence——N positive integers not larger than 10^4.

Output

For each the case, output one integer per line, denoting the length of the longest increasing sequence of consecutive numbers, which is achievable by demolishing some(may be zero) consecutive numbers.

Sample Input

7
1 7 3 5 9 4 8
6
2 3 100 4 4 5

Sample Output

4
4

HINT

Source

解题:线段树。。

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int maxn = 10005;
 4 struct node{
 5     int lt,rt,value;
 6 }tree[maxn<<2];
 7 struct Block {
 8     int lt,rt;
 9 } block[maxn];
10 void build(int lt,int rt,int v) {
11     tree[v].lt = lt;
12     tree[v].rt = rt;
13     tree[v].value = 0;
14     if(lt == rt) return;
15     int mid = (lt + rt)>>1;
16     build(lt,mid,v<<1);
17     build(mid+1,rt,v<<1|1);
18 }
19 int query(int id,int v) {
20     if(tree[v].lt == tree[v].rt) return 0;
21     int mid = (tree[v].lt + tree[v].rt)>>1;
22     if(id <= mid) return max(query(id,v<<1),tree[v<<1|1].value);
23     return query(id,v<<1|1);
24 }
25 void update(int id,int v,int value) {
26     int mid = (tree[v].lt + tree[v].rt)>>1;
27     tree[v].value = max(tree[v].value,value);
28     if(tree[v].lt == tree[v].rt) return;
29     if(id <= mid) update(id,v<<1,value);
30     else update(id,v<<1|1,value);
31 }
32 int n,m,d[maxn],discrete[maxn],width[maxn];
33 int main() {
34     while(~scanf("%d",&n)) {
35         for(int i = m = 0; i < n; ++i) {
36             scanf("%d",d+i);
37             discrete[i] = d[i];
38         }
39         sort(discrete,discrete+n);
40         int len = unique(discrete,discrete+n) - discrete;
41         build(0,len-1,1);
42         block[m].lt = block[m].rt = 0;
43         for(int i = 1; i < n; ++i)
44             if(d[i-1] < d[i]) block[m].rt++;
45             else {
46                 ++m;
47                 block[m].lt = block[m].rt=i;
48             }
49         for(int i = 0; i <= m; ++i)
50             for(int j = block[i].rt; j >= block[i].lt; --j)
51                 width[j] = block[i].rt-j+1;
52         int ans = 0;
53         for(int i = m; i >= 0; --i) {
54             for(int j = block[i].rt; j >= block[i].lt; --j) {
55                 int id = lower_bound(discrete,discrete+len,d[j])-discrete;
56                 ans = max(j - block[i].lt + 1 + query(id,1),ans);
57                 update(id,1,width[j]);
58             }
59         }
60         printf("%d\n",ans);
61     }
62     return 0;
63 }

时间: 2024-12-15 07:14:38

CSUOJ 1551 Longest Increasing Subsequence Again的相关文章

中南OJ1551: Longest Increasing Subsequence Again(分块+离散化线段树)

1551: Longest Increasing Subsequence Again Time Limit: 2 Sec  Memory Limit: 256 MB Submit: 29  Solved: 15 [Submit][Status][Web Board] Description Give you a numeric sequence. If you can demolish arbitrary amount of numbers, what is the length of the

300. Longest Increasing Subsequence

Problem statement: Given an unsorted array of integers, find the length of longest increasing subsequence. For example,Given [10, 9, 2, 5, 3, 7, 101, 18],The longest increasing subsequence is [2, 3, 7, 101], therefore the length is 4. Note that there

SPOJ LIS2 Another Longest Increasing Subsequence Problem 三维偏序最长链 CDQ分治

Another Longest Increasing Subsequence Problem Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/problem/visitOriginUrl.action?id=19929 Description Given a sequence of N pairs of integers, find the length of the longest incre

Dynamic Programming | Set 3 (Longest Increasing Subsequence)

在 Dynamic Programming | Set 1 (Overlapping Subproblems Property) 和 Dynamic Programming | Set 2 (Optimal Substructure Property) 中我们已经讨论了重叠子问题和最优子结构性质,现在我们来看一个可以使用动态规划来解决的问题:最长上升子序列(Longest Increasing Subsequence(LIS)). 最长上升子序列问题,致力于在一个给定的序列中找到一个最长的子序列

LintCode Longest Increasing Subsequence

Given a sequence of integers, find the longest increasing subsequence (LIS). You code should return the length of the LIS. Clarification What's the definition of longest increasing subsequence? The longest increasing subsequence problem is to find a

Longest Increasing Subsequence

Longest Increasing Subsequence(LIS) 一个美丽的名字 非常经典的线性结构dp [朴素]:O(n^2) d(i)=max{0,d(j) :j<i&&a[j]<a[i]}+1 直接两个for [二分查找优化]:O(n^2) g(i):d值为i的最小的a  每次更新然后lower_bound即可 [大于等于] lower_boundReturn iterator to lower bound Returns an iterator pointing

[LeetCode][JavaScript]Longest Increasing Subsequence

Longest Increasing Subsequence Given an unsorted array of integers, find the length of longest increasing subsequence. For example,Given [10, 9, 2, 5, 3, 7, 101, 18],The longest increasing subsequence is [2, 3, 7, 101], therefore the length is 4. Not

LeetCode 300. Longest Increasing Subsequence

300. Longest Increasing Subsequence Description Submission Solutions Add to List Total Accepted: 64115 Total Submissions: 170859 Difficulty: Medium Contributors: Admin Given an unsorted array of integers, find the length of longest increasing subsequ

【LeetCode从零单刷】Longest Increasing Subsequence

题目: Given an unsorted array of integers, find the length of longest increasing subsequence. For example, Given [10, 9, 2, 5, 3, 7, 101, 18], The longest increasing subsequence is [2, 3, 7, 101], therefore the length is 4. Note that there may be more