Codeforces 1187D:Subarray Sorting

该博客讨论了一种编程题目,要求通过非递减排序子数组的方式来转换数组a到数组b。作者提出了利用线段树解决此问题的思路,并解释了如何通过线段树查找与目标值相等的元素下标,以及如何判断是否能实现数组转换。最后,给出了代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Subarray Sorting

You are given an array a1,a2,…,an and an array b1,b2,…,bn.
For one operation you can sort in non-decreasing order any subarray a[l…r] of the array aa.

For example, if a=[4,2,2,1,3,1] and you choose subbarray a[2…5], then the array turns into [4,1,2,2,3,1].

You are asked to determine whether it is possible to obtain the array b by applying this operation any number of times (possibly zero) to the array aa.

Input
The first line contains one integer t (1≤t≤3⋅105) — the number of queries.

The first line of each query contains one integer n (1≤n≤3⋅10^5).

The second line of each query contains n integers a1,a2,…,an (1≤ai≤n).

The third line of each query contains n integers b1,b2,…,bn (1≤bi≤n).

It is guaranteed that ∑n≤3⋅105 over all queries in a test.

Output
For each query print YES (in any letter case) if it is possible to obtain an array b and NO (in any letter case) otherwise.

Example

Input
4
7
1 7 1 4 4 5 6
1 1 4 4 5 7 6
5
1 1 3 3 5
1 1 3 3 5
2
1 1
1 2
3
1 2 3
3 2 1

Output
YES
YES
NO
NO

题意:就是让你只能通过对a数组的任意区间进行非递减排序,看能不能变成b数组。

思路:线段树问题,对数组a进行建树,然后对b数组遍历,从a数组中找到第一个与b数组值相等的下标k(因为数组比较大,不可能用两个for循环去找,所以要用队列来储存下标。然后只需到对应的队列去取值就行,如果为空,则说明a数组的值不全等于数组,直接break。),如果在区间<1,k>中数组a的最小值小于b当前的值,那么你无论如何变换都无法实现。如果大于或等于,那么就说明当前值为区间最小值,则可以通过变换将其换到b数组对应的位置,再将权值设为inf。

代码如下

#include <bits/stdc++.h>
#define inf 999999

using namespace std;

const int maxn = 3e5+5;
int num[maxn],num2[maxn],Q,n;
queue<int>q[maxn];
int G[maxn*4];

void buildtree(int l,int r,int t)
{
    if(l==r){
        G[t] = num[l];
        return ;
    }
    int mid  = (int)(l+r)/2;
    buildtree(l,mid,t*2);
    buildtree(mid+1,r,t*2+1);
    G[t] = min(G[t*2],G[t*2+1]);
    return ;
}

int query(int l,int r,int x,int y,int t)
{
    if(x<=l&&y>=r){
        return G[t];
    }
    int mid = (int)(l+r)/2,mi = inf;
    if(x<=mid)
        mi = min(mi,query(l,mid,x,y,t*2));
    if(y>mid)
        mi = min(mi,query(mid+1,r,x,y,t*2+1));
    return mi;
}

void update(int l,int r,int x,int t)
{
    if(l==x&&r==x){
        G[t] = inf;
        return ;
    }
    int mid = (int)(l+r)/2;
    if(x<=mid)
        update(l,mid,x,t*2);
    if(x>mid)
        update(mid+1,r,x,t*2+1);
    G[t] = min(G[t*2],G[t*2+1]);
    return ;

}

int main()
{
    scanf("%d",&Q);
    for(int i=0;i<Q;i++)
    {
        int a;
        scanf("%d",&n);
        for(int j=1;j<=n;j++){
            while(!q[j].empty())
                q[j].pop();
        }
        for(int j=1;j<=n;j++){
            scanf("%d",&num[j]);
            q[num[j]].push(j);
        }
        for(int j=1;j<=n;j++)
            scanf("%d",&num2[j]);
        buildtree(1,n,1);
        int flag = 0;
        for(int j=1;j<=n;j++)
        {
            if(q[num2[j]].empty()){
                flag=1;break;
            }
            int u = q[num2[j]].front();
            int v = query(1,n,1,u,1);
            q[num2[j]].pop();
            if(v<num2[j]){
                flag = 1;break;
            }
            update(1,n,u,1);
        }
        if(!flag) printf("YES\n");
        else printf("NO\n");
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值