题目

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. c# 冒号 :

    1.表示继承关系 class classA:classB 2.继承构造, 调用自己这个类的某个构造函数,因为有的类构造函数很多参数,而有的参数又不是必须填写,或者可以提供一些默认值,就跟重载是一样的道 ...

  2. ClassLibary和WPF User Control LIbary和WPF Custom Control Libary的异同

    说来惭愧,接触WPF这么长时间了,今天在写自定义控件时遇到一个问题:运行界面中并没有显示自定义控件,经调试发现原来没有加载Themes中的Generic.xaml. 可是为什么在其他solution中 ...

  3. 转Postman请求Https接口

    转自:https://blog.csdn.net/ONS_cukuyo/article/details/79172242 单向认证 像平常一样访问就行,无需做任何处理,只需要把http://变成htt ...

  4. Rabbitmq消息队列(一) centos下安装rabbitmq

    1.简介 AMQP,即Advanced Message Queuing Protocol,高级消息队列协议,是应用层协议的一个开放标准,为面向消息的中间件设计.消息中间件主要用于组件之间的解耦,消息的 ...

  5. source sh运行脚本的差别

    主要有两种方式运行shell脚本 1)source test.bsh 2)sh test.bsh 1)souce运行脚本文件会在父程序中运行.各项动作都会在原本的bash内生效.运行过程不另开进程.脚 ...

  6. 使用wifi连接eclipse进行android程序调试

    首先手机必须是root过的.能够使用百度一键root工具. 然后,在手机中打开这个终端(terminal)应用,输入例如以下命令:         su         setprop service ...

  7. JavaScript的toString()

    JavaScript toString() 方法 JavaScript Boolean 对象 定义和用法 toString() 方法可把一个逻辑值转换为字符串,并返回结果. 语法 booleanObj ...

  8. Frogger - poj 2253 (Dijkstra)

      Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 28802   Accepted: 9353 Description Fr ...

  9. 网络流合集:bzoj1433,1934,1854 题解

    转载请注明:http://blog.csdn.net/jiangshibiao/article/details/23992205 网络流/二分图大合集 [NO.1*原题] 1433: [ZJOI200 ...

  10. ios 动画 创建一个UIImageView并将其属性设置animationImages为UIImages 的数组

    NSArray *animationFrames = [NSArray arrayWithObjects: [UIImage imageWithName:@"image1.png" ...