牛客练习赛91 C(贪心+并查集跳跃经典应用 D(前缀和+树状数组处理二维偏序

本文通过两个实例展示了并查集和树状数组在解决复杂问题中的作用。第一个例子是魔法学院问题,利用并查集优化字符替换操作,减少重复遍历,实现高效求解。第二个案例是监狱逃亡问题,借助树状数组处理不等式求解符合条件的路径数量。这两个经典数据结构在解决实际问题中展现出了强大的能力。
摘要由CSDN通过智能技术生成

魔法学院(hard version)
思路:
题解
注意是一次操作是替换区间内某一个字符
经典的并查集应用
f [ i ] f[i] f[i] 表示 i i i 位置及其之后的第一个可以修改的位置
从高价值字符枚举到低价值字符,那么对于高价值字符覆盖过的区间,低价值字符就没有必要去遍历了。遍历时,用并查集对之前覆盖过的区间进行跳跃即可,因此每个字符至多被遍历一次
code:

#include<bits/stdc++.h>
#define endl '\n'
#define ll long long
#define ull unsigned long long
#define ld long double
#define all(x) x.begin(), x.end()
#define mem(x, d) memset(x, d, sizeof(x))
#define eps 1e-6
using namespace std;
const int maxn = 1e7 + 9;
const int mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
ll n, m;
int s[maxn], f[maxn];
struct node{
	int l, r, c;
	bool operator<(const node &B){
		return c > B.c;
	}
}d[maxn];
int find(int x){
	return f[x] == x ? x : f[x] = find(f[x]);
}
void merge(int x, int y){
	x = find(x); y = find(y);
	if(x != y){
		f[x] = y;
	}
}
void work()
{
	cin >> n >> m;
	for(int i = 1; i <= n + 1; ++i) f[i] = i;
	for(int i = 1; i <= n; ++i){
		char x;cin >> x;s[i] = (int)x;
	}
	for(int i = 1; i <= m; ++i){
		char c;
		cin >> d[i].l >> d[i].r >> c;
		d[i].c = int(c);
	}
	sort(d + 1, d + 1 + m);
	for(int i = 1; i <= m; ++i){
		int now = find(d[i].l);
		if(now > d[i].r) continue;
		while(now <= d[i].r){
			s[now] = max(s[now], d[i].c);
			merge(now, d[i].r + 1);// now之后第一个需要修改的位置是d[i].r+1
			now = find(now + 1);
		}	
	}
	ll ans = 0;
	for(int i = 1; i <= n; ++i) ans += s[i];
	cout << ans << endl;
}

int main()
{
	ios::sync_with_stdio(0);
//	int TT;cin>>TT;while(TT--)
	work();
	return 0;
}

监狱逃亡
题意:
给定一个 3 × n 3\times n 3×n 的矩阵,起点为 ( 1 , 1 ) (1,1) (1,1),终点为 ( 3 , n ) (3,n) (3,n),每个位置有一个价值 a i j a_{ij} aij,每次可以向右或者向下移动一格,求走到终点路径权值和 > = 0 >=0 >=0 的方案数,答案对 1 0 9 + 7 10^9+7 109+7 取模
思路:
矩阵只有 3 3 3 行,从第一行走到第三行,必然会有两次移动向下,设在第 i i i 列第一次向下,第 j j j 列第二次向下( i < = j i<=j i<=j
考虑维护三行的前缀和,终点价值即为 s u m 1 [ i ] + ( s u m 2 [ j ] − s u m 2 [ i − 1 ] + ( s u m 3 [ n ] − s u m 3 [ j − 1 ] ) sum1[i]+(sum2[j]-sum2[i-1]+(sum3[n]-sum3[j-1]) sum1[i]+(sum2[j]sum2[i1]+(sum3[n]sum3[j1])
满足不等式 s u m 1 [ i ] + ( s u m 2 [ j ] − s u m 2 [ i − 1 ] + ( s u m 3 [ n ] − s u m 3 [ j − 1 ] ) > = 0 sum1[i]+(sum2[j]-sum2[i-1]+(sum3[n]-sum3[j-1])>=0 sum1[i]+(sum2[j]sum2[i1]+(sum3[n]sum3[j1])>=0
分离 i , j i,j i,j
不等式变形为 s u m 2 [ i − 1 ] − s u m 1 [ i ] − s u m 3 [ n ] < = s u m 2 [ j ] − s u m 3 [ j − 1 ] ( i < = j ) sum2[i-1]-sum1[i]-sum3[n]<=sum2[j]-sum3[j-1](i<=j) sum2[i1]sum1[i]sum3[n]<=sum2[j]sum3[j1](i<=j)
不等式变形这步最好写成上边的式子,如果变为 s u m 3 [ j − 1 ] − s u m 2 [ j ] − s u m 3 [ n ] < = s u m 1 [ i ] − s u m 2 [ i − 1 ] ( i < = j ) sum3[j-1]-sum2[j]-sum3[n]<=sum1[i]-sum2[i-1](i<=j) sum3[j1]sum2[j]sum3[n]<=sum1[i]sum2[i1](i<=j), 这样类似于逆序对,不方便求解
将不等式两边的值离散化,用树状数组即可求解
code:

#include<bits/stdc++.h>
#define endl '\n'
#define ll long long
#define ull unsigned long long
#define ld long double
#define all(x) x.begin(), x.end()
#define mem(x, d) memset(x, d, sizeof(x))
#define eps 1e-6
using namespace std;
const int maxn = 2e6 + 9;
const int mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
ll n, m;
int c[maxn];
void update(int i, int k){
	while(i <= n * 2) c[i] += k, i += i & (-i);
}
int qry(int i){
	int ans = 0;while(i) ans += c[i], i -= i & (-i);return ans;
}
ll sum[4][maxn];

void work()
{
	cin >> n;
	for(int i = 1; i <= 3; ++i)
		for(int j = 1; j <= n; ++j)
		{
			cin >> sum[i][j];sum[i][j] += sum[i][j-1];
		}
	vector <ll> v;
	for(int i = 1; i <= n; ++i){
		v.push_back(sum[2][i] - sum[3][i-1]);
		v.push_back(sum[2][i-1] - sum[1][i] - sum[3][n]);
	}
	sort(all(v));
	v.erase(unique(all(v)), v.end());
	ll ans = 0;
	for(int i = 1; i <= n; ++i){
		int pos1 = lower_bound(all(v), sum[2][i-1] - sum[1][i] - sum[3][n]) - v.begin() + 1;
		update(pos1, 1);
		int pos2 = lower_bound(all(v), sum[2][i] - sum[3][i-1]) - v.begin() + 1;
		(ans += qry(pos2)) %= mod;
	}
	cout << ans;
}

int main()
{
	ios::sync_with_stdio(0);
//	int TT;cin>>TT;while(TT--)
	work();
	return 0;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值