枚举$a_{i}$并判断是否可行,有以下结论:若$a_{i}$可以留下来,一定存在一种合法方案使得$a_{i}$仅参与最后若干次合并,且第一次参与合并前左右都不超过2个数
证明:将大于$a_{i}$的看成1,小于$a_{i}$的看成0,将合并分为两类:
1.都在左/右区间,那么相当于删除了最右/左边的$01/10$,若存在3个数,可以将倒数第3个数与$01/10$合并去删除
2.左右区间各有1个,当达到3个后,不管是$000$和$111$还是$010$和$101$都可以预先合并再与$a_{i}$合并
根据上述结论,我们可以对两边分别求出最终结果能否为$0,00,1,11,01/10$,并判断能否组合即可
(可以保留$a_{i}$的组合方法有8种:$0-1$,$1-0$,$00-11$,$11-00$,$01/10-01/10$)
 对这些结果分为两类进行判断:
1.全部相同(即$0/00/1/11$):以0为例,不断插入一个数字,要合并当且仅当以$111$或$10$为结尾(特别的,如果$10$是最后两个数,那么就无解,也可以删除掉),证明略(贪心策略中$10$消除是为了更好地消除$111$)
具体维护方法:根据贪心,可以发现插入并合并完后一定是若干个$0$和不超过2个$1$,对0和1的个数进行维护,最后只需要判断0的个数是否比1的多即可
2.$01/10$,首先长度必须要为偶数,之后考虑如果$0$和$1$个数相同,那么一定存在相邻的$01/10$且必然有别的数(否则就已经完成了),不断消除$01/10$即可
假设$1$的个数比$0$的个数多,按照第一种方案的贪心,有解必然存在一次合并后满足$0$的个数和$1$的个数一样多(包括未插入的部分),即最终状态下$0$的个数不小于比$1$的个数,证明略
但这样的复杂度为$o(n^{2})$,需要优化:
从小到大枚举$a_{i}$,那么每一次相当于将一个位置上的$1$变成$0$,考虑要维护哪些信息:1.两种贪心时的最终状态;2.$0$的个数和$1$的个数

  1. 1 #include<bits/stdc++.h>
  2. 2 using namespace std;
  3. 3 #define N 1000005
  4. 4 #define L (k<<1)
  5. 5 #define R (L+1)
  6. 6 #define mid (l+r>>1)
  7. 7 struct sta{
  8. 8 int x,y;
  9. 9 };
  10. 10 struct ji{
  11. 11 int k;
  12. 12 sta a[2];
  13. 13 }o,o0,o1;
  14. 14 struct type{
  15. 15 ji p0,p1;
  16. 16 }f[N<<2];
  17. 17 int t,n,a[N],id[N],ans[N];
  18. 18 sta merge(sta x,sta y){
  19. 19 sta z;
  20. 20 z.x=x.x+max(y.x-x.y,0);
  21. 21 z.y=(y.y+max(x.y-y.x,0)-1)%2+1;
  22. 22 return z;
  23. 23 }
  24. 24 type merge(type x,type y){
  25. 25 type z;
  26. 26 z.p0.k=x.p0.k+y.p0.k;
  27. 27 z.p1.k=x.p1.k+y.p1.k;
  28. 28 z.p0.a[0]=merge(x.p0.a[0],y.p0.a[0]);
  29. 29 z.p0.a[1]=merge(y.p0.a[1],x.p0.a[1]);
  30. 30 z.p1.a[0]=merge(x.p1.a[0],y.p1.a[0]);
  31. 31 z.p1.a[1]=merge(y.p1.a[1],x.p1.a[1]);
  32. 32 return z;
  33. 33 }
  34. 34
  35. 35 void build(int k,int l,int r){
  36. 36 if (l==r){
  37. 37 f[k]=type{o0,o1};
  38. 38 return;
  39. 39 }
  40. 40 build(L,l,mid);
  41. 41 build(R,mid+1,r);
  42. 42 f[k]=merge(f[L],f[R]);
  43. 43 }
  44. 44 void update(int k,int l,int r,int x){
  45. 45 if (l==r){
  46. 46 f[k]=type{o1,o0};
  47. 47 return;
  48. 48 }
  49. 49 if (x<=mid)update(L,l,mid,x);
  50. 50 else update(R,mid+1,r,x);
  51. 51 f[k]=merge(f[L],f[R]);
  52. 52 }
  53. 53 type query(int k,int l,int r,int x,int y){
  54. 54 if ((l>y)||(x>r))return type{o,o};
  55. 55 if ((x<=l)&&(r<=y))return f[k];
  56. 56 return merge(query(L,l,mid,x,y),query(R,mid+1,r,x,y));
  57. 57 }
  58. 58 bool pd(type k,int p,int x){
  59. 59 if (!x)return k.p0.a[p].x>k.p0.a[p].y;
  60. 60 if (x==1)return k.p1.a[p].x>k.p1.a[p].y;
  61. 61 if (x==2){
  62. 62 if (k.p0.k==k.p1.k)return 1;
  63. 63 if (k.p0.k<k.p1.k)return k.p0.a[p].x>=k.p0.a[p].y;
  64. 64 return k.p1.a[p].x>=k.p1.a[p].y;
  65. 65 }
  66. 66 }
  67. 67 int main(){
  68. 68 o0.a[0].y=o0.a[1].y=1;
  69. 69 o1.k=o1.a[0].x=o1.a[1].x=1;
  70. 70 scanf("%d",&t);
  71. 71 while (t--){
  72. 72 scanf("%d",&n);
  73. 73 for(int i=1;i<=n;i++){
  74. 74 scanf("%d",&a[i]);
  75. 75 ans[i]=0;
  76. 76 id[a[i]]=i;
  77. 77 }
  78. 78 build(1,1,n);
  79. 79 for(int i=1;i<=n;i++){
  80. 80 update(1,1,n,id[i]);
  81. 81 type x=query(1,1,n,1,id[i]-1),y=query(1,1,n,id[i]+1,n);
  82. 82 if ((pd(x,0,0))&&(pd(y,1,1))||(pd(x,0,1))&&(pd(y,1,0))||(pd(x,0,2))&&(pd(y,1,2)))ans[id[i]]=1;
  83. 83 }
  84. 84 for(int i=1;i<=n;i++)printf("%d",ans[i]);
  85. 85 printf("\n");
  86. 86 }
  87. 87 }

[nowcoder5669E]Eliminate++的更多相关文章

  1. Effective Java 06 Eliminate obsolete object references

    NOTE Nulling out object references should be the exception rather than the norm. Another common sour ...

  2. Effective Java 24 Eliminate unchecked warnings

    Note Eliminate every unchecked warning that you can. Set<Lark> exaltation = new HashSet(); The ...

  3. hdu 4115 Eliminate the Conflict ( 2-sat )

    Eliminate the Conflict Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/O ...

  4. HDU 4041 Eliminate Witches! (模拟题 ACM ICPC 2011亚洲北京赛区网络赛)

    HDU 4041 Eliminate Witches! (模拟题 ACM ICPC 2011 亚洲北京赛区网络赛题目) Eliminate Witches! Time Limit: 2000/1000 ...

  5. hdu4115 Eliminate the Conflict

    Eliminate the Conflict Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...

  6. Once you eliminate all the other factors,the only thing remaining must be the truth.

    Once you eliminate all the other factors,the only thing remaining must be the truth. 一旦你排除了杂因,剩下的一定是 ...

  7. HDU 4115 Eliminate the Conflict(2-SAT)(2011 Asia ChengDu Regional Contest)

    Problem Description Conflicts are everywhere in the world, from the young to the elderly, from famil ...

  8. 把配置和环境解耦 eliminate “works on my machine” problems when collaborating on code with co-workers docker架构与解决的问题

    Docker实践 - 懒人的技术笔记 - 博客频道 - CSDN.NET  http://blog.csdn.net/lincyang/article/details/43055061 Docker直 ...

  9. HDU 4115 Eliminate the Conflict(2-sat)

    HDU 4115 Eliminate the Conflict pid=4115">题目链接 题意:Alice和Bob这对狗男女在玩剪刀石头布.已知Bob每轮要出什么,然后Bob给Al ...

随机推荐

  1. vue 中级基础考察面试题

    vue 生命周期有哪些 beforeCreate created beforeMount mounted beforeUpdate updated activated deactivated befo ...

  2. 从0到1使用Kubernetes系列(二):安装工具介绍

    该系列第一篇为:<从0到1使用Kubernetes系列--Kubernetes入门>.本文是Kubernetes系列的第二篇,将介绍使用Kubeadm+Ansible搭建Kubernete ...

  3. v72.01 鸿蒙内核源码分析(Shell解析) | 应用窥伺内核的窗口 | 百篇博客分析OpenHarmony源码

    子曰:"苟正其身矣,于从政乎何有?不能正其身,如正人何?" <论语>:子路篇 百篇博客系列篇.本篇为: v72.xx 鸿蒙内核源码分析(Shell解析篇) | 应用窥视 ...

  4. Java(23)常用API二

    作者:季沐测试笔记 原文地址:https://www.cnblogs.com/testero/p/15228415.html 博客主页:https://www.cnblogs.com/testero ...

  5. 2.3 Core Building Blocks 核心构件

    Core Building Blocks 核心构件 DDD mostly focuses on the Domain & Application Layers and ignores the ...

  6. JBOSS未授权访问漏洞利用

    1. 环境搭建 https://www.cnblogs.com/chengNo1/p/14297387.html 搭建好vulhub平台后 进入对应漏洞目录 cd vulhub/jboss/CVE-2 ...

  7. C# 如何使用代码添加控件及控件事件

    1.首先简单设计一下界面: 添加了Click事件 <Window x:Class="WpfApp.MainWindow" xmlns="http://schemas ...

  8. [技术博客]WEB实现划词右键操作

    [技术博客]WEB实现划词右键操作 一.功能解释 简单地对题目中描述的功能进行解释:在浏览器中,通过拖动鼠标选中一个词(或一段文字),右键弹出菜单,且菜单为自定义菜单,而非浏览器本身的菜单.类似的功能 ...

  9. Vue接收后端传过来excel表格的文件流并下载

    题外话:当接收文件流时要确定文件流的类型,但也有例外就是application/octet-stream类型,主要是只用来下载的类型,这个类型简单理解意思就是通用类型类似 var .object.ar ...

  10. spring、spring boot中配置多数据源

    在项目开发的过程中,有时我们有这样的需求,需要去调用别的系统中的数据,那么这个时候系统中就存在多个数据源了,那么我们如何来解决程序在运行的过程中到底是使用的那个数据源呢? 假设我们系统中存在2个数据源 ...