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的更多相关文章

  1. CodeForces - 899E Segments Removal (优先队列 + 链表)

    给定一个序列,每次从序列中找一个长度最大的元素相同的片段,删除它. 如果长度相同,删除最靠左边的那个片段. 问,需要删几次. 用链表处理删除片段.对于删除之后两边又能连成一个片段的那种情况,用set记 ...

  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. CodeForces 22D Segments 排序水问题

    主题链接:点击打开链接 升序右键点.采取正确的点 删边暴力 #include <cstdio> #include <cstring> #include <algorith ...

  6. Codeforces Round #451 & Codeforces Round #452

    Rounding Solution Proper Nutrition 枚举 Solution Phone Numbers 模拟 Solution Alarm Clock 贪心,好像不用线段树也可以,事 ...

  7. 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 ...

  8. codeforces 895B XK Segments 二分 思维

    codeforces 895B XK Segments 题目大意: 寻找符合要求的\((i,j)\)对,有:\[a_i \le a_j \] 同时存在\(k\),且\(k\)能够被\(x\)整除,\( ...

  9. Codeforces Beta Round #14 (Div. 2) C. Four Segments 水题

    C. Four Segments 题目连接: http://codeforces.com/contest/14/problem/C Description Several months later A ...

随机推荐

  1. php开启mbstring扩展并设置支持utf-8编码

    前一段时间使用一个服务的接口,因为调用接口时使用的参数里面有中文,调用接口会出现异常问题,后来才明白是编码不一致的问题.然而,我本地项目开发使用的是utf-8,接口那边也是需要utf-8的,那么问题来 ...

  2. java常用类总结

    0.jar包下载地点 http://mvnrepository.org/ 1.序列化反序列化Object代码 百度云:http://pan.baidu.com/disk/home#list/path= ...

  3. select,radio,checkbox兼容性

  4. python getatime() 查看文件的访问时间

    import time,os def main(): file_name=r'C:\Temp\Req.xml' file_times_access=time.localtime(os.path.get ...

  5. Node.js最新技术栈之Promise篇

    前言 大家好,我是桑世龙,github和cnodejs上的i5ting,目前在天津创业,公司目前使用技术主要是nodejs,算所谓的MEAN(mongodb + express + angular + ...

  6. mysql查询操作1

    ##1.在已有的表中插入一行记录 insert into tb_name values("",""...); ##2.查询语句的框架和用法 select 字段名 ...

  7. php json_decode返回null

    在使用json_decode函数想把json串转化为数组的时候,出现了null,当时还以为是因为json对字符串的长度有限制,还以为是因为两边少了引号,经过多次处理,发现都没有效果. 百度各种帖子,发 ...

  8. 08: python基础练习题

    1.while循环实现输出2 - 3 + 4 - 5 + 6 ... + 100 的和 # 使用while循环实现输出2 - 3 + 4 - 5 + 6 ... + 100 的和 s = 0 i = ...

  9. 20165310java_teamExp1_week1

    结对编程项目-四则运算-week1 需求分析 第一周达成 支持真分数的四则运算 支持多运算符 能手动输入n道题目,n由使用者输入 后续拓展的可能 能随机生成n道题目,n由使用者输入 能够判断正误,错误 ...

  10. C++ vector 删除一个指定元素 和 find 一个指定元素以及遍历删除、 map遍历删除元素和删除find到的元素

    vector: 1.delete element 转载:http://www.cnblogs.com/xudong-bupt/p/3522457.html #include <vector> ...