题目

Vasya has an array of integers of length n.

Vasya performs the following operations on the array: on each step he finds the longest segment of consecutive equal integers (the leftmost, if there are several such segments) and removes it. For example, if Vasya's array is [13, 13, 7, 7, 7, 2, 2, 2], then after one operation it becomes [13, 13, 2, 2, 2].

Compute the number of operations Vasya should make until the array becomes empty, i.e. Vasya removes all elements from it.

分析

链表+优先队列

 #include <cstdio>
#include <algorithm>
#include <cstring>
#include <iostream>
#include <queue> using namespace std;
const int maxn=+;
int a[maxn];
int vis[maxn]; struct Node{
int len,id,val,l;
bool operator <(const Node &rhs)const{
return len<rhs.len||(len==rhs.len&&l>rhs.l);
}
}node[maxn]; int n;
int Next[maxn],Last[maxn];
int main(){
memset(vis,,sizeof(vis));
scanf("%d",&n);
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
}
int cnt=,m=;
for(int i=;i<=n;i++){
cnt++;
if(a[i]!=a[i+]){
node[++m].len=cnt;
node[m].id=m;
node[m].val=a[i];
node[m].l=i;
cnt=;
}
}
priority_queue<Node>q;
//printf("%d",m); for(int i=;i<=m;i++){
Next[i]=i+;
Last[i]=i-;
q.push(node[i]);
}
Next[m]=;Last[]=;
int ans=;
while(!q.empty()){
while(!q.empty()&&vis[q.top().id])q.pop();
if(!q.empty()){
Node x=q.top();q.pop();
//printf("%d :%d %d\n",x.id,Next[x.id],Last[x.id]);
//printf("%d %d %d\n",x.id,x.len,x.val);
ans++;
Next[Last[x.id]]=Next[x.id];
Last[Next[x.id]]=Last[x.id];
if(Last[x.id]&&Next[x.id]&&node[Last[x.id]].val==node[Next[x.id]].val&&!vis[node[Last[x.id]].id]&&!vis[node[Next[x.id]].id]){
vis[node[Last[x.id]].id]=;
vis[node[Next[x.id]].id]=; int l=node[Next[x.id]].l;
node[++m].val=node[Last[x.id]].val;
node[m].len=node[Last[x.id]].len+node[Next[x.id]].len;
node[m].id=m;
node[m].l=l;
Next[Last[Last[x.id]]]=m;
Last[Next[Next[x.id]]]=m;
Next[m]=Next[Next[x.id]];
Last[m]=Last[Last[x.id]];
q.push(node[m]);
}
}
}
printf("%d",ans); return ;
}

codeforce452DIV2——E. Segments Removal的更多相关文章

  1. Codeforces 899E - Segments Removal

    899E - Segments Removal 思路:priority_queue+pair 代码: #include<bits/stdc++.h> using namespace std ...

  2. 【CodeForces】899 E. Segments Removal

    [题目]E. Segments Removal [题意]给定n个数字,每次操作删除最长的连续相同数字(等长删最左),求全部删完的最少次数.n<=2*10^6,1<=ai<=10^9. ...

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

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

  4. Codeforces Round #452 (Div. 2) 899E E. Segments Removal

    题 OvO http://codeforces.com/contest/899/problem/E Codeforces Round #452 (Div. 2) - e 899E 解 用两个并查集(记 ...

  5. Codeforces Round #451 & Codeforces Round #452

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

  6. Background removal with deep learning

    [原文链接] Background removal with deep learning   This post describes our work and research on the gree ...

  7. [LeetCode] Number of Segments in a String 字符串中的分段数量

    Count the number of segments in a string, where a segment is defined to be a contiguous sequence of ...

  8. Greenplum记录(一):主体结构、master、segments节点、interconnect、performance monitor

    结构:Client--master host--interconnect--segment host 每个节点都是单独的PG数据库,要获得最佳的性能需要对每个节点进行独立优化. master上不包含任 ...

  9. Application package 'AndroidManifest.xml' must have a minimum of 2 segments.

    看了源码就是packagename里面必须包含一个. 源码在: ./sdk/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/id ...

随机推荐

  1. 分布式使用Redis

    为什么我们做分布式使用Redis? https://www.cnblogs.com/yaodengyan/p/9717080.html 绝大部分写业务的程序员,在实际开发中使用 Redis 的时候,只 ...

  2. 读论文系列:Object Detection ICCV2015 Fast RCNN

    Fast RCNN是对RCNN的性能优化版本,在VGG16上,Fast R-CNN训练速度是RCNN的9倍, 测试速度是RCNN213倍:训练速度是SPP-net的3倍,测试速度是SPP-net的3倍 ...

  3. 【java规则引擎】java规则引擎搭建开发环境

    Drools官网:http://www.jboss.org/drools Drools and jBPM consist out of several projects:(Drools软件包提供的几个 ...

  4. 十二、python沉淀之路--内置函数

    1.abs函数,求绝对值. a = abs(-3) print(a) 返回:3 2.all函数:判断是否是可迭代对象. 官方解释:Return True if bool(x) is True for ...

  5. Linux环境下安装jenkins

    废话不多说,直接开始 1.从官网下载Jenkins的war包 2.下载好的War放到Tomcat的网站根目录webapps下,然后启动Tomcat. 3.打开浏览器,输入http://IP:8080/ ...

  6. Hbase 参数配置及优化

    From:http://www.open-open.com/lib/view/open1346684547787.html 接触hbase已有半年的时间,查了很多资料,也参考了很多别人心得,也希望把自 ...

  7. css3中做3D导航栏

    看别人做的一个3D导航栏,觉得很厉害,这里先保存下来,后面有时间好好分析一下: <!doctype html> <html lang="en"> <h ...

  8. Linux下GCC和Makefile实例(从GCC的编译到Makefile的引入)

    一.确认已经装好了GCC和Make的软件包 可以使用whereis命令查看: 如果whereis  gcc和whereis  make命令有结果,说明安装了这两个软件,可以继续往下做. 二.使用GCC ...

  9. Data_Structure-绪论作业

    一.作业题目 仿照三元组或复数的抽象数据类型写出有理数抽象数据类型的描述 (有理数是其分子.分母均为整数且分母不为零的分数). 有理数基本运算: 构造有理数T,元素e1,e2分别被赋以分子.分母值 销 ...

  10. windows下通过.bat运行java程序

    在windows下运行Java项目,单独的jar可以使用,java -jar xxx.jar 运行,如果是一个zip包,里面包含了class文件和所依赖的jar的时候,可以使用 (也可以以看看这里): ...