Educational Codeforces Round 67 D. Subarray Sorting

Educational Codeforces Round 67 D. Subarray Sorting

传送门

题意;

给出两个数组\(a,b\),现在可以对\(a\)数组进行任意次排序,问最后能否得到\(b\)数组。
\(n\leq 3*10^5,a\leq n.\)

思路:

首先注意到任意次排序可以等价于任意次交换两个相邻的数,当且仅当前一个数不小于后面一个数。
我一开始想的是按权值从小到大来构造,但最终发现这条路走不通。
正解就是比较直接的思路,按位置一个一个来匹配。
对于一个\(b_i\),询问目前出现位置最早的\(a_j\),满足\(a_j=b_i\),当其能够移动过去,只要前面的数权值都不小于\(a_j\)即可。
因为我们匹配过后就要把\(a_j\)删去并且重新更新,所以不会出现\(j<i\)的情况。
以上权值线段树维护最小位置+vector存储位置即可解决。
代码如下:

#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int N = 3e5 + 5;
int T;
int n;
int a[N], b[N];
vector <int> p[N];
int Min[N << 2];
void build(int o, int l, int r) {
    Min[o] = INF;
    if(l == r) return ;
    int mid = (l + r) >> 1;
    build(o << 1, l, mid);
    build(o << 1|1, mid + 1, r);
}
void update(int o, int l, int r, int p, int v) {
    if(l == r) {
        Min[o] = v;
        return ;
    }
    int mid = (l + r) >> 1;
    if(p <= mid) update(o << 1, l, mid, p, v);
    else update(o << 1|1, mid + 1, r, p, v);
    Min[o] = min(Min[o << 1], Min[o << 1|1]);
}
int query(int o, int l, int r, int L, int R) {
    if(l >= L && r <= R) return Min[o];
    int ans = INF;
    int mid = (l + r) >> 1;
    if(L <= mid) ans = min(ans, query(o << 1, l, mid, L, R));
    if(R > mid) ans = min(ans, query(o << 1|1, mid + 1, r, L, R));
    return ans;
}
int main() {
    ios::sync_with_stdio(false); cin.tie(0);
    cin >> T;
    while(T--) {
        cin >> n;
        for(int i = 1; i <= n; i++) p[i].clear();
        for(int i = 1; i <= n; i++) {
            cin >> a[i];
            p[a[i]].push_back(i);
        }
        for(int i = 1; i <= n; i++) cin >> b[i];
        build(1, 1, n);
        for(int i = 1; i <= n; i++) if(!p[i].empty()) {
            reverse(p[i].begin(), p[i].end());
            update(1, 1, n, i, p[i].back());
        }
        bool ok = true;
        for(int i = 1; i <= n; i++) {
            if(p[b[i]].empty()) {
                ok = false;
                break;
            }
            if(query(1, 1, n, 1, b[i]) < p[b[i]].back()) {
                ok = false;
                break;
            }
            p[b[i]].pop_back();
            update(1, 1, n, b[i], (p[b[i]].empty() ? INF : p[b[i]].back()));
        }
        if(ok) cout << "YES" << '\n';
        else cout << "NO" << '\n';
    }
    return 0;
}

原文地址:https://www.cnblogs.com/heyuhhh/p/11313784.html

时间: 2024-08-30 05:16:57

Educational Codeforces Round 67 D. Subarray Sorting的相关文章

Educational Codeforces Round 35 E. Stack Sorting 模拟

Educational Codeforces Round 35 E. Stack Sorting 题意:长度为 n 的序列 a[] ,a[] 里的数是 1~n,一个空栈 s,一个空序列 b[].两个操作:把 a[] 的第一个数放到 s 里: 或者把 s 的栈顶元素加到 b[] 的末尾. 如果你能通过这两个操作把 a[] 的数最后都放入 b[] 中,且 b[] 是升序的,那就可说 a[] 是 stack-sortable . 现在给出 a[] 的前 k 个数,要你确定是否有满足 stack-sor

Codeforces Educational Codeforces Round 67

目录 Contest Info Solutions A. Stickers and Toys B. Letters Shop C. Vasya And Array D. Subarray Sorting E. Tree Painting Contest Info Data:2019.6.30 Solved:4/7 Solutions A. Stickers and Toys 题意: 有\(A\)物品\(s\)个,\(B\)物品\(t\)个,现在将这些物品装到\(n\)个箱子里,每个箱子只有一下三

Educational Codeforces Round 67 (Rated for Div. 2)

A 略 B 记录每种字母的出现次数前缀和,然后p[i][j]表示字母j出现至少i次的最靠前的位置,然后直接搜取最大即为答案,O(26(n+m)+Σ|ti|),差点想到二分去了,复杂度会多个log #include<bits/stdc++.h> using namespace std; const int N=2e5+7; int n,s[N][26],p[N][26],sum[26]; char str[N]; int main() { scanf("%d",&n)

Educational Codeforces Round 23 F. MEX Queries(线段树)

题目链接:Educational Codeforces Round 23 F. MEX Queries 题意: 一共有n个操作. 1.  将[l,r]区间的数标记为1. 2.  将[l,r]区间的数标记为0. 3.  将[l,r]区间取反. 对每个操作,输出标记为0的最小正整数. 题解: hash后,用线段树xjb标记一下就行了. 1 #include<bits/stdc++.h> 2 #define ls l,m,rt<<1 3 #define rs m+1,r,rt<&l

Educational Codeforces Round 21 F. Card Game(网络流之最大点权独立集)

题目链接:Educational Codeforces Round 21 F. Card Game 题意: 有n个卡片,每个卡片有三个值:p,c,l; 现在让你找一个最小的L,使得满足选出来的卡片l<=L,并且所有卡片的p的和不小于k. 选择卡片时有限制,任意两张卡片的c之和不能为质数. 题解: 和hdu 1565 方格取数(2)一样,都是求最大点权独立集. 不难看出来,这题再多一个二分. 注意的是在构造二部图的时候,按照c值的奇偶性构造. 当c==1时要单独处理,因为如果有多个c==1的卡片,

Educational Codeforces Round 36 (Rated for Div. 2)

Educational Codeforces Round 36 (Rated for Div. 2) F. Imbalance Value of a Tree You are given a tree T consisting of n vertices. A number is written on each vertex; the number written on vertex i is ai. Let's denote the function I(x,?y) as the differ

Educational Codeforces Round 69 (Rated for Div. 2) B - Pillars

Educational Codeforces Round 69 (Rated for Div. 2) B - Pillars There are n pillars aligned in a row and numbered from 1 to n. Initially each pillar contains exactly one disk. The i-th pillar contains a disk having radius ai. You can move these disks

Educational Codeforces Round 71 (Rated for Div. 2) D - Number Of Permutations

原文链接:https://www.cnblogs.com/xwl3109377858/p/11405773.html Educational Codeforces Round 71 (Rated for Div. 2) D - Number Of Permutations You are given a sequence of n pairs of integers: (a1,b1),(a2,b2),…,(an,bn). This sequence is called bad if it is

Educational Codeforces Round 79 D Santa&#39;s Bot

  被教育场 题意:先等概率选一个人,再从他想要礼物里等概率选一个,再独立于前两次选择,选一个人,他想要的礼物也被选中,则该组合有效,求组合有效的分数概率(模意义下) 玩一下两个样例应该就能出来知道咋算,虽然我第一个样例是跑了两重循环得出 7/8,拼凑起来才勉强理解的题意. 但知道咋算不一定会code啊. 我就是啊. 模拟了分数的加法乘法运算,通分约分,肯定要WA啊,因为到后面分子分母越来越大存不下. 但实际上,两个分数在某个模数意义下相加可以直接转化,即   x/y+u/v  == x*inv