题目

P3506 [POI2010]MOT-Monotonicity 2

第一次切掉没题解的题\(qwq\)

做法

首先确定\(a_i\)的位置后显然就能确定\(a_{i+1}\)的位置,建一棵权值线段树,维护\(<,=,>\)三种情况

考虑确定\(a_{i}\)的位置

  1. 在\([min,a_{i}-1]\)中找\(<\)的最大值

  2. 在\([a_{i}+1,max]\)中找\(>\)的最大值

  3. 找\([a_{i},a_{i}]\)的\(=\)的值(其实不用线段树,开个数组就能维护)

  4. 比较三个值,假定最大值为\(val\),则更新\([a_{i},a_{i}]\)中\(s[(val-1)\%k+1]\)的值(想一想为什么只更新一个符号的值就能保证正确性?)

这题的难点解决了,至于输出方案,如果你做多了这样的题自然能想到开个数组存每次的状态,然后再开个数组存前驱

代码写得比较乱,重载这些大家自己加吧

#include<cstdio>
#include<cstring>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
typedef int LL;
const LL maxn=1500009;
inline LL Read(){
LL x(0),f(1); char c=getchar();
while(c<'0'||c>'9'){
if(c=='-') f=-1; c=getchar();
}
while(c>='0'&&c<='9')
x=(x<<3)+(x<<1)+c-'0',c=getchar();
return x*f;
}
struct node{
LL mx,mxi,tot;
};
struct Tree{
node t[4];
LL son[2];
}tree[maxn];
LL nod;
node Query(LL now,LL l,LL r,LL lt,LL rt,LL opt){
if(!now||lt>rt)
return (node){0,0,0};
if(lt<=l&&rt>=r)
return tree[now].t[opt];
node ans=(node){0,0,0};
LL mid=(l+r)>>1;
if(lt<=mid)
ans=Query(tree[now].son[0],l,mid,lt,rt,opt);
if(rt>mid){
node tmp=Query(tree[now].son[1],mid+1,r,lt,rt,opt);
if(ans.mx<tmp.mx)
ans=tmp;
}
return ans;
}
inline void Update(LL now,LL opt){
LL son0(tree[now].son[0]),son1(tree[now].son[1]);
if(tree[son0].t[opt].mx>tree[son1].t[opt].mx)
tree[now].t[opt]=tree[son0].t[opt];
else
tree[now].t[opt]=tree[son1].t[opt];
}
inline void Change(LL &now,LL l,LL r,LL opt,LL goal,LL val,LL tot){
if(!now){
now=++nod;
if(l==r)
for(LL i=1;i<=3;++i)
tree[now].t[i]=(node){0,l,0};
}
if(l==r){
if(tree[now].t[opt].mx<val)
tree[now].t[opt].mx=val,
tree[now].t[opt].tot=tot;
return;
}
LL mid=(l+r)>>1;
if(goal<=mid)
Change(tree[now].son[0],l,mid,opt,goal,val,tot);
else
Change(tree[now].son[1],mid+1,r,opt,goal,val,tot);
Update(now,opt);
}
LL n,k,root,ans,last,_min,_max;
LL pre[maxn],a[maxn],c[maxn],ch[maxn],w[maxn];
void Write(LL now){
if(!now)
return;
Write(pre[now]);
printf("%d ",w[now]);
}
struct LS{
LL id,val;
}b[maxn];
inline bool cmp1(LS x,LS y){
return x.val<y.val;
}
int main(){
n=Read(),k=Read();
for(LL i=1;i<=n;++i)
b[i]=(LS){i,Read()},
c[i]=b[i].val;
sort(b+1,b+1+n,cmp1);
LL num(0);
for(LL i=1,last=-1;i<=n;++i){
if(last!=b[i].val)
last=b[i].val,
++num;
a[b[i].id]=num;
}
_min=1,_max=num; for(LL i=1;i<=k;++i){
char c;
scanf(" %c",&c);
if(c=='<')
ch[i]=1;
else if(c=='>')
ch[i]=3;
else
ch[i]=2;
}
LL tot(0),last;
for(LL i=1;i<=n;++i){
node ans1=Query(root,_min,_max, _min,a[i]-1,1),
ans2=Query(root,_min,_max, a[i],a[i] ,2),
ans3=Query(root,_min,_max, a[i]+1,_max,3);
++ans1.mx,++ans2.mx,++ans3.mx; if(ans1.mx<ans2.mx) ans1=ans2;
if(ans1.mx<ans3.mx) ans1=ans3; LL sum(ans1.mx);
w[++tot]=c[i],
pre[tot]=ans1.tot,
Change(root,_min,_max,ch[(sum-1)%k+1],a[i],sum,tot);
if(sum>ans)
ans=sum,
last=tot;
}
printf("%d\n",ans);
Write(last);
return 0;
}/*
20 5
2 4 3 1 3 5 3 8 9 2 1 20 3 5 9 1 2 4 5 3
< > = < > 11
2 4 3 3 5 3 9 1 1 4 3
*/

P3506 [POI2010]MOT-Monotonicity 2的更多相关文章

  1. [补档][Poi2010]Monotonicity 2

    [Poi2010]Monotonicity 2 题目 给出N个正整数a[1..N],再给出K个关系符号(>.<或=)s[1..k]. 选出一个长度为L的子序列(不要求连续),要求这个子序列 ...

  2. BZOJ2090: [Poi2010]Monotonicity 2【线段树优化DP】

    BZOJ2090: [Poi2010]Monotonicity 2[线段树优化DP] Description 给出N个正整数a[1..N],再给出K个关系符号(>.<或=)s[1..k]. ...

  3. 【BZOJ2090/2089】[Poi2010]Monotonicity 2 动态规划+线段树

    [BZOJ2090/2089][Poi2010]Monotonicity Description 给出N个正整数a[1..N],再给出K个关系符号(>.<或=)s[1..k].选出一个长度 ...

  4. [Poi2010]Monotonicity 2 线段树

    这道题考试的时候先打了个dfs暴力.又打了个O(n²)的动规.然后竟然心血来潮拍了一下..明明知道过不去的...然后水了50分(20个测试点这么多啊啊啊啊). 因为它已经提前给你如果长度为i时下一位的 ...

  5. Poi2010 Monotonicity 2

    树状数组优化dp 可以证明最优解一定是通过之前的最优转移过来的,所以每一个点只需要保存以该节点为结尾的最长长度即可 对于不同符号,等于号维护数组,大于小于维护树状数组 #include<cstd ...

  6. Monotonicity 2[POI2010]

    题目描述 给出N个正整数a[1..N],再给出K个关系符号(>.<或=)s[1..k].选出一个长度为L的子序列(不要求连续),要求这个子序列的第i项和第i+1项的的大小关系为s[(i-1 ...

  7. #14 [BZOJ2090/2089] [Poi2010]Monotonicity 2/Monotonicity

    题解: 首先想到了标算..然后证明了一发是错的(事实证明很智障) 先说正确性比较显然的O(n^2)算法 令f[i][j]表示前i个物品,匹配到第j个括号,最大值是多少 g[i][j]表示前i个物品,匹 ...

  8. BZOJ2090 : [Poi2010]Monotonicity 2

    设f[i]表示以i为结尾的最长的合法序列的长度,=号直接维护,<号和>号用两棵树状数组维护即可,时间复杂度$O(n\log n)$. #include<cstdio> #def ...

  9. bzoj2089&2090: [Poi2010]Monotonicity

    双倍经验一眼题... f[i][1/2]表示以i结尾,当前符号应该是</>的最长上升子序列, 用BIT优化转移就好 =的话就不用说了吧= = #include<iostream> ...

随机推荐

  1. MYSQL数据插入、更新及删除

    上文讲到创建数据表,本文介绍create table后的数据插入: 一.通过insert into ...values...插入 insert into tablename (column1,colu ...

  2. oc 跳转控制方法

    1.presentViewController - (void)presentViewController:(UIViewController *)viewControllerToPresent an ...

  3. WordPress函数:get_bloginfo()用法详解

    描述 返回你博客的信息,这些信息可以用在任何地方的 PHP 代码中.这个函数,和 bloginfo() 一样,可以用来在模板文件的任何地方显示你博客的信息. 用法 <?php $bloginfo ...

  4. eclipse / ADT(Android Develop Tool) 一些方便的初始设置

      1.设置编辑窗口的背景色eclipse的主编辑窗口的背景色,默认为白色,个人感觉太亮,推荐保护视力的“墨绿色”,当然也可以根据个人喜好更改,如下图 2.主编辑窗口的字体字号等,也可以根据自己的爱好 ...

  5. spring的xml配置文件出现故障

    今天在断网的情况下,spring的applicationContext.xml文件开头部分出现红叉 <span style="font-size:18px;">< ...

  6. 近期建了一个.net源代码共享群,群共享有大量网友分享的.net(C#)商业源代码

    本群创建于2013/6/21: 群里都是.net(C#)程序开发者,群共享有大量网友分享的.net(C#)商业源代码.比方:DTCMS旗舰版,hishop微分销,shopnum微分销.多用户微信公众平 ...

  7. Google Code Jam 2014 资格赛:Problem C. Minesweeper Master

    Problem Minesweeper is a computer game that became popular in the 1980s, and is still included in so ...

  8. 玩转JPA(一)---异常:Repeated column in mapping for entity/should be mapped with insert=&quot;false&quot; update=&quot;fal

    近期用JPA遇到这样一个问题:Repeated column in mapping for entity: com.ketayao.security.entity.main.User column: ...

  9. 配置Nginx与tomcat负责均衡集群,

    今天主要说说,nginx如何配置tomcat集群,首先我们先介绍一下各个软件: 一: 1.Nginx介绍: 下载地址:http://nginx.org/en/download.html nginx这个 ...

  10. 解决from lxml import etree 导入的时候,显示etree不存在

    问题: 当安装完lxml之后,发现使用 from lxml import etree  时,etree不可用 原因 :是lxml中没有etree包 解决: 去官网下载对应包:官网地址:http://l ...