当前位置:网站首页>P3201 [HNOI2009] 梦幻布丁 启发式合并
P3201 [HNOI2009] 梦幻布丁 启发式合并
2022-07-24 22:13:00 【Snow_raw】
P3201 [HNOI2009] 梦幻布丁 启发式合并
思路: 先初始化总段数 a n s ans ans,用 c o l o r [ i ] ! = c o l o r [ i − 1 ] color[i]!=color[i-1] color[i]!=color[i−1]统计即可。然后我们采用 链表 这种数据结构将每种颜色出现过的下标存在该颜色的链表之中,处理完后每个颜色即每条链表存的都是该颜色出现过的下标。之后我们采用启发式合并的操作,因为 n n n 最大为 2 e 5 2e5 2e5 所以复杂度最坏是 O ( n l o g n ) O(nlogn) O(nlogn)可以接受,启发式合并的核心即每次操作将 s i z e size size 小的集合合并到 s i z e size size 大的集合,但是本题有个问题需要特殊处理,例如如果将 x x x 颜色变成 y y y 颜色,但 x x x 的 s i z e size size 大于 y y y 的 s i z e size size 那我们该如何处理呢?我们可以用一个 p p p 数组来存颜色的映射,如果出现 x x x 的 s i z e size size 大于 y y y 的 s i z e size size 的情况,我们只需要 s w a p ( p [ x ] , p [ y ] ) swap(p[x],p[y]) swap(p[x],p[y]) 即可。意思是从此刻开始我们认为原本的 x x x 颜色就是 y y y 颜色,原本的 y y y 颜色就是 x x x 颜色。
代码:
/* * @author: Snow * @Description: Algorithm Contest * @LastEditTime: 2022-07-22 16:22:40 */
#include<bits/stdc++.h>
using namespace std;
#define int long long
#pragma GCC optimize(3)
typedef pair<int,int>PII;
#define pb emplace_back
int n,m;
const int N = 1e5+10;
const int M = 1e6+10;
int color[M];
int h[M],ne[N],e[N],idx;
int p[M];
int ans;
int sz[M];
void add(int a,int b){
e[idx]=b;
ne[idx]=h[a];
h[a]=idx++;
}
void merge(int &x,int &y){
//实时更新要加引用
if(x==y)return ;//x和y可能相等
if(sz[x]>sz[y])swap(x,y);
for(int i=h[x];i!=-1;i=ne[i]){
int j=e[i];
ans-=(color[j-1]==y)+(color[j+1]==y);
}
for(int i=h[x];i!=-1;i=ne[i]){
int j=e[i];
color[j]=y;
if(ne[i]==-1){
ne[i]=h[y];
h[y]=h[x];
break;
}
}
h[x]=-1;
sz[y]+=sz[x];
sz[x]=0;
}
signed main(){
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
cin>>n>>m;
memset(h,-1,sizeof h);
for(int i=1;i<=n;i++){
cin>>color[i];
if(color[i]!=color[i-1])ans++;
sz[color[i]]++;
add(color[i],i);
}
for(int i=1;i<M;i++)p[i]=i;
while(m--){
int op;
cin>>op;
if(op==1){
int x,y;
cin>>x>>y;
merge(p[x],p[y]);
}
else cout<<ans<<endl;
}
return 0;
}
边栏推荐
- C # use SQLite
- On the open and closed principle
- Apipost signs up with Chinatelecom! Work together to accelerate the digital transformation of enterprises
- 通过企业微信自建应用向微信推送信息
- RichTextBox operation
- 阿里云SSL证书
- IP first experiment hdcl encapsulates PPP, chap, mGRE
- 基于深度学习的多任务人脸属性分析(基于飞桨PaddlePaddle)
- 单调栈结构练习——子数组最小值的累加和
- 百度网盘+Chrom插件
猜你喜欢

Local data enhancement method of icml2022 | graph neural network

IP first experiment hdcl encapsulates PPP, chap, mGRE

Glidemodule appglidemodule and generated API details

Visual studio input! No prompt

Gee - dataset introduction mcd12q1

From violent recursion to dynamic programming, memory search

【ICML2022】气候变化与机器学习:机遇、挑战与考虑,121页ppt

Use kettle to read the data in Excel file and store it in MySQL

由斐波那契数列引述到矩阵快速幂技巧

Violent recursion - detailed explanation of Queen n & how to optimize with bit operation
随机推荐
ODBC executes stored procedure to get return value
Flex layout
PCL点云处理之创建二维格网组织点云数据(六十四)
The kettle job implementation runs a kettle conversion task every 6S
GlideModule AppGlideModule和Generated API详解
TrinityCore魔兽世界服务器-注册网站
PCL点云处理之pcd文件转txt文件(单个或多个批量转换)(六十三)
VC prompts to recompile every time you press F5 to run
单调栈结构练习——子数组最小值的累加和
头脑风暴之——利用reduce方法重构concat函数
Morris traversal
On the open and closed principle
ASP. Net core 6.0 data validation based on model validation
Boundary extraction of PCL point cloud processing (58)
AC自动机
Use kettle to read the data in Excel file and store it in MySQL
[cloud native kubernetes] kubernetes cluster advanced resource object staterulesets
Website resources
Process / thread synchronization mechanism
Glidemodule appglidemodule and generated API details