Codeforces 899E - Segments Removal
思路:priority_queue+pair
代码:
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mp make_pair
#define mem(a,b) memset(a,b,sizeof(a)) const int N=2e5+;
int pre[N];
int nxt[N];
int col[N];
int num[N];
priority_queue<pair<int,int> >q,del;
int main()
{
ios::sync_with_stdio(false);
cin.tie();
int n,a,cnt=,ans=;
cin>>n;
for(int i=;i<n;i++)
{
cin>>a;
if(a==col[cnt])num[cnt]++;
else col[++cnt]=a,num[cnt]=;
}
for(int i=;i<=cnt;i++)pre[i]=i-,nxt[i]=i+,q.push(mp(num[i],-i));//建立链表,区间入队,-i是因为优先选择最左边的区间
while(cnt)
{
while(!del.empty()&&del.top()==q.top())del.pop(),q.pop();//删除已经被合并的区间
a=-q.top().second;
q.pop();
int t=pre[a],_t=nxt[a];
pre[_t]=t,nxt[t]=_t;
if(t&&col[t]==col[_t])//如果左右两个区间颜色一样,合并两个区间
{
del.push(mp(num[t],-t)),del.push(mp(num[_t],-_t));//把合并前的区间放进待删队列
num[t]+=num[_t],nxt[t]=nxt[_t],pre[nxt[_t]]=t;
q.push(mp(num[t],-t));
cnt--;
}
cnt--;
ans++;
}
cout<<ans<<endl;
return ;
}
Codeforces 899E - Segments Removal的更多相关文章
- CodeForces - 899E Segments Removal (优先队列 + 链表)
给定一个序列,每次从序列中找一个长度最大的元素相同的片段,删除它. 如果长度相同,删除最靠左边的那个片段. 问,需要删几次. 用链表处理删除片段.对于删除之后两边又能连成一个片段的那种情况,用set记 ...
- 【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: ...
- CodeForces 22D Segments 排序水问题
主题链接:点击打开链接 升序右键点.采取正确的点 删边暴力 #include <cstdio> #include <cstring> #include <algorith ...
- Codeforces Round #451 & Codeforces Round #452
Rounding Solution Proper Nutrition 枚举 Solution Phone Numbers 模拟 Solution Alarm Clock 贪心,好像不用线段树也可以,事 ...
- Codeforces Round #337 (Div. 2) D. Vika and Segments 线段树扫描线
D. Vika and Segments 题目连接: http://www.codeforces.com/contest/610/problem/D Description Vika has an i ...
- codeforces 895B XK Segments 二分 思维
codeforces 895B XK Segments 题目大意: 寻找符合要求的\((i,j)\)对,有:\[a_i \le a_j \] 同时存在\(k\),且\(k\)能够被\(x\)整除,\( ...
- Codeforces Beta Round #14 (Div. 2) C. Four Segments 水题
C. Four Segments 题目连接: http://codeforces.com/contest/14/problem/C Description Several months later A ...
随机推荐
- 教你玩转产品管理系统iClap(PC端功能篇)
之前和大家介绍了iClap的基础功能, 这一次针对PC端右侧的工具栏再做一个详细的介绍 随着版本的更新迭代,陆续会有更多工具和功能推出! 导航 为项目成员提供网址浏览访问导航服务,帮助项目成员快速查找 ...
- 利用keyframes实现幻灯效果
源码如下: <style> @keyframes looppic{ from{ background:url(images/1.jpg); /*图片的地址*/ } 25%{ /*可以依据不 ...
- yii2 restful api——app接口编程实例
<?php namespace common\components; use common\models\Cart; use common\models\User; use Yii; use y ...
- JavaScript: apply 方法 详解(转)——非常好
转载自 http://www.cnblogs.com/KeenLeung/archive/2012/11/19/2778229.html 我在一开始看到javascript的函数apply和call ...
- php CI框架实现验证码功能和增强验证码安全性实战教程
php CI框架实现验证码功能和增强验证码安全性实战教程 CodeIgniter简称CI是最流行的一个php MVC框架之一,本人讲从实际项目使用中写系列实战经验,有别与其他的理论讲解文章,会附上实战 ...
- 深入理解kafka
摘自: <kafka权威指南> 集群间成员关系 Kafka 使用Zoo keeper 来维护集群成员的信息.每个broker 都有一个唯一标识符,这个标识符可以在配置文件里指定,也可以自动 ...
- P4878 [USACO05DEC]layout布局
P4878 [USACO05DEC]layout布局 差分约束 最短路径最长路,最长路径最短路 本题求的是最长路径,所以跑最短路 根据题意连边,然后spfa即可 注意要判断图的连通性,所以新建一个虚拟 ...
- Ubuntu下使用face_recognition进行人脸识别
Face Recognition是一个基于Python的人脸识别库,在github上地址如下:https://github.com/ageitgey/face_recognition. 看着挺好玩,本 ...
- Python 自学基础(四)——time模块,random模块,sys模块,os模块,loggin模块,json模块,hashlib模块,configparser模块,pickle模块,正则
时间模块 import time print(time.time()) # 当前时间戳 # time.sleep(1) # 时间延迟1秒 print(time.clock()) # CPU执行时间 p ...
- php 设置模式 单元素模式(单例模式或单件模式)
单元素模式: 某些应用程序资源是独占的,因为有且只有一个此类型的资源.应用程序每次包含且仅包含一个对象,那么这个对象就是一个单元素.指的是在应用程序的范围内只对指定的类创建一个实例.通常该模式中包含一 ...