题目

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. Unity3d 背景、音效 播放 简单demo

    仅实现功能,AudioListener在MainCamera中 using UnityEngine; using System.Collections; using System.Collection ...

  2. C/C++ 笔试题一

    摘自 网络上的 笔试题,据说是华为的,考察的内容还算全面,也很细致: 答案 疏略 检查了下,应该没有什么大问题,但是 还是那句话,尽信之不如无,所以还是要自己思考 1.static有什么用途?(请至少 ...

  3. python 中出现 “IndentationError: expected an indented block” 问题

    python 学习 在定义Python函数的时候如下 >>>def hello() . . .print "hello" 这样会报错的,报错如下: Indenta ...

  4. HTML里 iframe跳转后关闭iframe

    if(window != top){      top.location.href = location.href;    }

  5. Spring字符编码过滤器

    1 <filter> 2 <filter-name>characterEncodingFilter</filter-name> 3 <filter-class ...

  6. vs中删除nuget包

    最近发现有些解决方案都是用来nuget包,这个偶尔能跑,但是有一个爱抽风的毛病,生成解决方案的时候报错:无法连接到远程服务器,真几把蛋疼.... 就是下图的情况 网上找了下不是很容易找到处理这个问题的 ...

  7. BZOJ3632: 外太空旅行

    BZOJ1547: 周末晚会 https://lydsy.com/JudgeOnline/problem.php?id=1547 分析: 对于一个串旋转若干次会回到本身,旋转次数即是同构个数,这个东西 ...

  8. [Luogu3769][CH弱省胡策R2]TATT

    luogu 题意 其实就是四维偏序. sol 第一维排序,然后就只需要写个\(3D-tree\)了. 据说\(kD-tree\)的单次查询复杂度是\(O(n^{1-\frac{1}{k}})\).所以 ...

  9. niosii 改变软核之后重新编译方法

    操作系统:Win7 64 bit 开发环境:Quartus II 12.0 (64-Bit)  + Nios II 12.0 Software Build Tools for Eclipse 使用Qu ...

  10. 转:django中session的实现机制

    转:www.jianshu.com 要理解session,首先要搞清楚cookie的概念.由于http是无状态的,服务器不能记住用户的信息状态,因此若由同一个客户端发起的多条请求,服务器不能辨别这些请 ...