题目链接: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)的更多相关文章

  1. codeforces 374D. Inna and Sequence 线段树

    题目链接 给m个数, n个操作, 一个数列, 初始为空.一共有3种操作, 在数列末尾加0, 加1, 或删除位置为a[i]的数, a[i]为初始给的m个数, 如果a[i]大于数列长度, 那么什么也不发生 ...

  2. 2016暑假多校联合---Rikka with Sequence (线段树)

    2016暑假多校联合---Rikka with Sequence (线段树) Problem Description As we know, Rikka is poor at math. Yuta i ...

  3. codeforces Good bye 2016 E 线段树维护dp区间合并

    codeforces Good bye 2016 E 线段树维护dp区间合并 题目大意:给你一个字符串,范围为‘0’~'9',定义一个ugly的串,即串中的子串不能有2016,但是一定要有2017,问 ...

  4. Hdu 3564 Another LIS 线段树+LIS

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission( ...

  5. 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 ...

  6. hdu_3564_Another LIS(线段树+LIS)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=3564 题意:给你N个数的位置.数i的位置为第i个数,比如 0 0 2,表示1插在第0个位置,此时数列为 ...

  7. luogu5010 HMR的LIS III (dp+线段树)

    这个东西和最长上升子序列很像 考虑如果已经知道每个位置为开头的LIS长度和个数 f[i],我可以扫一遍 判断这个个数和K的大小,找到第一个长度=len而且个数<K的,这个位置就是要选的 然后K- ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. PAT Basic 1042

    1042 字符统计 请编写程序,找出一段给定文字中出现最频繁的那个英文字母. 输入格式: 输入在一行中给出一个长度不超过 1000 的字符串.字符串由 ASCII 码表中任意可见字符及空格组成,至少包 ...

  2. nodejs 如何发送一个带JSON的GET请求?

    GET /megacorp/employee/_search { "aggs" : { "all_interests" : { "terms" ...

  3. 10大vim插件

    Taglist taglist是一个用于显示定位程序中各种符号的插件,例如宏定义.变量名.结构名.函数名这些东西 我们将其称之为符号(symbols),而在taglist中将其称之为tag.显然,要想 ...

  4. ASP.NET(三):整体总结

    导读:经过一段时间的学习,我的ASP.NET也算是结束了.在这个过程中,总结了它的六大对象,现在先做个总体的总结,然后还会总结一下真假分页的情况.只有总结才能收获.ASP.net严格说起来,其实在VB ...

  5. 【Luogu】P1330封锁阳光大学(bfs染色)

    题目链接 这题恶心死我了. bfs染色,统计每个联通块两色的个数,ans加它们的最小值. #include<cstdio> #include<cctype> #include& ...

  6. BZOJ 3669 [Noi2014]魔法森林 ——SPFA / Link-Cut Tree

    [题目分析] 大意就是有一张图,边权有两个值,ai和bi 找到一条路径,使得路径上的max(ai)+max(bi)最小. 遇到有两个权值或者多个权值的时候,如果他们互相影响,试着用分块搞一搞. 如果互 ...

  7. BZOJ2337 [HNOI2011]XOR和路径 【概率dp + 高斯消元】

    题目 题解 突然get到这样路径期望的题目八成是高斯消元 因为路径上的dp往往具有后效性,这就形成了一个方程组 对于本题来说,直接对权值dp很难找到突破口 但是由于异或是位独立的,我们考虑求出每一位的 ...

  8. 【gets getline的用法 char[]转化为str】poj 2418

    http://poj.org/problem?id=2418 [注意] 1. 输入有空格,用 char str[maxn]; while(gets(str)){ str[]!='\0'; } 或 st ...

  9. iOS 收款计算器算法

    一个收款计算器算法,从之前高仿有赞Demo里面抽离的一个界面 demo 在这里 https://github.com/L-vinCent/calculView_function 显示计算记录 不能连续 ...

  10. Ubuntu Jdk卸载 Oracle Jdk安装

    完全卸载 移除所有 Java相关包 (Sun, Oracle, OpenJDK, IcedTea plugins, GIJ): apt-get update apt-cache search java ...