luogu P2391 白雪皑皑 (并查集 思维)

简介: luogu P2391 白雪皑皑 (并查集 思维)

原题链接

题意:

20200401134307494.png

思路:

由于颜色是可覆盖的,考虑倒着维护颜色。

用r o o t i

表示i ii前面第一个没被染色的雪花的位置。

由于是倒序枚举,所以对于每次染色,都暴力进行修改,找r o o t i 大概是并查集维护序列连通性

每个点最多被修改一次,时间复杂度O ( n )

代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll>PLL;
typedef pair<int, int>PII;
typedef pair<double, double>PDD;
#define I_int ll
inline ll read(){ll x = 0, f = 1;char ch = getchar();while(ch < '0' || ch > '9'){if(ch == '-')f = -1;ch = getchar();}while(ch >= '0' && ch <= '9'){x = x * 10 + ch - '0';ch = getchar();}return x * f;}
inline void write(ll x){if (x < 0) x = ~x + 1, putchar('-');if (x > 9) write(x / 10);putchar(x % 10 + '0');}
#define read read()
#define closeSync ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define multiCase int T;cin>>T;for(int t=1;t<=T;t++)
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define repp(i,a,b) for(int i=(a);i<(b);i++)
#define per(i,a,b) for(int i=(a);i>=(b);i--)
#define perr(i,a,b) for(int i=(a);i>(b);i--)
ll ksm(ll a, ll b,ll mod){ll res = 1;while(b){if(b&1)res=res*a%mod;a=a*a%mod;b>>=1;}return res;}
const int maxn=1e6+7,inf=0x3f3f3f3f;
const double eps=1e-5;
int root[maxn],col[maxn];
int Find(int x){
  if(x!=root[x]) root[x]=Find(root[x]);
  return root[x];
}
int main(){
  int n=read,m=read,p=read,q=read;
  rep(i,1,n) root[i]=i;
  for(int i=m;i;i--){
    int l=(i*p+q)%n+1,r=(i*q+p)%n+1;
    if(l>r) swap(l,r);
    for(int j=r;j>=l;j=root[j]){
      int t=Find(j);
      if(t==j) col[j]=i,root[j]=Find(j-1);
    }
  }
  rep(i,1,n) printf("%d\n",col[i]);
    return 0;
}
目录
相关文章
|
4月前
|
算法 C++
一题带你写出图论算法模板!!!
一题带你写出图论算法模板!!!
|
6月前
|
机器学习/深度学习 算法
蓝桥杯丨回溯法
蓝桥杯丨回溯法
30 0
|
7月前
|
算法
每日刷题|回溯法解决组合问题
每日刷题|回溯法解决组合问题
|
算法 JavaScript 前端开发
日拱算法:解两道“杨辉三角”题
什么是“杨辉三角”,想必大家并不陌生~~ 在「杨辉三角」中,每个数是它左上方和右上方的数的和。
|
算法
【每日挠头算法题】Acwing 756. 蛇形矩阵 —— 巧妙解法
【每日挠头算法题】Acwing 756. 蛇形矩阵 —— 巧妙解法
77 0
【每日挠头算法题】Acwing 756. 蛇形矩阵 —— 巧妙解法
|
机器学习/深度学习 算法
<<算法很美>>——(六)——回溯算法(下)—N皇后问题
<<算法很美>>——(六)——回溯算法(下)—N皇后问题
<<算法很美>>——(六)——回溯算法(下)—N皇后问题
|
C++
这道「岛屿题」用并查集做就体现出其威力了!
之前的岛屿题,用 DFS 和 BFS 来做要比用并查集更加好做并且高效,但是最对这一道题来说,用并查集要更加好做。
64 0
牛客-加边的无向图(思维+并查集)
牛客-加边的无向图(思维+并查集)
51 0
|
机器学习/深度学习 C++
【力扣·每日一题】1036. 逃离大迷宫 (C++ bfs 思维)
【力扣·每日一题】1036. 逃离大迷宫 (C++ bfs 思维)
73 0
【力扣·每日一题】1036. 逃离大迷宫 (C++ bfs 思维)
Codeforces 796D. Police Stations (思维+bfs)
Codeforces 796D. Police Stations (思维+bfs)
79 0