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

Codeforces Round #852 (Div. 2) D - Moscow Gorillas

时间:2023-02-15 10:59:42浏览次数:40  
标签:rr 852 int Moscow Codeforces cnt1 ans 端点 ll

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

不妨枚举 MEX(...) 的值 x。此时对于序列 [l, r],需要满足:两个序列的 1 到 x - 1 都在这个区间内,并且 x 都不在这个区间内。

对于第一个条件,我们可以按照顺序处理两个序列的 1 到 x - 1 的最左边位置和最右边位置,显然可以在枚举的过程中 O(1) 处理。我们可以对 x = 1 的情况进行特殊处理,在此之后,这个区间就必须要包含至少一个值,假设此时左端点要落在 [1, L] 内,右端点要落在 [R, n] 内,那么 L <= R 成立。

接下来考虑第二个要求:不能包括两个位置。我们对其中一个位置 p 进行分析:

1) L <= p <= R,此时序列一定会包含 p,方案数为 0。

2) p < L,此时左端点不能到 p 的左边,那么需要将左端点更新为 [p + 1, L]。 p > R 同理。

最后只要将左右端点的区间长度相乘就是 MEX(...) 为 x 时的答案了。

转自 :作者:tiger_2005 https://www.bilibili.com/read/cv21786337 出处:bilibili   侵删

 

int A[200010], B[200010], N;
int idxA[200010], idxB[200010];
long long cnt1 (int p) {
    return 1ll * p * (p + 1) / 2;
}
int main(){
    cin >> N;
    readI(1, N, A);
    readI(1, N, B);
    long long ans = 1; // whole array
    for (int i = 1; i <= N; i ++) {
        idxA[A[i]] = i;
        idxB[B[i]] = i;
    }
    int L = N, R = 1;
    for (int i = 1; i <= N; i ++) {
        int p = idxA[i], q = idxB[i];
        if (p > q)
            swap(p, q);
        if (i == 1) {
            if (p == q)
                ans += cnt1(p - 1) + cnt1(N - p);
            else
                ans += cnt1(p - 1) + cnt1 (q - p - 1) + cnt1(N - q);
        }
        else {
            L = min(L, idxA[i-1]);
            R = max(R, idxA[i-1]);
            L = min(L, idxB[i-1]);
            R = max(R, idxB[i-1]);
            // consider l in [1, L] ans r in [R, N]
            // printf("- %d %d %d\n", i, L, R);
            int lr = L, rl = R;
            int ll = 1, rr = N;
            if ((L <= p && p <= R) || (L <= q && q <= R))
                continue;
            if (p < L)
                ll = max(ll, p + 1);
            if (p > R)
                rr = min(rr, p - 1);
            if (q < L)
                ll = max(ll, q + 1);
            if (q > R)
                rr = min(rr, q - 1);
            ans += 1ll * (lr - ll + 1) * (rr - rl + 1);
            // printf("* [%d %d] [%d %d]\n", ll, lr, rl, rr);
        }
    }
    printf("%lld\n", ans);
    return 0;
} 

 

标签:rr,852,int,Moscow,Codeforces,cnt1,ans,端点,ll
From: https://www.cnblogs.com/acmLLF/p/17121957.html

相关文章

  • CF1735C_Codeforces Round #824 (Div. 2)C
    题意:现有一个26个小写字母形成的环,将一个字符串加密,加密规则为将字母变换为环中该字母顺时针的下一个字母现给出加密后的字符串,要求解密出字典序最小的字符串分析:对于......
  • C、Grid game 【 Codeforces Round #534 (Div. 2) 】
    C、Gridgame题意:给你一个4×4的方格,然后给你一些1×2或者2×1的小方格,当这些方格在一行或者一列的时候会消除掉,问最佳放置位置。思路:QAQ,什么时候思维会变强......
  • Codeforces Round #442 (Div. 2)E. Danil and a Part-time Job 线段树+lazytag
    题意:一颗有根树,树上每一个节点有一个灯,要支持两种操作第一种操作是统计一颗子树内开着的灯个数。第二种操作是将一个子树内的所有灯状态改变(开灯->关灯,关灯->开灯)。解......
  • Codeforces Round #397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 c
    FSouvenirs将询问离线,对原数组离散化,然后用权值线段树维护区间对应权值最大值,\(a_i\)的权值为\(i\),再用树状数组维护区间两数绝对值差的最小值。复杂度为\(\mathcal{......
  • 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)......
  • #0033. Educational Codeforces Round 3
    609A贪心优先选大的USBflashdrive609B先处理每个genre的书有多少本,然后直接枚举每次购买的是哪两种genre然后乘法原理即刻609C手下考虑balanced的长啥样。假设这n......
  • #0032. Educational Codeforces Round 2
    600A简单题但有个坑点在于会有空字符串600B一道可以用来实验upper_bound的题600C挺有趣的一道题。首先考虑怎样的字符串可以通过permutation变成palindrome:条件是至......
  • Codeforces Round #852 (Div. 2)
    A.YetAnotherPromotion题意:买土豆,一种卖a元一公斤,买m公斤送一公斤;一种卖b元一公斤。求买n公斤土豆最少花多少钱。解:完全没有思考,把a*n,b*n,买尽可能多m倍数的土豆剩......