题目

Vasya has an array of integers of length n.

Vasya performs the following operations on the array: on each step he finds the longest segment of consecutive equal integers (the leftmost, if there are several such segments) and removes it. For example, if Vasya's array is [13, 13, 7, 7, 7, 2, 2, 2], then after one operation it becomes [13, 13, 2, 2, 2].

Compute the number of operations Vasya should make until the array becomes empty, i.e. Vasya removes all elements from it.

分析

链表+优先队列

  1. #include <cstdio>
  2. #include <algorithm>
  3. #include <cstring>
  4. #include <iostream>
  5. #include <queue>
  6.  
  7. using namespace std;
  8. const int maxn=+;
  9. int a[maxn];
  10. int vis[maxn];
  11.  
  12. struct Node{
  13. int len,id,val,l;
  14. bool operator <(const Node &rhs)const{
  15. return len<rhs.len||(len==rhs.len&&l>rhs.l);
  16. }
  17. }node[maxn];
  18.  
  19. int n;
  20. int Next[maxn],Last[maxn];
  21. int main(){
  22. memset(vis,,sizeof(vis));
  23. scanf("%d",&n);
  24. for(int i=;i<=n;i++){
  25. scanf("%d",&a[i]);
  26. }
  27. int cnt=,m=;
  28. for(int i=;i<=n;i++){
  29. cnt++;
  30. if(a[i]!=a[i+]){
  31. node[++m].len=cnt;
  32. node[m].id=m;
  33. node[m].val=a[i];
  34. node[m].l=i;
  35. cnt=;
  36. }
  37. }
  38. priority_queue<Node>q;
  39. //printf("%d",m);
  40.  
  41. for(int i=;i<=m;i++){
  42. Next[i]=i+;
  43. Last[i]=i-;
  44. q.push(node[i]);
  45. }
  46. Next[m]=;Last[]=;
  47. int ans=;
  48. while(!q.empty()){
  49. while(!q.empty()&&vis[q.top().id])q.pop();
  50. if(!q.empty()){
  51. Node x=q.top();q.pop();
  52. //printf("%d :%d %d\n",x.id,Next[x.id],Last[x.id]);
  53. //printf("%d %d %d\n",x.id,x.len,x.val);
  54. ans++;
  55. Next[Last[x.id]]=Next[x.id];
  56. Last[Next[x.id]]=Last[x.id];
  57. if(Last[x.id]&&Next[x.id]&&node[Last[x.id]].val==node[Next[x.id]].val&&!vis[node[Last[x.id]].id]&&!vis[node[Next[x.id]].id]){
  58. vis[node[Last[x.id]].id]=;
  59. vis[node[Next[x.id]].id]=;
  60.  
  61. int l=node[Next[x.id]].l;
  62. node[++m].val=node[Last[x.id]].val;
  63. node[m].len=node[Last[x.id]].len+node[Next[x.id]].len;
  64. node[m].id=m;
  65. node[m].l=l;
  66. Next[Last[Last[x.id]]]=m;
  67. Last[Next[Next[x.id]]]=m;
  68. Next[m]=Next[Next[x.id]];
  69. Last[m]=Last[Last[x.id]];
  70. q.push(node[m]);
  71. }
  72. }
  73. }
  74. printf("%d",ans);
  75.  
  76. return ;
  77. }

codeforce452DIV2——E. Segments Removal的更多相关文章

  1. Codeforces 899E - Segments Removal

    899E - Segments Removal 思路:priority_queue+pair 代码: #include<bits/stdc++.h> using namespace std ...

  2. 【CodeForces】899 E. Segments Removal

    [题目]E. Segments Removal [题意]给定n个数字,每次操作删除最长的连续相同数字(等长删最左),求全部删完的最少次数.n<=2*10^6,1<=ai<=10^9. ...

  3. CodeForces - 899E Segments Removal (优先队列 + 链表)

    给定一个序列,每次从序列中找一个长度最大的元素相同的片段,删除它. 如果长度相同,删除最靠左边的那个片段. 问,需要删几次. 用链表处理删除片段.对于删除之后两边又能连成一个片段的那种情况,用set记 ...

  4. Codeforces Round #452 (Div. 2) 899E E. Segments Removal

    题 OvO http://codeforces.com/contest/899/problem/E Codeforces Round #452 (Div. 2) - e 899E 解 用两个并查集(记 ...

  5. Codeforces Round #451 & Codeforces Round #452

    Rounding Solution Proper Nutrition 枚举 Solution Phone Numbers 模拟 Solution Alarm Clock 贪心,好像不用线段树也可以,事 ...

  6. Background removal with deep learning

    [原文链接] Background removal with deep learning   This post describes our work and research on the gree ...

  7. [LeetCode] Number of Segments in a String 字符串中的分段数量

    Count the number of segments in a string, where a segment is defined to be a contiguous sequence of ...

  8. Greenplum记录(一):主体结构、master、segments节点、interconnect、performance monitor

    结构:Client--master host--interconnect--segment host 每个节点都是单独的PG数据库,要获得最佳的性能需要对每个节点进行独立优化. master上不包含任 ...

  9. Application package 'AndroidManifest.xml' must have a minimum of 2 segments.

    看了源码就是packagename里面必须包含一个. 源码在: ./sdk/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/id ...

随机推荐

  1. zoj-3963 Heap Partition(贪心+二分+树状数组)

    题目链接: Heap Partition Time Limit: 2 Seconds      Memory Limit: 65536 KB      Special Judge A sequence ...

  2. cursor光标类型

    今天早上在网上看到一篇关于光标类型的总结代码,很好,特定拿来: 最终结果: 代码: <!DOCTYPE html> <html lang="zh-cn"> ...

  3. 人生苦短之我用Python篇(基础)

    Python简介 Python,是一种面向对象的解释型计算机程序设计语言,由荷兰人Guido van Rossum于1989年发明,第一个公开发行版发行于1991年. Python是纯粹的自由软件, ...

  4. Linux下shell命令 1

    1   [root@hadoop-namenode-1 iebd] cd /filename/filename  跳转至filename文件夹 2   [root@hadoop-namenode-1 ...

  5. 剑指Offer面试题:12.链表的倒数第K个结点

    一 题目:链表的倒数第K个结点 题目:输入一个链表,输出该链表中倒数第k个结点.为了符合大多数人的习惯,本题从1开始计数,即链表的尾结点是倒数第1个结点.例如一个链表有6个结点,从头结点开始它们的值依 ...

  6. 关于verilog中小数直接赋值

    verilog中小数直接赋值的话小数会近似成1,如0.1,0.6,0.9赋值的话就会变成1,5.1,5.9也都会变成6.并且quartus默认小数是64位.

  7. BZOJ4832: [Lydsy2017年4月月赛]抵制克苏恩

    传送门 题目大意: 攻击k次,每次可攻击随从或英雄. 随从数不大于7个,且1滴血的a个,2滴b个,3滴c个. 攻击一次血-1,如果随从没死可以生成3滴血随从一个 题解: 概率/期望dp f[i][j] ...

  8. VS 2015 开发Android底部导航条----[实例代码,多图]

      1.废话背景介绍  在Build 2016开发者大会上,微软宣布,Xamarin将被整合进所有版本的Visual Studio之中. 这也就是说,Xamarin将免费提供给所有购买了Visual ...

  9. 洛谷 2680 (NOIp2015) 运输计划

    题目:https://www.luogu.org/problemnew/show/P2680 因为是最长的时间最短,所以二分! 离线LCA可以知道路径长度.每次只看超过二分值的路径. 原本的想法是遍历 ...

  10. UNION和UNION ALL关键字

    UNION和UNION ALL关键字都是将两个结果集合并为一个,但这两者从使用和效率上来说都有所不同. 1.对重复结果的处理:UNION在进行表链接后会筛选掉重复的记录,Union All不会去除重复 ...