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的更多相关文章

  1. Codeforces Round #452 (Div. 2) A B C

    Codeforces Round #452 (Div. 2) A Splitting in Teams 题目链接: http://codeforces.com/contest/899/problem/ ...

  2. 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 ...

  3. 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 ...

  4. 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 ...

  5. Codeforces Round #452 (Div. 2)

    第一次打..(太弱(+99积分是几个意思 A 题意:一堆数,只有1和2,问最多凑出多少个3. 分情况即可 #include<cstdio> int main(){ int a=0,b=0, ...

  6. 【Codeforces Round #455 (Div. 2) B】Segments

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 处理出所有的线 其实就是区间. 总共有n*(n+1)/2个 然后按照左端点.右端点排序 每次取最左边的线. 多种可能就取右端点尽量小 ...

  7. 【Codeforces Round #452 (Div. 2) A】 Splitting in Teams

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 贪心 1优先和2组队. 如果1没有了 就结束. 如果1还有多余的. 那么就自己3个3个组队 [代码] #include <bi ...

  8. 【Codeforces Round #452 (Div. 2) B】Months and Years

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 闰,平,平 平,闰,平 平,平,闰 平,平,平 4种情况都考虑到就好. 可能有重复的情况. 但是没关系啦. [代码] #includ ...

  9. 【Codeforces Round #452 (Div. 2) C】 Dividing the numbers

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] n为偶数. l = 1, r = n (l,r)放在一组 l++,r-- 新的l,r放在另外一组 直到l+1==r 这个时候,判断两 ...

随机推荐

  1. 龙芯 飞腾 intel的 OpenBenchMarking数据

    1. 今天从openbenchmarking 里面进行了简单的查找. 数据主要为: 机器配置: LS3A3000的数据为: 来源: https://openbenchmarking.org/resul ...

  2. [转帖] 飞腾FT2000+ CPU的进展(2019.6)

    中国长城:拟进一步收购飞腾股权,强化信息基础设施国产化平台地位 2019-06-26 09:28 http://www.sohu.com/a/323065095_100016383 今年年中的事情 浪 ...

  3. 使用jbc查询数据封装成对象的工具类

    适用于获取Connection对象的util package com.briup.myDataSource; import java.io.FileReader; import java.io.Inp ...

  4. 复合模式MVC

    这里也只说一下简单的原理. Model:模型实现处理数据的切逻辑. View:视图呈现模型的数据和状态. Control:解读视图对模型的操作. 视图和模型之间使用观察者模式,只要模型的状态改变视图立 ...

  5. CentOS7安装配置redis5集群

    一.服务器准备 本文准备了3台服务器 , 分别是 172.18.0.231 172.18.0.232 172.18.0.233 每台运行2个redis实例, 端口分别为7000 7001 ,即总共6个 ...

  6. StoneTab标签页CAD插件 3.2.5

    //////////////////////////////////////////////////////////////////////////////////////////////////// ...

  7. ELinq学习一

    ELinq安装:在Nuget控制台中输入:install-package ELinq一.ELinq与DLinq和EF的功能差异 二.数据库对照表 三.CRUD操作1.插入(Insert)(1)简单形式 ...

  8. [NOIP10.5模拟赛]1.a题解--离散化+异或线段树

    题目链接: 咕咕咕 https://www.luogu.org/problemnew/show/CF817F 闲扯 在Yali经历几天折磨后信心摧残,T1数据结构裸题考场上连暴力都TM没打满 分析 观 ...

  9. luogu4677山区建小学题解--区间DP

    题目链接 https://www.luogu.org/problemnew/show/P4677 分析 这道题方法跟之前题不一样,我们相当于枚举一个左右端点来线性扩展,同时划分断点进行决策 \(f[i ...

  10. Java学习路径(抛光砖)

    这就是我刚刚在五孔问答中找到的Java学习路线图抛光砖价格.我个人认为,这条Java学习路线是可以的.它是2018年相对较新的Java学习路线,更符合企业就业标准. Java学习路径的第一阶段:Jav ...