题目

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. iOS开发之Documentation.build/Script-BC552B3A15.sh:

    /Users/hbbhao/Library/Developer/Xcode/DerivedData/AWLive-dmbegyhgamayzudqqdentwngdpkr/Build/Intermed ...

  2. [分享]Google 全球 IP 地址库[Google Global Cache IPs]

    Google 全球 IP 地址库 IP 地址来源:http://www.kookle.co.nr,共计4351个. Bulgaria 93.123.23.1 93.123.23.2 93.123.23 ...

  3. 转载:TCP连接的状态详解以及故障排查

    FROM:http://blog.csdn.net/hguisu/article/details/38700899 该博文的条理清晰,步骤明确,故复制到这个博文中收藏,若文章作者看到且觉得不能装载,麻 ...

  4. JAVA视频链接

    Java基础Java马士兵:链接:https://pan.baidu.com/s/1jJRvxGi密码:v3xb Java刘意:链接:https://pan.baidu.com/s/1kVZQCqr密 ...

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

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

  6. ACM学习历程—HDU5700 区间交(树状数组 && 前缀和 && 排序)

    http://acm.hdu.edu.cn/showproblem.php?pid=5700 这是这次百度之星初赛2B的第五题.省赛回来看了一下,有这样一个思路:对于所有的区间排序,按左值排序. 然后 ...

  7. 【Xamarin 】MonoTouch - UIImageView响应点击事件

    //圆角头像 UIImageView _avatarView = new UIImageView(new RectangleF(_blockSpace, _blockSpace, 2 * _avata ...

  8. ③SpringBoot中Redis的使用

    本文基于前面的springBoot系列文章进行学习,主要介绍redis的使用. SpringBoot对常用的数据库支持外,对NoSQL 数据库也进行了封装自动化. redis介绍 Redis是目前业界 ...

  9. Android之Application类用法

    Application和Activity,Service一样是Android框架的一个系统组件,当Android程序启动时系统会创建一个Application对象,用来存储系统的一些信息. Andro ...

  10. Difference between boot ip. service ip and persistent ip in hacmp

    - boot IP is the original address on a network interface even when the cluster is down - service IP ...