【洛谷】P8686 [蓝桥杯 2019 省 A] 修改数组 的题解

本文介绍了如何使用并查集解决蓝桥杯竞赛中的一道数组修改题目。通过构建树结构并进行路径压缩,优化查找和修改过程,实现高效算法。在读入数据时,利用并查集找到连续数字的最大值进行修改,同时处理可能的树合并操作。
摘要由CSDN通过智能技术生成

【洛谷】P8686 [蓝桥杯 2019 省 A] 修改数组 的题解

题目传送门

思路

数据量还是蛮大的,直接暴力 1 1 1 秒跑不完,主要的时间都花费在读入一个数,查找没有出现过的第一个数上,考虑利用并查集优化这个过程。

将出现过的连续数字都构造到一棵树里,然后将每一个数的父节点直接标记为与它连续的且前面出现过的最大数。

那么在之后读入数据的时候如果这个数已经出现过,就可以直接找它的父节点,将这个数修改为父节点的数字加一输出。

但是两个问题:

  1. 树不是直接就构造成这样的,这涉及到了并查集的路径压缩。

  2. 在读入元素修改过后,还要判断修改数的前后数字是否出现过,也就是看树能不能合并。

思路

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cctype>
#include <climits>
#include <algorithm>
#include <map>
#include <queue>
#include <vector>
#include <ctime>
#include <string>
#include <cstring>
#define lowbit(x) x & (-x)
#define endl "\n"
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
namespace fastIO {
	inline int read() {
		register int x = 0, f = 1;
		register char c = getchar();
		while (c < '0' || c > '9') {
			if(c == '-') f = -1;
			c = getchar();
		}
		while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
		return x * f;
	}
	inline void write(int x) {
		if(x < 0) putchar('-'), x = -x;
		if(x > 9) write(x / 10);
		putchar(x % 10 + '0');
		return;
	}
}
using namespace fastIO;
int fa[1000005];
bool vis[1000005];
int find(int x) {
	return x == fa[x] ? x : fa[x] = find(fa[x]);
}
int main() {
	//freopen(".in","r",stdin);
    //freopen(".out","w",stdout);
	ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
	memset(vis,false, sizeof(vis));
	for(int i = 1; i <= 1000000; i++) fa[i] = i;
	int n, x;
	cin >> n;
	for(int i = 1; i <= n; i ++) {
    	cin >> x;
    	if(vis[x]) x = find(x) + 1;
    	cout << x << " ";
    	vis[x] = true;
    	if(vis[x - 1] && x != 1) fa[x - 1] = x;
		if(vis[x + 1]) fa[x] = x + 1;
	}
	return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
题目描述 如果一个数的十进制表示法中,任何相邻的两个数字均不相同,那么这个数就被称为 “有趣的数”。例如,以下各数都是有趣的数: 1、21、321、4321 但是,以下各数却不是有趣的数: 22、121、3212 给出正整数 n,请你求出长度为 n 的有趣的数的个数,并输出这个值除以 998244353 的余数。 输入格式 输入一行,包含一个整数 n。 输出格式 输出一个整数,表示答案除以 998244353 的余数。 数据范围 1≤n≤1000 输入样例1: 2 输出样例1: 81 输入样例2: 3 输出样例2: 531441 题解 有趣的数 求长度为n的有趣数的个数 思路: 第一位有9种选择,第二位有9种选择,第三位有8种选择(因为只有和前一位不一样才可以) 代码1: 时间复杂度 O(n) C++ 代码 #include <iostream> #include <cstdio> using namespace std; int main() { int n; scanf("%d", &n); int mod = 998244353; long long res = 1; for (int i = 1; i <= n; i ++ ) res = res * (i <= 2 ? 9 : 10 - i % 2) % mod; printf("%lld\n", res); return 0; } 代码2: 时间复杂度 O(n) C++ 代码 #include <iostream> #include <cstdio> using namespace std; const int MOD = 998244353; int main() { int n; scanf("%d", &n); int f[1010][10]; for (int i = 0; i < 10; i ++ ) f[1][i] = 1; for (int i = 2; i <= n; i ++ ) for (int j = 0; j < 10; j ++ ) for (int k = 0; k < 10; k ++ ) if (j != k) f[i][j] = (f[i][j] + f[i - 1][k]) % MOD; int res = 0; for (int i = 1; i < 10; i ++ ) res = (res + f[n][i]) % MOD; printf("%d\n", res); return 0; }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值