CodeForces - 899E Segments Removal (优先队列 + 链表)
给定一个序列,每次从序列中找一个长度最大的元素相同的片段,删除它。
如果长度相同,删除最靠左边的那个片段。
问,需要删几次。

用链表处理删除片段。对于删除之后两边又能连成一个片段的那种情况,用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 (优先队列 + 链表)的更多相关文章
- Codeforces 899E - Segments Removal
899E - Segments Removal 思路:priority_queue+pair 代码: #include<bits/stdc++.h> using namespace std ...
- 【CodeForces】899 E. Segments Removal
[题目]E. Segments Removal [题意]给定n个数字,每次操作删除最长的连续相同数字(等长删最左),求全部删完的最少次数.n<=2*10^6,1<=ai<=10^9. ...
- Codeforces Round #452 (Div. 2) 899E E. Segments Removal
题 OvO http://codeforces.com/contest/899/problem/E Codeforces Round #452 (Div. 2) - e 899E 解 用两个并查集(记 ...
- codeforce452DIV2——E. Segments Removal
题目 Vasya has an array of integers of length n. Vasya performs the following operations on the array: ...
- Running Median POJ - 3784 (对顶堆/优先队列 | 链表)
For this problem, you will write a program that reads in a sequence of 32-bit signed integers. After ...
- Codeforces 681C. Heap Operations 优先队列
C. Heap Operations time limit per test:1 second memory limit per test:256 megabytes input:standard i ...
- Codeforces 948C Producing Snow(优先队列+思维)
题目链接:http://codeforces.com/contest/948/problem/C 题目大意:给定长度n(n<=1e5),第一行v[i]表示表示第i堆雪的体积,第二行t[i]表示第 ...
- Codeforces Gym 101291C【优先队列】
<题目链接> 题目大意: 就是一道纯模拟题,具体模拟过程见代码. 解题分析:要掌握不同优先级的优先队列的设置.下面是对优先队列的使用操作详解: priority_queue<int& ...
- CodeForces - 799B-T-shirt buying (优先队列)
题目链接 /* Name: Copyright: Author: Date: 2018/5/2 16:09:54 Description:优先队列 */ #include <iostream&g ...
随机推荐
- github 新建一个分支
我能说今天在github上新建分支的时候懵逼了半天吗..为了下次不再懵逼,还是在这里记录一下吧.. 进入你的项目---code---Branch----点击那个倒三角-----你会发现一个输入框(这是 ...
- restful 风格 加上springmvc
一.spring 版本:spring-framework-3.2.7.RELEASE 二.所需其它Jar包: 三.主要代码: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ...
- 关于ajax中return并不能作为方法的返回值
接下来关于ajax中的return值最后没有办法是方法的最终返回值问题 login(username,password) { console.log("进入方法"); $.ajax ...
- 一步一步教你玩转.NET Framework的配置文件app.config
转自https://www.cnblogs.com/tonnie/archive/2010/12/17/appconfig.html 在一般的项目中,为了使你的代码更加灵活,更方便调整,减少不必要的h ...
- Ubuntu下安装XAMPP
来源:http://www.ido321.com/1265.html 最近,我也玩起了Linux了,瞬间觉得自己逼格又上去了,所以,就给笔记本安装了Ubuntu+Win7双系统.当然在Ubuntu下必 ...
- Spark集群任务提交
1. 集群管理器 Spark当前支持三种集群管理方式 Standalone—Spark自带的一种集群管理方式,易于构建集群. Apache Mesos—通用的集群管理,可以在其上运行Hadoop Ma ...
- 团队作业-Beta冲刺(周三)
这个作业属于哪个课程 https://edu.cnblogs.com/campus/xnsy/SoftwareEngineeringClass1 这个作业要求在哪里 https://edu.cnblo ...
- .vue公共组件裁减导航
场景: 有一个公共头部和底部,vue搭建的框架,在app.vue里写的公共方法,首页是个登录页面,不需要公共部分,在这基础上进行公共部分的显示隐藏. 即注册页.登录页.404页面都不要导航 代码: ( ...
- Python——字典dict()详解
一.字典 字典是Python提供的一种数据类型,用于存放有映射关系的数据,字典相当于两组数据,其中一组是key,是关键数据(程序对字典的操作都是基于key),另一组数据是value,可以通过key来进 ...
- 2_分布式计算框架MapReduce
一.mr介绍 1.MapReduce设计理念是移动计算而不是移动数据,就是把分析计算的程序,分别拷贝一份到不同的机器上,而不是移动数据. 2.计算框架有很多,不是谁替换谁的问题,是谁更适合的问题.mr ...