codeforce452DIV2——E. Segments Removal
题目
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的更多相关文章
- Codeforces 899E - Segments Removal
899E - Segments Removal 思路:priority_queue+pair 代码: #include<bits/stdc++.h> using namespace std ...
- 【CodeForces】899 E. Segments Removal
[题目]E. Segments Removal [题意]给定n个数字,每次操作删除最长的连续相同数字(等长删最左),求全部删完的最少次数.n<=2*10^6,1<=ai<=10^9. ...
- CodeForces - 899E Segments Removal (优先队列 + 链表)
给定一个序列,每次从序列中找一个长度最大的元素相同的片段,删除它. 如果长度相同,删除最靠左边的那个片段. 问,需要删几次. 用链表处理删除片段.对于删除之后两边又能连成一个片段的那种情况,用set记 ...
- Codeforces Round #452 (Div. 2) 899E E. Segments Removal
题 OvO http://codeforces.com/contest/899/problem/E Codeforces Round #452 (Div. 2) - e 899E 解 用两个并查集(记 ...
- Codeforces Round #451 & Codeforces Round #452
Rounding Solution Proper Nutrition 枚举 Solution Phone Numbers 模拟 Solution Alarm Clock 贪心,好像不用线段树也可以,事 ...
- Background removal with deep learning
[原文链接] Background removal with deep learning This post describes our work and research on the gree ...
- [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 ...
- Greenplum记录(一):主体结构、master、segments节点、interconnect、performance monitor
结构:Client--master host--interconnect--segment host 每个节点都是单独的PG数据库,要获得最佳的性能需要对每个节点进行独立优化. master上不包含任 ...
- Application package 'AndroidManifest.xml' must have a minimum of 2 segments.
看了源码就是packagename里面必须包含一个. 源码在: ./sdk/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/id ...
随机推荐
- zoj-3963 Heap Partition(贪心+二分+树状数组)
题目链接: Heap Partition Time Limit: 2 Seconds Memory Limit: 65536 KB Special Judge A sequence ...
- cursor光标类型
今天早上在网上看到一篇关于光标类型的总结代码,很好,特定拿来: 最终结果: 代码: <!DOCTYPE html> <html lang="zh-cn"> ...
- 人生苦短之我用Python篇(基础)
Python简介 Python,是一种面向对象的解释型计算机程序设计语言,由荷兰人Guido van Rossum于1989年发明,第一个公开发行版发行于1991年. Python是纯粹的自由软件, ...
- Linux下shell命令 1
1 [root@hadoop-namenode-1 iebd] cd /filename/filename 跳转至filename文件夹 2 [root@hadoop-namenode-1 ...
- 剑指Offer面试题:12.链表的倒数第K个结点
一 题目:链表的倒数第K个结点 题目:输入一个链表,输出该链表中倒数第k个结点.为了符合大多数人的习惯,本题从1开始计数,即链表的尾结点是倒数第1个结点.例如一个链表有6个结点,从头结点开始它们的值依 ...
- 关于verilog中小数直接赋值
verilog中小数直接赋值的话小数会近似成1,如0.1,0.6,0.9赋值的话就会变成1,5.1,5.9也都会变成6.并且quartus默认小数是64位.
- BZOJ4832: [Lydsy2017年4月月赛]抵制克苏恩
传送门 题目大意: 攻击k次,每次可攻击随从或英雄. 随从数不大于7个,且1滴血的a个,2滴b个,3滴c个. 攻击一次血-1,如果随从没死可以生成3滴血随从一个 题解: 概率/期望dp f[i][j] ...
- VS 2015 开发Android底部导航条----[实例代码,多图]
1.废话背景介绍 在Build 2016开发者大会上,微软宣布,Xamarin将被整合进所有版本的Visual Studio之中. 这也就是说,Xamarin将免费提供给所有购买了Visual ...
- 洛谷 2680 (NOIp2015) 运输计划
题目:https://www.luogu.org/problemnew/show/P2680 因为是最长的时间最短,所以二分! 离线LCA可以知道路径长度.每次只看超过二分值的路径. 原本的想法是遍历 ...
- UNION和UNION ALL关键字
UNION和UNION ALL关键字都是将两个结果集合并为一个,但这两者从使用和效率上来说都有所不同. 1.对重复结果的处理:UNION在进行表链接后会筛选掉重复的记录,Union All不会去除重复 ...