当前位置:网站首页>P3201 [hnoi2009] dream pudding heuristic merge
P3201 [hnoi2009] dream pudding heuristic merge
2022-07-24 22:41:00 【Snow_ raw】
P3201 [HNOI2009] Dream pudding Heuristic merging
Ideas : First initialize the total number of segments a n s ans ans, use c o l o r [ i ] ! = c o l o r [ i − 1 ] color[i]!=color[i-1] color[i]!=color[i−1] Just count . Then we use Linked list This data structure stores the subscripts of each color in the linked list of that color , After processing, each color, that is, each linked list stores the subscript of the color . Then we use heuristic merging , because n n n The maximum is 2 e 5 2e5 2e5 So the worst complexity is O ( n l o g n ) O(nlogn) O(nlogn) Acceptable , The core of heuristic merging is that each operation will s i z e size size Small sets merge into s i z e size size Large set , But there is a problem that needs special treatment , For example, if x x x Color becomes y y y Color , but x x x Of s i z e size size Greater than y y y Of s i z e size size So how do we deal with it ? We can use one p p p Array to store color mapping , If appear x x x Of s i z e size size Greater than y y y Of s i z e size size The situation of , We just need s w a p ( p [ x ] , p [ y ] ) swap(p[x],p[y]) swap(p[x],p[y]) that will do . It means that from now on, we think the original x x x Color is y y y Color , The original y y y Color is x x x Color .
Code :
/* * @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){
// Real time updates should be referenced
if(x==y)return ;//x and y May be equal
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;
}
边栏推荐
- Glidemodule appglidemodule and generated API details
- Helm -- a powerful package management tool for kubernetes applications
- Ranking of engineering project management software
- Gee - dataset introduction mcd12q1
- NVIDA-TensorRT部署(一)
- Gradle learning set integration
- 解决JSP无法使用session.getAttribute()
- Application programming of communication heartbeat signal for communication abnormality judgment
- 有序表之AVL树
- Uniform sampling and thinning of PCL point cloud processing (61)
猜你喜欢

Talk about how redis handles requests
![洛谷 P2024 [NOI2001] 食物链](/img/7f/6ccbc19942f0d4a153025346496834.png)
洛谷 P2024 [NOI2001] 食物链

Background image and QR code synthesis

Outlook邮件创建的规则失效,可能的原因

The specified data is grouped and the number of repetitions is obtained in Oracle

Alibaba cloud SSL certificate

Gee - dataset introduction mcd12q1

"Yuan universe 2086" outsold "San ti" in one-day sales and won the champion of JD books' one-day science fiction list
![[1184. Distance between bus stops]](/img/dd/3437e6a14ac02dac01c78b372a5498.png)
[1184. Distance between bus stops]

IndexTree2D
随机推荐
SQL语言的通用语法及分类(二)
General syntax and classification of SQL language (II)
Gee - dataset introduction mcd12q1
Let‘s Encrypt
From Fibonacci sequence to matrix fast power technique
《元宇宙2086》单日销量超《三体》 夺得京东图书单日科幻榜冠军
One click compilation and installation of redis6.2.4
Connector in C
Icassp 2022 | KS transformer for multimodal emotion recognition
TrinityCore魔兽世界服务器-注册网站
IP first experiment hdcl encapsulates PPP, chap, mGRE
Things to study
Using FRP to achieve intranet penetration
The size of STM32 stack
About the word 'don't remember'
Some analysis of slow MySQL query
Multi task face attribute analysis based on deep learning (based on paddlepaddle)
WPF opens external programs and activates them when needed
AC自动机
Read and understand the advantages of the LAAS scheme of elephant swap