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. c++四种强制类型转化

    c++ 数据类型转换: static_cast dynamic_cast reinterpret_cast const_cast C++ 类型转换(C风格的强制转换): (1)将浮点型数据赋值给整型变 ...

  2. Mysql查询一个表的所有字段名

    select COLUMN_NAME from information_schema.`COLUMNS`        -- 这行不变,照抄 where TABLE_SCHEMA = 'xerp'   ...

  3. java常用功能

    1.复制文件 private void fileChannelCopy(File source, File target) throws IOException { FileInputStream f ...

  4. Nginx启动SSL功能

    Nginx启动SSL功能,并进行功能优化,你看这个就足够了 一:开始Nginx的SSL模块 1.1 Nginx如果未开启SSL模块,配置Https时提示错误 nginx: [emerg] the &q ...

  5. Python装饰器的高级用法(翻译)

    原文地址 https://www.codementor.io/python/tutorial/advanced-use-python-decorators-class-function 介绍 我写这篇 ...

  6. Python3 itchat实现微信定时发送群消息

    Python3 itchat实现微信定时发送群消息 一.简介 1,使用微信,定时往指定的微信群里发送指定信息. 2,需要发送的内容使用excel进行维护,指定要发送的微信群名.时间.内容. 二.py库 ...

  7. 20165310java_teamExp1_week1

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

  8. [c/c++]指针(2)

    首先呢,讲讲数组 数组就是一连串的地址对不对?所以它们的地址是紧挨着的 1 | 2 | 3 | 4 | 2 | 0 1 2 3 4 那我们把一个数组的首地址赋给一个指针变量 ] = {, , , , ...

  9. C# 将文件转换为 Stream

    public Stream FileToStream(string fileName) { // 打开文件 FileStream fileStream = new FileStream(fileNam ...

  10. 常用模块之hashlib,subprocess,logging,re,collections

    hashlib 什么是hashlib 什么叫hash:hash是一种算法(3.x里代替了md5模块和sha模块,主要提供 SHA1, SHA224, SHA256, SHA384, SHA512 ,M ...