E. LIS of Sequence

Time Limit: 20 Sec

Memory Limit: 256 MB

题目连接

http://codeforces.com/contest/486/problem/E

Description

The next "Data Structures and Algorithms" lesson will be about Longest Increasing Subsequence (LIS for short) of a sequence. For better understanding, Nam decided to learn it a few days before the lesson.

Nam created a sequence a consisting of n (1 ≤ n ≤ 105) elements a1, a2, ..., an (1 ≤ ai ≤ 105). A subsequence ai1, ai2, ..., aik where 1 ≤ i1 < i2 < ... < ik ≤ n is called increasing if ai1 < ai2 < ai3 < ... < aik. An increasing subsequence is called longest if it has maximum length among all increasing subsequences.

Nam realizes that a sequence may have several longest increasing subsequences. Hence, he divides all indexes i (1 ≤ i ≤ n), into three groups:

group of all i such that ai belongs to no longest increasing subsequences.
    group of all i such that ai belongs to at least one but not every longest increasing subsequence.
    group of all i such that ai belongs to every longest increasing subsequence.

Since the number of longest increasing subsequences of a may be very large, categorizing process is very difficult. Your task is to help him finish this job.

Input

The first line contains the single integer n (1 ≤ n ≤ 105) denoting the number of elements of sequence a.

The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 105).

Output

Print a string consisting of n characters. i-th character should be '1', '2' or '3' depending on which group among listed above index i belongs to.

Sample Input

4
1 3 2 5

Sample Output

3223

HINT

题意

给你n个数

然后问你这里面的每个数,是否是

1.不属于任何最长上升子序列中

2.属于多个最长上升子序列中

3.唯一属于一个最长上升子序列中

题解:

对于每一个数,维护两个dp

dp1表示1到i的最长上升子序列长度

dp2表示从n到i最长递减子序列长度

然后如果dp1[i]+dp2[i] - 1 == lis ,就说明属于lis里面,如果dp1[i]的值是唯一的,就说明唯一属于一个lis

否则就不属于咯

代码

#include<iostream>
#include<stdio.h>
#include<map>
#include<cstring>
#include<algorithm>
using namespace std;
#define maxn 100005
int b[maxn];
int a[maxn];
void add(int x,int val)
{while(x<=100000){b[x] = max(b[x],val);x += x & (-x);}
}
int get(int x)
{int ans = 0;while(x){ans = max(ans,b[x]);x -= x & (-x);}return ans;
}
int dp1[maxn];
int dp2[maxn];
int ans[maxn];
map<int,int> H;
int main()
{int n;scanf("%d",&n);int LIS = 0;for(int i=1;i<=n;i++){scanf("%d",&a[i]);dp1[i] = 1 + get(a[i]-1);add(a[i],dp1[i]);LIS = max(LIS,dp1[i]);}reverse(a+1,a+1+n);memset(b,0,sizeof(b));for(int i=1;i<=n;i++){a[i] = 100000 - a[i] + 1;dp2[i] = 1 + get(a[i] - 1);add(a[i],dp2[i]);}reverse(dp2+1,dp2+1+n);for(int i=1;i<=n;i++){if(dp1[i]+dp2[i]-1!=LIS)ans[i]=1;else H[dp1[i]]++;}for(int i=1;i<=n;i++){if(ans[i]!=1&&H[dp1[i]]==1){ans[i]=3;}}for(int i=1;i<=n;i++)if(ans[i]==1)cout<<"1";else if(ans[i]==0)cout<<"2";else if(ans[i]==3)cout<<"3";
}
/*
10
2 2 2 17 8 9 10 17 10 5
*/

转载于:https://www.cnblogs.com/qscqesze/p/4908665.html

Codeforces Round #277 (Div. 2) E. LIS of Sequence DP相关推荐

  1. Codeforces Round #277 (Div. 2) 题解

    Codeforces Round #277 (Div. 2) A. Calculating Function time limit per test 1 second memory limit per ...

  2. 贪心+构造 Codeforces Round #277 (Div. 2) C. Palindrome Transformation

    题目传送门 1 /* 2 贪心+构造:因为是对称的,可以全都左一半考虑,过程很简单,但是能想到就很难了 3 */ 4 /**************************************** ...

  3. Codeforces Round #585 (Div. 2) E. Marbles 状压dp + 逆序对

    传送门 文章目录 题意: 思路: 题意: 思路: 考虑数列最终的状态一定是相同颜色在一起,所以我们发现他的颜色是有顺序的!显然可以用状压dpdpdp来枚举颜色的顺序,但是又有问题了,你怎么确定当前这个 ...

  4. Codeforces Round #268 (Div. 1) C. Hack it! 数位dp + 构造数位

    传送门 文章目录 题意: 思路: 题意: 定义f(x)f(x)f(x)表示xxx的十进制下数位和,现在给你aaa,让你选一个区间[l,r][l,r][l,r],满足∑i=lrf(i)moda=0\su ...

  5. Codeforces Round #741 (Div. 2) E. Rescue Niwen! 字符串 + dp

    传送门 文章目录 题意: 思路: 题意: 给你一个串sss,定义其扩张串为s1,s1s2,...,s1s2..sn,s2,s2s3,...,sns_1,s_1s_2,...,s_1s_2..s_n,s ...

  6. Codeforces Round #740 (Div. 2) D2. Up the Strip dp + 分块优化 + 逆向思维

    传送门 文章目录 题意: 思路 题意: 有nnn个细胞,你初始在第nnn细胞上,假设你当前在xxx处,你每次可以进行如下两个操作: (1)(1)(1)选择[1,x−1][1,x-1][1,x−1]内一 ...

  7. Codeforces Round #727 (Div. 2) E. Game with Cards dp + 思维

    传送门 文章目录 题意: 思路: 题意: 初始有左右手,上面各有一个数字为000的卡牌,每次都有一个新卡kik_iki​,你可以将其放在左手或者右手,使两只手上的卡片范围在[ll,i,rl,i][l_ ...

  8. Codeforces Round #462 (Div. 2) C. A Twisty Movement dp + 思维转换

    传送门 文章目录 题意: 思路: 题意: 给你一个长度为nnn的只包含1,21,21,2的序列aaa,你可以至多翻转一段区间,求翻转之后最长非递减子序列是多长. 思路: 考虑如果翻转的话,翻转的子区间 ...

  9. Codeforces Round #630 (Div. 2) F. Independent Set 树形dp

    传送门 文章目录 题意: 思路: 题意: 给你一棵树,求这棵树的边导出子图中独立集的数量和,独立集大小可以为000. 思路: 先考虑普通的独立集数量怎么求,无非就是分情况讨论一下选根还是不选根,而这个 ...

  10. Codeforces Round #277(Div 2) A、B、C、D、E题解

    转载请注明出处: http://www.cnblogs.com/fraud/           --by fraud A. Calculating Function 水题,判个奇偶即可 1 #inc ...

最新文章

  1. Android Handler与Looper原理简析
  2. 深入探讨数据仓库建模与ETL的实践技巧
  3. USB-WiFi在x210板子上的移植
  4. 我的Go+语言初体验——(7)Go+ 分数型有理数数据类型
  5. GCD API 理解 (一)
  6. centos搭建NFS服务器
  7. 全栈性能测试修炼宝典jmeter实战电子版_JMeter实战(一) 体系结构
  8. Lua中local变量和非local变量的区别
  9. 读书笔记 — Java高并发程序设计 — 第二章 — 基础(上)
  10. Java项目启动时,隐藏的 oracle 驱动异常问题
  11. 百度新营销:不只是关键词了
  12. 【Redis】《Redis 开发与运维》笔记-Chapter10-集群
  13. 论文查重软件检测报告怎么看?
  14. arcgis中python计算面积的表达式_Python计算任意多边形面积算法
  15. 【计算机网络】3-20假定1km长的CSMA/CD网络的数据率为1Gbit/s。设信号在网络上的传播速率为200000 km/s。求能够使用此协议的最短帧长。
  16. 辩论技巧与逻辑思维_在IT团队中促进富有成效的辩论的3种基本技能
  17. 计算机efs加密,我的电脑文件efs加密了,现在从做系统打不开了怎么处理啊
  18. windows中的SC命令
  19. ElasticSearch报错:FORBIDDEN/12/index read-only / allow delete (api)无法写入
  20. Python class __int__容易理解

热门文章

  1. C++头文件的若干说明和const对象定义
  2. python modules_python—模块-
  3. Java一套拳法刷掉n个遍历树的问题
  4. 极客大学架构师训练营 系统架构 高并发 高可用 垂直伸缩 水平伸缩 第7课 听课总结
  5. 极客大学架构师训练营 架构师职责 听课总结 -- 第一课
  6. Swift 5 闭包Closure简明教程
  7. 算法:有效九宫格数独Valid Sudoku
  8. 氩焊机器人编程_如何操作焊接机器人的编程?这六点是基础!
  9. java 任务链模式,flink部署运行架构
  10. Django搭建网站笔记