Codeforces 486E LIS of Sequence(线段树+LIS)
题目链接:Codeforces 486E LIS of Sequence
题目大意:给定一个数组。如今要确定每一个位置上的数属于哪一种类型。
解题思路:先求出每一个位置选的情况下的最长LIS,由于開始的想法,所以求LIS直接用线段树写了,没有改,能够用
log(n)的算法直接求也是能够的。然后在从后向前做一次类似LIS。每次推断A[i]是否小于f[dp[i]+1],这样就能够确定该位
置是否属于LIS序列。
然后为第三类的则说明dp[i] = k的仅仅有一个满足。
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef pair<int, int> pii;
const int maxn = 1e5;
int N, A[maxn + 5], dp[maxn + 5], ct[maxn + 5], f[maxn + 5], v[maxn + 5];
#define lson(x) ((x)<<1)
#define rson(x) (((x)<<1)|1)
int lc[maxn << 2], rc[maxn << 2];
pii s[maxn << 2];
pii merge(pii a, pii b) {
if (a.first == b.first)
return make_pair(a.first, a.second + b.second);
else
return a.first > b.first ? a : b;
}
void build (int u, int l, int r) {
lc[u] = l;
rc[u] = r;
if (l == r) {
s[u] = make_pair(0, 1);
return;
}
int mid = (l + r) >> 1;
build(lson(u), l, mid);
build(rson(u), mid + 1, r);
s[u] = merge(s[lson(u)], s[rson(u)]);
}
pii query(int u, int l, int r) {
if (l <= lc[u] && rc[u] <= r)
return s[u];
int mid = (lc[u] + rc[u]) >> 1;
pii ret = make_pair(0, 1);;
if (l <= mid)
ret = merge(ret, query(lson(u), l, r));
if (r > mid)
ret = merge(ret, query(rson(u), l, r));
return ret;
}
void modify(int u, int x, pii d) {
if (lc[u] == x && rc[u] == x) {
s[u] = merge(s[u], d);
return ;
}
int mid = (lc[u] + rc[u]) >> 1;
if (x <= mid)
modify(lson(u), x, d);
else
modify(rson(u), x, d);
s[u] = merge(s[lson(u)], s[rson(u)]);
}
int main () {
scanf("%d", &N);
for (int i = 1; i <= N; i++)
scanf("%d", &A[i]);
build(1, 0, maxn);
for (int i = 1; i <= N; i++) {
pii u = query(1, 0, A[i]-1);
dp[i] = u.first + 1;
modify(1, A[i], make_pair(dp[i], 1));
}
int ans = query(1, 1, maxn).first;
f[ans + 1] = maxn + 1;
for (int i = N; i; i--) {
if (A[i] < f[dp[i]+1]) {
v[i] = 1;
ct[dp[i]]++;
f[dp[i]] = max(f[dp[i]], A[i]);
}
}
for (int i = 1; i <= N; i++) {
if (v[i])
printf("%c", '2' + (ct[dp[i]] == 1 ? 1 : 0));
else
printf("1");
}
printf("\n");
return 0;
}
Codeforces 486E LIS of Sequence(线段树+LIS)的更多相关文章
- codeforces 374D. Inna and Sequence 线段树
题目链接 给m个数, n个操作, 一个数列, 初始为空.一共有3种操作, 在数列末尾加0, 加1, 或删除位置为a[i]的数, a[i]为初始给的m个数, 如果a[i]大于数列长度, 那么什么也不发生 ...
- 2016暑假多校联合---Rikka with Sequence (线段树)
2016暑假多校联合---Rikka with Sequence (线段树) Problem Description As we know, Rikka is poor at math. Yuta i ...
- codeforces Good bye 2016 E 线段树维护dp区间合并
codeforces Good bye 2016 E 线段树维护dp区间合并 题目大意:给你一个字符串,范围为‘0’~'9',定义一个ugly的串,即串中的子串不能有2016,但是一定要有2017,问 ...
- Hdu 3564 Another LIS 线段树+LIS
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission( ...
- Codeforces Round #250 (Div. 1) D. The Child and Sequence 线段树 区间取摸
D. The Child and Sequence Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest ...
- hdu_3564_Another LIS(线段树+LIS)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=3564 题意:给你N个数的位置.数i的位置为第i个数,比如 0 0 2,表示1插在第0个位置,此时数列为 ...
- luogu5010 HMR的LIS III (dp+线段树)
这个东西和最长上升子序列很像 考虑如果已经知道每个位置为开头的LIS长度和个数 f[i],我可以扫一遍 判断这个个数和K的大小,找到第一个长度=len而且个数<K的,这个位置就是要选的 然后K- ...
- Codeforces 438D The Child and Sequence - 线段树
At the children's day, the child came to Picks's house, and messed his house up. Picks was angry at ...
- Codeforces Round #250 (Div. 1) D. The Child and Sequence 线段树 区间求和+点修改+区间取模
D. The Child and Sequence At the children's day, the child came to Picks's house, and messed his h ...
随机推荐
- ACM训练联盟周赛 K. Teemo's reunited
Teemo likes to drink raspberry juice. He even spent some of his spare time tomake the raspberry jui ...
- bootstap 折叠
data-toggle="collapse" 添加到您想要展开或折叠的组件的链接上. href 或 data-target 属性添加到父组件,它的值是子组件的 id
- struts2模版搭建
点击MyEclipse菜单栏File按钮,点击new-->Web Project 输入Project name之后点击Finish 项目创建完成. 然后右键项目,点击MyEclipse--> ...
- Exchange 邮件服务器内存硬盘估算
- The BLOB and TEXT Types
官网参考:https://dev.mysql.com/doc/refman/5.7/en/blob.html 字符串类型对应的存储需求 Data Type Storage Required CHAR( ...
- nginx的详解(三)
6.禁止访问某个文件或目录1)禁止访问以txt或doc结尾的文件location ~* \.(txt|doc)${root /data/www/wwwroot/linuxtone/test;deny ...
- 安卓Toast实现
代码改变世界 Toast实现显示 // 第一个参数:当前的上下文环境.可用getApplicationContext()或this // 第二个参数:要显示的字符串.也可是R.string中字符串ID ...
- 无记录时显示gridview表头,并增加一行显示“没有记录”【绑定SqlDataSource控件时】
原文发布时间为:2008-08-04 -- 来源于本人的百度文章 [由搬家工具导入] using System;using System.Data;using System.Configuration ...
- Codeforces Round #267 (Div. 2) C. George and Job (dp)
wa哭了,,t哭了,,还是看了题解... 8170436 2014-10-11 06:41:51 njczy2010 C - George and Jo ...
- 标准C程序设计七---15
Linux应用 编程深入 语言编程 标准C程序设计七---经典C11程序设计 以下内容为阅读: <标准C程序设计>(第7版) 作者 ...