首页 > 其他分享 >codeforces 1793D Moscow Gorillas

codeforces 1793D Moscow Gorillas

时间:2023-04-06 12:58:05浏览次数:47  
标签:tmp int Moscow mr codeforces ans 序列 Gorillas

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

解题思路

依次找出 MEX = 1..n+1的序列数量就能得解。

MEX = n+1 只有全序列这一种情况。

MEX = 1时,找出两个序列中1的位置,较小位置左边的元素构成的子序列,较大位置右边的元素构成的子序列,以及两个位置中间的元素构成的子序列都满足。

MEX = i (i∈2...n) 时,找出两个序列同时包含 1~i 的最短子序列,记为S。如果 i+1 在S中,则无解。否则,根据 i+1 在两个序列中的位置,计算可能的序列组合数。下一轮计算两个序列同时包含 1~i+1 的最短子序列的范围,可以根据S的范围以及i+1的位置进行推导。

/* https://codeforces.com/problemset/problem/1793/D */
#include <bits/stdc++.h>
using namespace std;

#define N 200001
int n, p[N], q[N];

int main() {
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

    while (cin >> n) {
        int tmp;
        for (int i = 1; i <= n; i++) {
            cin >> tmp;
            p[tmp] = i;
        }
        for (int i = 1; i <= n; i++) {
            cin >> tmp;
            q[tmp] = i;
        }

        int64_t ans = 1, l, r, prel, prer;
        prel = l = min(p[1], q[1]);
        prer = r = max(p[1], q[1]);
        ans += (l-1)*l/2;
        ans += (n-r+1)*(n-r)/2;
        ans += (r-l)*(r-l-1)/2;
        for (int i = 1; i < n; i++) {
            l = min(p[i], q[i]);
            r = max(p[i], q[i]);
            l = min(l, prel), r = max(r, prer);
            if ((l <= p[i+1] && p[i+1] <= r) || (l <= q[i+1] && q[i+1] <= r)) {
                prel = l, prer = r;
                continue;
            }
            int ml = 1, mr = n;
            int left = min(p[i+1], q[i+1]);
            int right = max(p[i+1], q[i+1]);
            ml = left+1 <= l ? left+1 : ml;
            ml = right+1 <= l ? right+1 : ml;
            mr = right-1 >= r ? right-1 : mr;
            mr = left-1 >= r ? left-1 : mr;
            int64_t nl = l - ml, nr = mr - r;
            // cout << "..." << i << " [" << l << "," << r << "] " << nl * nr + nl + nr + 1 << endl;
            ans += nl * nr + nl + nr + 1;
            prel = l, prer = r;
        }
        cout << ans << endl;
    }
    return 0;
}

 

标签:tmp,int,Moscow,mr,codeforces,ans,序列,Gorillas
From: https://www.cnblogs.com/fwjonair/p/17292410.html

相关文章

  • Codeforces Round 642 (Div3)
    K-periodicGarland给定一个长度位\(n\)的\(01\)串,每次操作可以将\(1\)变为\(0\)或者将\(0\)变为\(1\),现在你需要通过操作使得所有\(1\)之间的距离为\(k\),求最少的操作次数,注意全为\(0\)也算\(1<=n<=1e6,1<=k<=n\)\(dp\)/贪心:最大子段和思想方法一:\(dp\)\(O(n)\)状......
  • codeforces 1795E Explosions?
    https://codeforces.com/problemset/problem/1795/E解题思路问题的核心是要构造有一个先严格递增,然后严格递减的子序列。不在这个序列中的怪物单独击杀。先递增后递减可以看作是两个对称的问题,所以把递增序列的构造考虑清楚就可以了。假设已经知道将1~i-1构造成严格递增子序列所......
  • Codeforces Round 862 (Div. 2)
    CodeforcesRound862(Div.2)链接CodeforcesRound862(Div.2)A题#include<iostream>#include<algorithm>#include<cstdio>#include<cstring>#include<vector>#include<cstring>#include<unordered_set>#includ......
  • Codeforces Round 863 (Div. 3)
    CodeforcesRound863(Div.3)链接CodeforcesRound863(Div.3)A题遍历这个字符串,如果要插入的数第一次小于当前的数,就将数插入到这里,如果到最后都没有插入数,插入到最后#include<iostream>#include<algorithm>#include<cstdio>#include<cstring>#include<vec......
  • Codeforces Round 863 (Div. 3) E题
    题目地址题意:定义数组a包含所有不含数字4的正整数,给出一个n,要求求出数组a中第n个数Solution数位dp+二分,求出[1,mid]中不含数字4的正整数个数,不过因为有可能mid包含4,但是由于贡献是一样的,可以直接把4都变成3,最后处理一下即可intdp[20];inta[20];voidinit(){ dp[0]=1; f......
  • Codeforces Round 863 (Div. 3)
    A.InsertDigit放在第一个比他小的数前面#include<bits/stdc++.h>usingnamespacestd;voidsolve(){intn,d;cin>>n>>d;strings;cin>>s;for(chari:s){if(d>i-'0')cout<<d,d......
  • Codeforces Round 640 (Div. 4) ABCDEFG
    https://codeforces.com/contest/1352不知道怎么的复制过来的代码容易歪,观看效果可能不大好。这场古早div4,大题极其友好,除了E卡空间卡到我爆炸,别的都体验感极好。A.SumofRoundNumbers#include<bits/stdc++.h>usingnamespacestd;typedeflonglongLL;typedefpai......
  • Codeforces Round 863 (Div. 3) A-C 赛后思路复盘
    A(思维)思路:观察样例可知数越大放在前面越优。遍历字符串,判断当前位置的数字和要插入的数字的关系,如果要插入的数大于当前数,那么就插入到当前数的前面。string里有一个insert函数,可以把指定字符串插入到指定下标之前。在原串下标为pos的字符前插入字符串strbasic_string&insert......
  • codeforces round 862
    A.和洛谷上的删数思路一致,后者是找峰顶,这个是找谷底从前到后枚举每一位与要添加的数比大小,如果要添加的数<=该位的数,就继续枚举,否则就将这个数添加在其前面B.需要移动的步数=两个点所在的层数之差的绝对值,只要计算出所在层数就可以一开始没想明白怎么算这个层数,先把每个......
  • Codeforces Round 861 (Div. 2)
    Preface这场感觉都是一个礼拜前补题打的了,但由于上周末事情比较多都没来得及写题解因此可能题意都记得不是很清楚了,就简略地谈一谈吧A.LuckyNumbers不难想到直接暴力从左端点枚举到右端点并对每个数进行暴力判断一个很naive的结论就是当答案为\(9\)时直接输出即可,然后我们......