P3506 [POI2010]MOT-Monotonicity 2
题目
P3506 [POI2010]MOT-Monotonicity 2
第一次切掉没题解的题\(qwq\)
做法
首先确定\(a_i\)的位置后显然就能确定\(a_{i+1}\)的位置,建一棵权值线段树,维护\(<,=,>\)三种情况
考虑确定\(a_{i}\)的位置
在\([min,a_{i}-1]\)中找\(<\)的最大值
在\([a_{i}+1,max]\)中找\(>\)的最大值
找\([a_{i},a_{i}]\)的\(=\)的值(其实不用线段树,开个数组就能维护)
比较三个值,假定最大值为\(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的更多相关文章
- [补档][Poi2010]Monotonicity 2
[Poi2010]Monotonicity 2 题目 给出N个正整数a[1..N],再给出K个关系符号(>.<或=)s[1..k]. 选出一个长度为L的子序列(不要求连续),要求这个子序列 ...
- BZOJ2090: [Poi2010]Monotonicity 2【线段树优化DP】
BZOJ2090: [Poi2010]Monotonicity 2[线段树优化DP] Description 给出N个正整数a[1..N],再给出K个关系符号(>.<或=)s[1..k]. ...
- 【BZOJ2090/2089】[Poi2010]Monotonicity 2 动态规划+线段树
[BZOJ2090/2089][Poi2010]Monotonicity Description 给出N个正整数a[1..N],再给出K个关系符号(>.<或=)s[1..k].选出一个长度 ...
- [Poi2010]Monotonicity 2 线段树
这道题考试的时候先打了个dfs暴力.又打了个O(n²)的动规.然后竟然心血来潮拍了一下..明明知道过不去的...然后水了50分(20个测试点这么多啊啊啊啊). 因为它已经提前给你如果长度为i时下一位的 ...
- Poi2010 Monotonicity 2
树状数组优化dp 可以证明最优解一定是通过之前的最优转移过来的,所以每一个点只需要保存以该节点为结尾的最长长度即可 对于不同符号,等于号维护数组,大于小于维护树状数组 #include<cstd ...
- Monotonicity 2[POI2010]
题目描述 给出N个正整数a[1..N],再给出K个关系符号(>.<或=)s[1..k].选出一个长度为L的子序列(不要求连续),要求这个子序列的第i项和第i+1项的的大小关系为s[(i-1 ...
- #14 [BZOJ2090/2089] [Poi2010]Monotonicity 2/Monotonicity
题解: 首先想到了标算..然后证明了一发是错的(事实证明很智障) 先说正确性比较显然的O(n^2)算法 令f[i][j]表示前i个物品,匹配到第j个括号,最大值是多少 g[i][j]表示前i个物品,匹 ...
- BZOJ2090 : [Poi2010]Monotonicity 2
设f[i]表示以i为结尾的最长的合法序列的长度,=号直接维护,<号和>号用两棵树状数组维护即可,时间复杂度$O(n\log n)$. #include<cstdio> #def ...
- bzoj2089&2090: [Poi2010]Monotonicity
双倍经验一眼题... f[i][1/2]表示以i结尾,当前符号应该是</>的最长上升子序列, 用BIT优化转移就好 =的话就不用说了吧= = #include<iostream> ...
随机推荐
- MQTT--linux安装部署(CentOS)
OS环境:CentOS6.5 1.安装依赖 yum -y install gcc gcc-c++ openssl-devel c-ares-devel libuuid-devel wget cmake ...
- js事件之onmousedown和onmouseup
<!DOCTYPE html> <html> <head> <script> function mouseDown() { document.getEl ...
- web开发中的路径问题
http://www.cnblogs.com/tianguook/archive/2012/08/31/2665755.html 转自:http://www.blogjava.net/meil/arc ...
- Java交通灯系统
交通灯管理项目模拟了对十字路口交通灯的控制,一般在我们生活中的十字路口是有人行道的,而此项目没有考虑人行道.具体需求如下: 1.异步随机生成按照各个路线行驶的车辆. 例如: ...
- hdu 1398 Square Coins 分钱币问题
Square Coins Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit ...
- ifconfig 命令
许多windows非常熟悉ipconfig命令行工具,它被用来获取网络接口配置信息并对此进行修改.Linux系统拥有一个类似的工具,也就是ifconfig(interfaces config).通常需 ...
- RS-232针脚(8 pin)
1 CD Carrier Detect 载波检测 2 RXD Receive Data 接收 3 TXD Transmit Data 发送 4 DTR Data Terminal Ready ...
- memcached在Java中的应用以及magent的配置-每天进步一点点
memcached在Java中的应用: http://nhy520.iteye.com/blog/1775893 magent配置memcached分布式集群的应用: http://www.jians ...
- 零基础学python-2.7 列表与元组
事实上,能够把列表和元组看成普通的数组.可是这个数组能够存储不同的数据类型(对象) 列表和元组的差别 列表 元组 使用的符号 [] () 元素数量 可变 不可变 改动元素 不能够 能够 假设大家有 ...
- usermod命令
usermod 功能: 修改用户 常用参数:-c 账号说明-d 账号家目录-e 密码失效日期-g 主用户组GID-G 次用户组GID-l 账号名称-s she ...