首页 > 其他分享 >CF1793 Codeforces Round 852 (Div. 2) D. Moscow Gorillas

CF1793 Codeforces Round 852 (Div. 2) D. Moscow Gorillas

时间:2023-03-05 20:57:08浏览次数:55  
标签:ch CF1793 852 Moscow rmax cdots include lmin MEX

https://codeforces.com/contest/1793/problem/D

对于给定的两个长度为 \(n\) 的排列 \(a_i, b_i\),定义 \(MEX(S)\) 为集合 \(S\) 中没有出现的最小的正整数,找出所有满足 \(MEX(a_l, a_{l + 1}, \cdots, a_r) = MEX(b_l, b_{l + 1}, \cdots, b_r)\) 的区间 \([l, r]\) 的数量

记 \(MEX(a_l, a_{l + 1}, \cdots, a_r) = MEX(b_l, b_{l + 1}, \cdots, b_r) = M\),考虑枚举 \(M\),计算符合条件的 \([l, r]\) 的数量,那么我们就必须使得两个排列都包含 \([1, M - 1]\) 的所有数,且不能包含 \(M\),我们可以找到这样一个极小的区间满足上述条件,若 \(M\) 在这个区间的外面,那么我们可以直接得出这个区间可以扩展到多大,这样 \(l\) 和 \(r\) 都可以在一个区间内任意取值,并使得两个区间均不包含 \(M\),由于包含 \([1, M - 1]\) 的极小区间满足线性关系,于是这个过程可以用线性的复杂度处理

#include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <algorithm>
#include <cstring>
#include <functional>

#define i64 long long

const int mod = 998244353, inf = 0x3f3f3f3f;

inline int read() {
	bool sym = 0; int res = 0; char ch = getchar();
	while (!isdigit(ch)) sym |= (ch == '-'), ch = getchar();
	while (isdigit(ch)) res = (res << 3) + (res << 1) + (ch ^ 48), ch = getchar();
	return sym ? -res : res;
}

int main() {
	int n = read(), a[n + 1], b[n + 1];
	for (int i = 1; i <= n; i++) a[read()] = i;
	for (int i = 1; i <= n; i++) b[read()] = i;
	int t1 = std::min(a[1], b[1]), t2 = std::max(a[1], b[1]);
	i64 ans = 1ll;
    ans += 1ll * t1 * (t1 - 1) / 2ll;
    ans += 1ll * (t2 - t1) * (t2 - t1 - 1) / 2ll;
    ans += 1ll * (n - t2 + 1) * (n - t2) / 2ll;
	int la = a[1], ra = a[1], lb = b[1], rb = b[1];
	for (int i = 2; i <= n; i++) {
		int lmin = std::min(la, lb), rmax = std::max(ra, rb);
		la = std::min(la, a[i]); ra = std::max(ra, a[i]);
		lb = std::min(lb, b[i]); rb = std::max(rb, b[i]);
		if (lmin <= a[i] && a[i] <= rmax || lmin <= b[i] && b[i] <= rmax) continue;
		if (std::max(a[i], b[i]) < lmin) ans += 1ll * (lmin - std::max(a[i], b[i])) * (n - rmax + 1);
		if (std::min(a[i], b[i]) > rmax) ans += 1ll * (std::min(a[i], b[i]) - rmax) * lmin;
		if (a[i] < lmin && b[i] > rmax) ans += 1ll * (lmin - a[i]) * (b[i] - rmax);
		if (b[i] < lmin && a[i] > rmax) ans += 1ll * (lmin - b[i]) * (a[i] - rmax);
	}
	printf("%lld", ans);
	return 0;
}

标签:ch,CF1793,852,Moscow,rmax,cdots,include,lmin,MEX
From: https://www.cnblogs.com/zrzring/p/CF1793D.html

相关文章

  • AcWing852 -- bug
    1.题目描述spfa判断负环2.bug原因因为是个模板题,所以我做的比较轻松,就用数组模拟队列,但因为spfa入队出队十分频繁,因此需要用循环队列,我是这么模拟循环队列的:in......
  • 850~852 绑定事件 标准方式、on&off、事件切换
    3.事件绑定1、JQuery标准的绑定方式jq对象.事件方法(回调函数);注:如果调用事件方法,不传递回调函数,则会触发浏览器默认行为。表......
  • CF1793E Velepin and Marketing
    个人思路:从小到大排序,因为一定先满足小的,再满足大的。分组时,我们发现,同一组内的数在排序后的序列内连续,这样更优。因为(不会证)。我们预处理出对于每个出书数量的答案,查询......
  • Codeforces Round #852 (Div. 2) C. Dora and Search
    https://codeforces.com/contest/1793/problem/C  我们考虑进行构造。不难发现,对于一个序列,如果它的左端点不是整个序列的最大值,那么无论在序列的右边加怎么样的值,它......
  • Codeforces Round #852 (Div. 2) D - Moscow Gorillas
    https://codeforces.com/contest/1793/problem/D不妨枚举MEX(...)的值x。此时对于序列[l,r],需要满足:两个序列的1到x-1都在这个区间内,并且x都不在这个区间内......
  • D. Moscow Gorillas
    D.MoscowGorillasInwinter,theinhabitantsoftheMoscowZooareverybored,inparticular,itconcernsgorillas.Youdecidedtoentertainthemandbrought......
  • Codeforces Round #852 (Div. 2)(C,D)
    CodeforcesRound#852(Div.2)(C,D)B这个题大意是给你一个\(x\),\(y\),\(x\)是极大值(\(a_i>a_{i+1},a_i>a_{i+1}\))的和,\(y\)是极小值(\(a_i<a_{i+1},a_i<a_{i+1}\))的......
  • Codeforces Round #852 (Div. 2)
    F.Rebrending题目抽象为现有长度为\(n\)的数组\(a_1,a_2,\cdots,a_n\),\(m\)次询问,每次询问\(\min\limits_{l\lei,j\ler,i\neqj}|a_i-a_j|\)\((1\lel<r\len)......
  • Codeforces Round #852 (Div. 2)
    A.YetAnotherPromotion题意:买土豆,一种卖a元一公斤,买m公斤送一公斤;一种卖b元一公斤。求买n公斤土豆最少花多少钱。解:完全没有思考,把a*n,b*n,买尽可能多m倍数的土豆剩......
  • BZOJ 1852 [MexicoOI06] 最长不下降序列
    https://darkbzoj.cc/problem/1852首先解决初始排序的问题:先把\(i,j\)对应的两组数\((a_i,b_i),(a_j,b_j)\)分为“必要”,“非必要”两类。“必要”,指\(i\)必须......