题目

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 ;
}

最新文章

  1. 最详细的Log4j使用教程
  2. 【Effective Java】12、避免过度同步
  3. iOS开发——网络篇——文件下载(NSMutableData、NSFileHandle、NSOutputStream)和上传、压缩和解压(三方框架ZipArchive),请求头和请求体格式,断点续传Range
  4. [Android]如何获取当前用户设置的时区
  5. TortoiseSVN,排除不想提交文件的方法(转)
  6. Shell—学习之心得
  7. 存量数据处理结果查询.txt
  8. Java中线程顺序执行
  9. 无法自动调试 未能调试远程过程。这通常说明未在服务器上启用调试 WCF 托管在IIS上
  10. Linux下多任务间通信和同步-概述
  11. Easyui datagrid 批量编辑和提交
  12. Android的内存优化
  13. Gatling简单测试SpringBoot工程
  14. hdu4791-Alice&#39;s Print Service
  15. MySQL 批量修改某一列的值为另外一个字段的值
  16. 如何创建带有大纲和书签的交互式web报表
  17. java 集合(五)MapDemo
  18. prometheus比zabbix好在哪点?
  19. Vmware Vcenter6.5 全新安装及群集配置介绍
  20. capacity &lt;&lt;= 1

热门文章

  1. [置顶] 关于Android实现 退出登录那些小事?
  2. 机器学习算法实现解析——libFM之libFM的训练过程概述
  3. js 获取元素宽
  4. 使用pdfcrack破解PDF密码(Linux)
  5. Kali Linux ettercap的使用
  6. HDU - 6231:K-th Number (不错的二分)
  7. Python Requests快速入门
  8. LOJ103 子串查找
  9. 洛谷P1979 华容道
  10. 生产环境连接数据库失败:Cannot create PoolableConnectionFactory❨Got mins one from a read call❩