Codeforces Round #452 (Div. 2) 899E E. Segments Removal
题
OvO http://codeforces.com/contest/899/problem/E
Codeforces Round #452 (Div. 2) - e
899E
解
用两个并查集(记为fa和ma),
fa用于更新可以合并在一起的段,维护每个段的左端点,右端点,中间有多少个相同的值,和这个段的值得是什么,
ma用于跳跃,
具体来说
例如

这组数据
标上序号(第三行是序号)

1.那么首先合并4个5(10-13),显然9所在的段和14所在的段不能合并
那么把13指向9( ma[13]=9 ) 然后把10指向14( ma[10]=14 )
2.然后合并3个4( 7-9 ),判断合并的两个位置记为a和b,首先a=6,b本来等于10,然后通过并查集ma更改b=14,显然不能合并
那么同样 ma[7]=10, ma[9]=6
3.然后合并3个6( 14-16 ),判断合并的位置是 a=13 和 b=17 ,通过并查集ma更改a=6
显然 a 所在段的值和 b 所在段的值是一样的,而且a和b所在段都未处理过,通过fa并查集处理 a 和 b 找到 a ,b在fa并查集的祖先,就可以合并a和b所在段。
对于寻找哪个段最长的话,通过一个保存段长度的优先队列来查找
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <queue> using namespace std; const int M=2e5+44; struct Node
{
int id,num,li,ri,cnt;
friend bool operator<(Node x,Node y)
{
if(x.cnt==y.cnt) return x.id>y.id;
return x.cnt<y.cnt;
}
} p[M]; priority_queue<Node> que;
int n,fa[M],ma[M]; int fff(int rt)
{
if(fa[rt]==rt)
return rt;
return fa[rt]=fff(fa[rt]);
} int emm(int rt)
{
if(ma[rt]==rt)
return rt;
return ma[rt]=emm(ma[rt]);
} void merge(int a,int b,int flag)
{
if(a<1 || b>n)
return ;
int qa=emm(a),qb=emm(b);
int qqa=fff(qa),qqb=fff(qb);
if(p[qa].num!=p[qb].num || p[qqa].cnt==-1 || p[qqb].cnt==-1)
{
int xa=a+1,xb=b-1;
ma[xb]=a; ma[xa]=b;
return ;
}
int pa=qqa,pb=qqb;
fa[pb]=pa;
p[pa].cnt+=p[pb].cnt,p[pa].li=min(p[pa].li,p[pb].li),p[pa].ri=max(p[pa].ri,p[pb].ri);
p[pb].cnt=-1;
if(flag)
que.push(p[pa]);
} int main()
{
Node now;
int nowid,ans;
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d",&p[i].num);
p[i].id=i; p[i].li=p[i].ri=i; p[i].cnt=1;
fa[i]=i; ma[i]=i;
}
for(int i=2;i<=n;i++)
if(p[i].num==p[i-1].num)
merge(i-1,i,0);
while(!que.empty())
que.pop();
for(int i=1;i<=n;i++)
if(p[i].cnt!=-1)
que.push(p[i]);
ans=0;
while(!que.empty())
{
now=que.top(); que.pop();
nowid=now.id;
if(p[nowid].cnt!=now.cnt) continue;
// cout<<p[nowid].li<<' '<<p[nowid].ri<<' '<<p[nowid].num<<endl;
ans++; p[nowid].cnt=-1;
merge(now.li-1,now.ri+1,1);
}
printf("%d\n",ans);
return 0;
} /* 19
1 1 2 2 3 3 4 4 4 5 5 5 5 6 6 6 3 2 1 */
Codeforces Round #452 (Div. 2) 899E E. Segments Removal的更多相关文章
- Codeforces Round #452 (Div. 2) A B C
Codeforces Round #452 (Div. 2) A Splitting in Teams 题目链接: http://codeforces.com/contest/899/problem/ ...
- Codeforces Round #448 (Div. 2) B. XK Segments【二分搜索/排序/查找合法的数在哪些不同区间的区间数目】
B. XK Segments time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- Codeforces Round #452 (Div. 2)-899A.Splitting in Teams 899B.Months and Years 899C.Dividing the numbers(规律题)
A. Splitting in Teams time limit per test 1 second memory limit per test 256 megabytes input standar ...
- Codeforces Round #452 (Div. 2) C. Dividing the numbers(水)
C. Dividing the numbers Petya has n integers: 1, 2, 3, ..., n. He wants to split these integers in t ...
- Codeforces Round #452 (Div. 2)
第一次打..(太弱(+99积分是几个意思 A 题意:一堆数,只有1和2,问最多凑出多少个3. 分情况即可 #include<cstdio> int main(){ int a=0,b=0, ...
- 【Codeforces Round #455 (Div. 2) B】Segments
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 处理出所有的线 其实就是区间. 总共有n*(n+1)/2个 然后按照左端点.右端点排序 每次取最左边的线. 多种可能就取右端点尽量小 ...
- 【Codeforces Round #452 (Div. 2) A】 Splitting in Teams
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 贪心 1优先和2组队. 如果1没有了 就结束. 如果1还有多余的. 那么就自己3个3个组队 [代码] #include <bi ...
- 【Codeforces Round #452 (Div. 2) B】Months and Years
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 闰,平,平 平,闰,平 平,平,闰 平,平,平 4种情况都考虑到就好. 可能有重复的情况. 但是没关系啦. [代码] #includ ...
- 【Codeforces Round #452 (Div. 2) C】 Dividing the numbers
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] n为偶数. l = 1, r = n (l,r)放在一组 l++,r-- 新的l,r放在另外一组 直到l+1==r 这个时候,判断两 ...
随机推荐
- ORACLE initialization or shutdown in progress 错误解决办法
第一步,运行cmd 第一步.sqlplus /NOLOG第二步.SQL>connect sys/change_on_install as sysdba'/提示:已成功第三步.SQL>shu ...
- 级联-city
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title> ...
- day04_XPATH提取数据
1.XML简介 1.1.定义 可扩展标记语言(EXtensible Markup Language) 1.2.特点 一种标记语言,很类似 HTML XML 的标签需要我们自行定义 被设计为具有自我 ...
- 怎样设置Cookie
因为 Cookie 是服务器保存在浏览器中的一小段信息, 因此这个设置应当是服务器发起的, 设置方法是在Response Header中添加: Set-Cookie字段, 值是多个键值对. 如下: / ...
- 图解Java继承内存分配
图解Java继承内存分配 继承的基本概念: (1)Java不支持多继承,也就是说子类至多只能有一个父类. (2)子类继承了其父类中不是私有的成员变量和成员方法,作为自己的成员变量和方法. (3)子 ...
- Oracle 以及 PLSQL安装
今天重装系统遇到oracle 安装的问题咯 ,oracle安装过程中很多疑难杂症咯 1如果之前装过,记得去删除注册表的Oracle 相关的文件 ,请百度有很多教程咯 2这个必须要勾选的!因为的是11g ...
- 深入浅出GNU X86-64 汇编
深入浅出GNU X86-64 汇编 来源 https://blog.csdn.net/pro_technician/article/details/78173777 原文 https://www3.n ...
- linux - 卸载python
2019年10月15日12:05:42 [root@spider1 bin]# rpm -qa|grep python|xargs rpm -ev --allmatches --nodeps ##强制 ...
- ajax 请求二进制流 图片
<html xmlns="http://www.w3.org/1999/xhtml"><head runat="server"> ...
- css 对div用hover设置border,出现抖动和div走位问题,解决方法
样式设置 : div:hover { border:1px solid red;} 当鼠标移动到div时,产生抖动和偏移. 产生的原因: 是因为设置border时设置了1px边框,多出的这1px,与其 ...