B

B - WeirdSort

 

 思路:经过认真的审题,你会发现,这只是个冒泡的变形,我们建立两个数组,然后用一个数组里面的数字确定位置,然后冒泡就行了。最后抖机灵用了个is_sorted,判断数组里面数字的排列顺序。

代码:

#include<iostream>
#include<algorithm>
#include<string>
#include<cmath>
using namespace std;

int a[105], b[105];


int main(){
    int i, j;
    int t;
    scanf("%d", &t);
    while (t--){
        int n, m;
        scanf("%d%d", &n, &m);
        for (i = 0; i < n; i++)
            scanf("%d", &a[i]);
        for (i = 0; i < m; i++)
            scanf("%d", &b[i]);
        for (i = 0; i < n; i++){
            for (j = 0; j < m; j++){
                if (a[b[j]-1]>a[b[j]])
                    swap(a[b[j]-1], a[b[j]]);
            }
        }
        if (is_sorted(a , a + n))
            printf("YES
");

        else
            printf("NO
");
    }

    return 0;
}
原文地址:https://www.cnblogs.com/pcdl/p/12489577.html