Codeforces 486E LIS of Sequence 题解

题目大意:

  一个序列,问其中每一个元素是否为所有最长上升子序列中的元素或是几个但不是所有最长上升子序列中的元素或一个最长上升子序列都不是。

思路:

  求以每一个元素为开头和结尾的最长上升子序列长度,若两者相加比最长上升子序列长度+1小,则一个也不是;否则若有另一元素与它的两个值完全相同,则不是所有;否则在所有。

代码:

 1 #include<map>
 2 #include<cstdio>
 3 #include<algorithm>
 4 using namespace std;
 5 const int M=100009;
 6 map <pair<int,int>,int> mp;
 7 int i,n,top,a[M],st[M],f1[M],f2[M];
 8 bool flag[M];
 9
10 void LIS(int *f)
11 {
12     st[top=1]=a[1]; f[1]=1;
13     for (int i=2;i<=n;++i)
14         if (st[top]<a[i]) st[++top]=a[i],f[i]=top;
15         else
16         {
17             int l=1,r=top,ans=0;
18             while (l<=r)
19             {
20                 int mid=l+r>>1;
21                 if (st[mid]<a[i]) ans=mid,l=mid+1; else r=mid-1;
22             }
23             st[ans+1]=a[i];
24             f[i]=ans+1;
25         }
26 }
27
28 int main()
29 {
30     scanf("%d",&n);
31     for (i=1;i<=n;++i) scanf("%d",&a[i]);
32     LIS(f1);
33     for (i=1;i<=n;++i) a[i]=M-a[i];
34     for (i=1;i+i<=n;++i) swap(a[i],a[n+1-i]);
35     LIS(f2);
36     for (i=1;i+i<=n;++i) swap(f2[i],f2[n+1-i]);
37     ++top;
38     for (i=1;i<=n;++i)
39         if (f1[i]+f2[i]==top) ++mp[make_pair(f1[i],f2[i])];
40         else flag[i]=1;
41     for (i=1;i<=n;++i)
42         if (!flag[i])
43             if (mp[make_pair(f1[i],f2[i])]==1) printf("3");
44             else printf("2");
45         else printf("1");
46     return 0;
47 }
时间: 2024-08-05 02:57:02

Codeforces 486E LIS of Sequence 题解的相关文章

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>

CodeForces 486E LIS of Sequence

题意: n(10^5)个数字的序列a  求每个位置i  它是不出现在任何LIS中  还是  出现在一些LIS中  还是  出现在所有LIS中 思路: 比赛时候唯一没做出的题-  赛后还是不会做- - -b  看了别人的代码觉得好精妙!! 首先以O(nlogn)复杂度求出LIS 然后我们倒序扫描序列a  对于位置i  如果lis[i]=LIS或者a[i]<big[lis[i]+1] (big[x]表示lis=x的a的最大值  这里的意思是  如果a[i]是某个LIS的最后一个  或者  能与某个L

Codeforces 486E LIS of Sequence --树状数组

题意: 一个序列可能有多个最长子序列,现在问每个元素是以下三个种类的哪一类: 1.不属于任何一个最长子序列 2.属于其中某些但不是全部最长子序列 3.属于全部最长子序列 解法: 我们先求出dp1[i]表示1~i 的最长递增子序列长度, dp2[i]表示 n~i 的最长递减子序列长度(严格增减),这里我们可以用维护最大值的树状数组来解决,开始还以为要用nlogn求LIS的那种算法,当然那样应该也可以,这里元素值是1~10^5的,可以直接用树状数组,如果元素值任意的话,我们离散化一下也可以用树状数组

【CF486E】LIS of Sequence题解

[CF486E]LIS of Sequence题解 题目链接 题意: 给你一个长度为n的序列a1,a2,...,an,你需要把这n个元素分成三类:1,2,3: 1:所有的最长上升子序列都不包含这个元素 2:有但非所有的最长上升子序列包含这个元素 3:所有的最长上升子序列都包含这个元素 输入格式: 第一行包含一个正整数n,表示序列的长度.第二行包含n个正整数a1,a2,...,an,表示序列中的元素. 输出格式: 一行,包含一个长度为n的.由1,2,3三种数字组成的字符串,第i个数字表示ai所属类

486E - LIS of Sequence(LIS)

题意:给一个长度为n的序列,问每个数关于序列的LIS(longest increasing sequence)是什么角色.这里分了三种: 1.此数没有出现在任意一条LIS中 2.此数出现在至少一条但是不是全部的LIS中 3.此数出现在所有的LIS中 解法:nlgn的LIS算法可以求出以每个i位置结束的LIS长度up[i].出现在LIS的数其实就是一个dag,找出那些某层唯一数值的数就行.LIS算法后,从后向前扫,维护所以长度的最大值,这中间可以判断某长度有几个值,如果某些长度有多个位置则他们都属

Codeforces Round #277 E. LIS of Sequence(486E) 树状数组乱搞

http://codeforces.com/contest/486/problem/E E. LIS of Sequence time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output The next "Data Structures and Algorithms" lesson will be about Longest I

Codeforces Round #FF (Div. 2) 题解

比赛链接:http://codeforces.com/contest/447 A. DZY Loves Hash time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output DZY has a hash table with p buckets, numbered from 0 to p?-?1. He wants to insert n 

Codeforces Round #259 (Div. 2) 题解

A. Little Pony and Crystal Mine time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Twilight Sparkle once got a crystal from the Crystal Mine. A crystal of size n (n is odd; n?>?1) is an n?×?n 

codeforces Arrival of the General 题解

A Ministry for Defense sent a general to inspect the Super Secret Military Squad under the command of the Colonel SuperDuper. Having learned the news, the colonel ordered to all n squad soldiers to line up on the parade ground. By the military charte