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 ...
随机推荐
- Redis 十分钟快速入门
本教程是一个快速入门教程,所以Redis的命令只是简单介绍了几个常用的,如果有其他需求请求官网查看API 使用. 1. Redis简介 Redis 是完全开源免费的,遵守BSD协议,是一个高性能的ke ...
- 模仿WIN32程序处理消息
#include "stdafx.h" #include "MyMessage.h" #include <conio.h> using namesp ...
- 改变 select下拉框 样式
select{ outline: none; text-indent: 10px; height: 45px; line-height: 45px; width: 100%; border:1px s ...
- 跑道标识和那些复杂的灯光系统 and 简介、编号、参数、标志及数量 and 飞机跑道标准与参数
http://www.360doc.com/content/16/0616/12/32670666_568219786.shtml http://news.carnoc.com/list/365/36 ...
- [Data Access] ORM 原理 (11): 效能議題
這絕對是 ORM 的使用者,開發人員與 DBAs 共同想要問的議題,到底我使用了 ORM 和使用傳統的 ADO.NET 下 SQL 指令的方式會差多少? 這個問題不但會發生在 Entity Frame ...
- iframe嵌套
iframe基本内涵 通常我们使用iframe直接直接在页面嵌套iframe标签指定src就可以了. <iframe src="demo_iframe_sandbox.htm" ...
- eclipse svn插件 设置自动加锁相关
eclipse svn插件 设置自动加锁相关 Subclipse 1.10.9 发布,改进说明:SVNKit 1.8.8Exception proof repository sorter. (1616 ...
- [转载]ASP.NET-----Repeater数据控件的用法总结
一.Repeater控件的用法流程及实例: 1.首先建立一个网站,新建一个网页index.aspx. 2.添加或者建立APP_Data数据文件,然后将用到的数据库文件放到APP_Data文件夹中. 3 ...
- 删除github上个人的repositories的操作步骤
- python构造栈结构
栈:是一种先进后出的数据结构:本片文章,我们用python的面向对象来构造这样的数据结构. 栈中的每一个数据除了存储当前的数值外,还存储着当前数值下一个数据的类型(注意不是下一个数据的数值). cla ...