Codeforces Round #277 (Div. 2) E. LIS of Sequence DP
E. LIS of Sequence
Time Limit: 20 Sec
Memory Limit: 256 MB
题目连接
http://codeforces.com/contest/486/problem/E
Description
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<=)
{
b[x] = max(b[x],val);
x += x & (-x);
}
}
int get(int x)
{
int ans = ;
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 = ;
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
dp1[i] = + get(a[i]-);
add(a[i],dp1[i]);
LIS = max(LIS,dp1[i]);
}
reverse(a+,a++n);
memset(b,,sizeof(b));
for(int i=;i<=n;i++)
{
a[i] = - a[i] + ;
dp2[i] = + get(a[i] - );
add(a[i],dp2[i]);
}
reverse(dp2+,dp2++n);
for(int i=;i<=n;i++)
{
if(dp1[i]+dp2[i]-!=LIS)ans[i]=;
else H[dp1[i]]++;
}
for(int i=;i<=n;i++)
{
if(ans[i]!=&&H[dp1[i]]==)
{
ans[i]=;
}
}
for(int i=;i<=n;i++)
if(ans[i]==)
cout<<"";
else if(ans[i]==)
cout<<"";
else if(ans[i]==)
cout<<"";
}
/*
10
2 2 2 17 8 9 10 17 10 5
*/
Codeforces Round #277 (Div. 2) E. LIS of Sequence DP的更多相关文章
- Codeforces Round #277 (Div. 2) 题解
Codeforces Round #277 (Div. 2) A. Calculating Function time limit per test 1 second memory limit per ...
- 【codeforces】Codeforces Round #277 (Div. 2) 解读
门户:Codeforces Round #277 (Div. 2) 486A. Calculating Function 裸公式= = #include <cstdio> #include ...
- 贪心+构造 Codeforces Round #277 (Div. 2) C. Palindrome Transformation
题目传送门 /* 贪心+构造:因为是对称的,可以全都左一半考虑,过程很简单,但是能想到就很难了 */ /************************************************ ...
- Codeforces Round #367 (Div. 2) C. Hard problem(DP)
Hard problem 题目链接: http://codeforces.com/contest/706/problem/C Description Vasiliy is fond of solvin ...
- 套题 Codeforces Round #277 (Div. 2)
A. Calculating Function 水题,分奇数偶数处理一下就好了 #include<stdio.h> #include<iostream> using names ...
- Codeforces Round #277(Div 2) A、B、C、D、E题解
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud A. Calculating Function 水题,判个奇偶即可 #includ ...
- Codeforces Round #277 (Div. 2)
整理上次写的题目: A: For a positive integer n let's define a function f: f(n) = - 1 + 2 - 3 + .. + ( - 1)nn ...
- Codeforces Round #277 (Div. 2) D. Valid Sets 暴力
D. Valid Sets Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/486/problem ...
- Codeforces Round #277 (Div. 2) B. OR in Matrix 贪心
B. OR in Matrix Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/486/probl ...
随机推荐
- Android裁剪固定大小头像的功能
转载自: http://www.eoeandroid.com/thread-497277-1-1.html 效果很好,特意转载过来记录一下,加深一下印象. 效果就是 :中间的方框不动,可以拖动图片,选 ...
- GreenDao官方文档翻译(上)
笔记摘要: 上一篇博客简单介绍了SQLite和GreenDao的比较,后来说要详细介绍下GreenDao的使用,这里就贴出本人自己根据官网的文档进行翻译的文章,这里将所有的文档分成上下两部分翻译,只为 ...
- Android-给另一个Activity传递HashMap
I have a HashMap which I would pass to another Activity class. I simply use this code: Intent intent ...
- ajax请求总是不成功?浏览器的同源策略和跨域问题详解
场景 码农小明要做一个展示业务数据的大屏给老板看,里面包含了来自自己网站的数据和来自隔壁老王的数据.那么自己网站的数据提供了 http://xiaoming.com/whoami 这样的数据接口隔壁老 ...
- 链表的倒数第K个节点
题目:输入一个链表,输出该链表中倒数第K个节点.为了符合大多数人的习惯,本题从1开始计数,即链表的尾节点是倒数第1个结点. package com.edu; class LinkNode{ //定义一 ...
- codeforces 675C Money Transfers map
上面是官方题解,写的很好,然后就A了,就是找到前缀和相等的最多区间,这样就可以减去更多的1 然后肯定很多人肯定很奇怪为什么从1开始数,其实从2开始也一样,因为是个环,从哪里开始记录前缀和都一样 我们的 ...
- 《Python基础教程(第二版)》学习笔记 -> 第七章 更加抽象
对象的魔力 多态:意味着可以对不同类的对象使用同样的操作: 封装:对外部世界隐藏对象的工作细节: 继承:以普通的类为基础建立专门的类对象 多态① 多态和方法绑定到对象特性上面的函数称为方法(metho ...
- EF6 Database First (DbContext) - Change Schema at runtime
Problem:There are two SQL databases (dev and live) with on Azure which has identical table structure ...
- 下破解安装Python开发工具WingIDE4.1
步骤: 1.将系统时间调整到一个月之前,然后执行安装. 可以使用date命令调整系统时间,如:date -s '2012-08-14 10:00:00' 2.安装成功后,打开程序,按照提示信息,申请一 ...
- Matlab文件操作
1. Matlab文件操作主要有三个步骤:首先打开文件,然后对文件进行读写操作,最后要关闭文件. 2. fid=fopen(文件名,打开方式) 'r' 只读,文件必须存在(缺省的打开方式) 'w' ...