当前位置:网站首页>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;
}
边栏推荐
猜你喜欢

Icassp 2022 | KS transformer for multimodal emotion recognition

【数据库学习】Redis 解析器&&单线程&&模型

Okaleido tiger NFT is about to log in to binance NFT platform, and the future market continues to be optimistic

Li Kou 1184. Distance between bus stops

IndexTree

VScode默认输出到调试控制台如何调整到终端以及两者中的乱码问题

AC自动机

Push information to wechat through enterprise wechat self built application

Monotonic stack structure exercise -- cumulative sum of minimum values of subarrays

Get the solution to the slow running speed of Mengxin Xiaobai computer! ٩ ( ‘ ω‘ )و get! ٩ ( ‘ ω‘ )و
随机推荐
Ranking of engineering project management software
Visual studio input! No prompt
Implementation of graph structure, from point to edge and then to graph
QT学习之VS创建QT项目显示未将对象引用设置到对象的实例
【1184. 公交站间的距离】
How static code analysis works
AVL tree of ordered table
Backgroundworker enables time-consuming operations without affecting interface response
How to adjust the default output of vscode to the debugging console to the terminal and the problem of garbled code in both
力扣 1184. 公交站间的距离
Oracle中实现对指定数据分组且获取重复次数
"Fundamentals of program design" Chapter 10 function and program structure 7-3 recursive realization of reverse order output integer (15 points)
Process / thread synchronization mechanism
Org.json Jsonexception: what about no value for value
DDos攻击分类
From violent recursion to dynamic programming, memory search
Monotonic stack structure exercise -- cumulative sum of minimum values of subarrays
Random sampling and thinning of PCL point cloud processing randomsample (60)
Poj2308 continuously look at dfs+bfs+ optimization
Boundary extraction of PCL point cloud processing (58)