给定一个序列,每次从序列中找一个长度最大的元素相同的片段,删除它。

如果长度相同,删除最靠左边的那个片段。

问,需要删几次。

用链表处理删除片段。对于删除之后两边又能连成一个片段的那种情况,用set记一下合并就好了。

就是如果这个片段被合并成另一片段了,那么优先队列取到这个的时候就直接pop掉。

本来用自定义的结构体实现的。看了大佬的博客,学会了pair的妙用。

pair <a, b>若被排序, a是第一关键字,b是第二关键字。

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define maxn 2000000 + 1000
  4. typedef pair<int, int> Node;
  5. int a[maxn], l[maxn], r[maxn];
  6. int sum[maxn], num[maxn];
  7. set<Node> flag;
  8. priority_queue<Node> q;
  9.  
  10. int main()
  11. {
  12. int n, tot = ;
  13. scanf("%d", &n);
  14. for (int i = ; i <= n; i++) scanf("%d", &a[i]);
  15.  
  16. int tmp = ;
  17. for (int i = ; i <= n; i++)
  18. if (a[i] == a[i+]) tmp++;
  19. else sum[++tot] = tmp, num[tot] = a[i], tmp = ;
  20.  
  21. for (int i = ; i <= tot; i++)
  22. l[i] = i-, r[i] = i+, q.push(Node(sum[i], -i));
  23.  
  24. int ans = ;
  25. while(!q.empty())
  26. {
  27. int len = q.top().first, index = -q.top().second;
  28. q.pop();
  29. if (flag.count(Node(len, -index))) continue;
  30.  
  31. ans++;
  32.  
  33. int left = l[index], right = r[index];
  34. if (left >= && right <= tot && num[left] == num[right])
  35. {
  36. flag.insert(Node(sum[left], -left));
  37. flag.insert(Node(sum[right], -right));
  38. sum[left] += sum[right];
  39. q.push(Node(sum[left], -left));
  40.  
  41. r[left] = r[right];
  42. l[r[right]] = left;
  43. }
  44. else
  45. r[left] = right, l[right] = left;
  46. }
  47.  
  48. printf("%d\n", ans);
  49. }

CodeForces - 899E 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 Round #452 (Div. 2) 899E E. Segments Removal

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

  4. codeforce452DIV2——E. Segments Removal

    题目 Vasya has an array of integers of length n. Vasya performs the following operations on the array: ...

  5. Running Median POJ - 3784 (对顶堆/优先队列 | 链表)

    For this problem, you will write a program that reads in a sequence of 32-bit signed integers. After ...

  6. Codeforces 681C. Heap Operations 优先队列

    C. Heap Operations time limit per test:1 second memory limit per test:256 megabytes input:standard i ...

  7. Codeforces 948C Producing Snow(优先队列+思维)

    题目链接:http://codeforces.com/contest/948/problem/C 题目大意:给定长度n(n<=1e5),第一行v[i]表示表示第i堆雪的体积,第二行t[i]表示第 ...

  8. Codeforces Gym 101291C【优先队列】

    <题目链接> 题目大意: 就是一道纯模拟题,具体模拟过程见代码. 解题分析:要掌握不同优先级的优先队列的设置.下面是对优先队列的使用操作详解: priority_queue<int& ...

  9. CodeForces - 799B-T-shirt buying (优先队列)

    题目链接 /* Name: Copyright: Author: Date: 2018/5/2 16:09:54 Description:优先队列 */ #include <iostream&g ...

随机推荐

  1. ASM 磁盘组的的scrip

    之前经常用如下方式进行查询:步骤 1 以oracle用户登录系统.步骤 2 执行如下命令改变ORACLE_SID环境变量.$ export ORACLE_SID=+ASM1[1或者2]需要通过ps - ...

  2. SpringBoot---核心---日志配置

  3. postgresql修改数据库名

    alter database abc rename to cba;

  4. (转)io优化

    原文:http://blog.csdn.net/gzh0222/article/details/9227393 1.系统学习 IO性能对于一个系统的影响是至关重要的.一个系统经过多项优化以后,瓶颈往往 ...

  5. CM5.7.2 yum离线安装笔记

    一.建立yum本地服务源(yum支持http和ftp两种协议,这里使用http协议)  1.启动httpd服务   启动命令:service httpd start   关闭命令:service ht ...

  6. FirstAFNetWorking

    // ViewController.h // FirstAFNetWorking // // Created by 张国锋 on 15/7/20. // Copyright (c) 2015年 张国锋 ...

  7. 关于UITableView的性能优化(历上最全面的优化分析)

    (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath ...

  8. static 关键字用法

    static a=0; 就是把a初始化为0:初始值为0而已 即使a是局部变量,每次进入此变量所在的函数,a值还是保持上次赋值: 在中断里建议在局部变量前加上static,以确保此变量值的寿命

  9. fiddler设置只抓取某一域名请求

    简单易懂~

  10. MySQL表的碎片整理和空间回收小结

    MySQL表碎片化(Table Fragmentation)的原因 关于MySQL中表碎片化(Table Fragmentation)产生的原因,简单总结一下,MySQL Engine不同,碎片化的原 ...