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

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

问,需要删几次。

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

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

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

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

#include <bits/stdc++.h>
using namespace std;
#define maxn 2000000 + 1000
typedef pair<int, int> Node;
int a[maxn], l[maxn], r[maxn];
int sum[maxn], num[maxn];
set<Node> flag;
priority_queue<Node> q; int main()
{
int n, tot = ;
scanf("%d", &n);
for (int i = ; i <= n; i++) scanf("%d", &a[i]); int tmp = ;
for (int i = ; i <= n; i++)
if (a[i] == a[i+]) tmp++;
else sum[++tot] = tmp, num[tot] = a[i], tmp = ; for (int i = ; i <= tot; i++)
l[i] = i-, r[i] = i+, q.push(Node(sum[i], -i)); int ans = ;
while(!q.empty())
{
int len = q.top().first, index = -q.top().second;
q.pop();
if (flag.count(Node(len, -index))) continue; ans++; int left = l[index], right = r[index];
if (left >= && right <= tot && num[left] == num[right])
{
flag.insert(Node(sum[left], -left));
flag.insert(Node(sum[right], -right));
sum[left] += sum[right];
q.push(Node(sum[left], -left)); r[left] = r[right];
l[r[right]] = left;
}
else
r[left] = right, l[right] = left;
} printf("%d\n", ans);
}

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. go 从表结构生成结构体

    package main import ( "fmt" "github.com/gohouse/converter" ) func main() { // 初始 ...

  2. Java基础语法(Eclipse)

    Java基础语法 今日内容介绍 u Eclipse开发工具 u 超市库存管理系统 第1章 Eclipse开发工具 Eclipse是功能强大Java集成开发工具.它可以极大地提升我们的开发效率.可以自动 ...

  3. restframework安装及APIView分析

    一.restframework的安装 方式一:pip3 install djangorestframework 方式二:pycharm图形化界面安装 方式三:pycharm命令行下安装(装在当前工程所 ...

  4. querySelector/querySelectorAll

    querySelector获取页面I属性D为test的元素: document.getElementById("test"); //or document.querySelecto ...

  5. Cannot load JDBC driver class 'com.mysql.jdbc.Driver解决方法

    “Cannot load JDBC driver class 'com.mysql.jdbc.Driver ” 表示没有JDBC连接MySql的驱动包,因此需要手动添加驱动包到WEB-INF目录下的l ...

  6. web标准、可用性、可访问性

    前言:大家不难发现,只要是招聘UED相关的岗位,如前端开发工程师.交互设计师.用户研究员甚至视觉设计师,一般都对web标准.可用性和可访问性的理解有要求.那么到底什么是web标准.可用性.可访问性呢? ...

  7. Hive基础(1)

    Hive基础(1) Hive的HQL(2) 1. Hive并不是分布式的,它独立于机器之外,类似于Hadoop的客户端. 2. 元数据和数据的区别,前者如表名.列名.字段名等. 3. Hive的三种安 ...

  8. xmlHttpRequest在Firefox下不起作用?

    描述: XMLHttpRequest 在IE下正常,在Firefox下不起作用. 原因: XMLHttpRequest 对象的 onreadystatechange 不会在Firefox下执行, 解放 ...

  9. [论文理解] MobileNets: Efficient Convolutional Neural Networks for Mobile Vision Applications

    MobileNets: Efficient Convolutional Neural Networks for Mobile Vision Applications Intro MobileNet 我 ...

  10. 2018.4.21 如何正确快速安装/卸载云服务器Centos7安装GUI图形化界面GNOME桌面环境

    为云服务哦Centos安装图形化界面GNOME .KDE 1.开始前先验证一下能不能上网 ping www.baidu.com 2.接下来开始安装X(X Window System),命令为 yum ...