单调栈+贪心维护LIC

普通:O(\(N^2\))

状态:dp[j]表示,以j结尾的最长的上升子序列
转移:dp[j]=dp[i]+1(if a[j]>a[i] )
初始化:dp[i]=1

优化(nlogn)

solution:维护stack[top]表示长度为top的最长子序列结尾最小的是stack[top]

贪心+dp

code:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int MAXX=100010;
int a[MAXX],stack[MAXX];
int top,n;
int main(){
    scanf("%d",&n);
    for(int i=1;i<=n;++i)scanf("%d",&a[i]);
    for(int i=1;i<=n;++i){
     if(a[i]>stack[top])stack[++top]=a[i];
     else {
      int pos=lower_bound(stack+1,stack+top+1,a[i])-stack;
      stack[pos]=a[i];
     }
    }
    cout<<top;
    return 0;
}

原文地址:https://www.cnblogs.com/ARTlover/p/9538393.html

时间: 2024-08-05 17:04:01

单调栈+贪心维护LIC的相关文章

【单调栈维护连续区间】2019.1.18模拟赛T2 浇花

这道题是一道单调栈的题 1 题目描述 2 JDFZ在餐厅门前种了一排nn棵花,每棵花都有一个高度.浇花大爷会枚举所有的区间,然后从区间中找出一个高度最矮的花进行浇水.由于浇花大爷浇完水之后就精疲力竭了,所以请你帮助他计算每棵花都被浇了几次水. 3 4 输入格式 5 第一行一个整数nn. 第二行nn个整数,分别表示每棵花的高度. 6 7 输出格式 8 一行nn个整数用空格隔开,分别表示每棵花被浇了几次水. 9 10 样例一 11 input 12 3 13 1 3 5 14 output 15 3

HDU 5033---Building(单调栈)

题目链接 Problem Description Once upon a time Matt went to a small town. The town was so small and narrow that he can regard the town as a pivot. There were some skyscrapers in the town, each located at position xi with its height hi. All skyscrapers loc

poj 3250 Bad Hair Day (单调栈)

Bad Hair Day Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 14883   Accepted: 4940 Description Some of Farmer John's N cows (1 ≤ N ≤ 80,000) are having a bad hair day! Since each cow is self-conscious about her messy hairstyle, FJ wants

HDOJ 4252 A Famous City 单调栈

单调栈: 维护一个单调栈 A Famous City Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1671    Accepted Submission(s): 644 Problem Description After Mr. B arrived in Warsaw, he was shocked by the skyscrap

hdu3410 Passing the Message 单调栈

// hdu3410 Passing the Message 单调栈 // 题目意思:给你n个数,询问第i个数直到左边比它本身大的第一个数的这段 // 区间内求一个最大的值 和 直到右边比它本身大的数的第一个数的这段区间内 // 再求一个最大值. // 解题方法: // 单调栈,维护一个栈,使得站内元素单调递减即离栈顶越近,值越小 // 从左往右扫一遍,最后一个比当前元素小的数组下标(出栈的元素)就是我们要求 // 的值.然后从右往左再扫一次,就可以了. // 高大上一点,用了单调队列,只是队列

浅谈—单调栈

一.概念: 单调栈的本质还是一个栈,只不过是栈的元素从栈底到栈顶单调递增或者是单调递减. 二.单调栈的维护: 每加入一个元素,将这个元素和栈顶元素相比较,(假设你维护的是一个单调递增的栈),如果当前元素大于等于栈顶元素,说明这个元素 没有破坏这个栈的单调性,直接加入:如果当前元素小于栈顶元素,直接向栈中加入元素会破坏栈的单调性,这时我们需要将栈顶元素依次弹出,知道遇到第一个 大于或者等于当前元素的元素,再将该元素压入栈中.时间复杂度O(n),所有的元素都会进栈一次. 三.单调栈的性质: (1)单

CodeForces 548D 单调栈

Mike and Feet Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Practice CodeForces 548D Appoint description: Description Mike is the president of country What-The-Fatherland. There are n bears living in this

hdu 1506 单调栈问题

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1506 题目的意思其实就是要找到一个尽可能大的矩形来完全覆盖这个矩形下的所有柱子,只能覆盖柱子,不能留空. 我们求得的面积其实就是Max{s=(right[i] - left[i] + 1)*height[i];(i>=1&&i<=n)} 每一个柱子都要尽可能向左向右延伸,使得获得最大的面积. 此时我就要用到单调栈 单调栈就是栈内元素单调递增或者单调递减的栈,单调栈只能在栈顶操作.

LeetCode 84 Largest Rectangle in Histogram (单调栈)

Given n non-negative integers representing the histogram's bar height where the width of each bar is 1, find the area of largest rectangle in the histogram. Above is a histogram where width of each bar is 1, given height = [2,1,5,6,2,3]. The largest