题目

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. Auto Layout简单应用——以编码的方式实现Auto Layout自动布局(二)

    在上一篇文章iOS学习笔记02——以编码的方式实现Auto Layout自动布局(一)中我们简单的介绍了使用Visual Format Language创建布局约束来实现自动布局,这种方法创建的布局约 ...

  2. 剑指offer-第四章解决面试题思路(判断一个数组是否为二叉搜索树的后序遍历序列)

    二叉搜索树:二叉搜索树根节点的左边都比根节点小,右边都比根节点大. 例题:输入一个数组,判断是否为二叉搜索树的后序遍历序列,如果是,返回true,如果不是,返回flase,假设没有重复的元素. 思路: ...

  3. 【4】JDK和CGLIB生成动态代理类的区别

    当一个对象(客户端)不能或者不想直接引用另一个对象(目标对象),这时可以应用代理模式在这两者之间构建一个桥梁--代理对象. 按照代理对象的创建时期不同,可以分为两种: 静态代理:事先写好代理对象类,在 ...

  4. Cam350导入Allegro的*.rou文件

    如果生产allegro的生产文件有椭圆形钻孔,生成.rou.直接自动导入到cam350是没办法成功的. 以下说说本人的步骤.allegro里面的单位都是mm 在cam350的File-->Imp ...

  5. w3cschool在线教程

    做网页开发的,没有不知道w3cschool的,如果你还不知道,那么就应该早点看下面推荐的文章,菜鸟可以帮你提升你的技能,老鸟可以温故而知新. 第一个是:http://www.w3school.com. ...

  6. Hadoop体系结构之 Mapreduce

    MR框架是由一个单独运行在主节点上的JobTracker和运行在每个集群从节点上的TaskTracker共同组成.主节点负责调度构成一个作业的所有任务,这些任务分布在不同的不同的从节点上.主节点监视它 ...

  7. Oracle中的存储过程简单例子

    --创建表create table TESTTABLE(  id1  VARCHAR2(12),  name VARCHAR2(32))select t.id1,t.name from TESTTAB ...

  8. 通过docker构建zabbix监控系统

    下载zabbix的镜像 $ docker pull berngp/docker-zabbix Using default tag: latest latest: Pulling from berngp ...

  9. Linux添加路由

    在Linux的VM中可以添加.删除路由. 中如图的拓扑结构中需要在中间的VM上添加路由,使最左边的VM和最右边的VM实现互通. 在这台VM上需要添加IP Forwarding的功能,在操作系统中也需要 ...

  10. 找回mysql root用户的密码

    1.停掉mysql服务ps -ef | grep mysqldkill -9 mysql进程的pid2.vi /etc/my.cnf找到[mysqld]在下面添加一行skip-grant-tables ...