小组练习第一周---H

You are given an array aa of length n.
You are also given a set of distinct positions p1,p2,…,pmp1,p2,…,pm, where 1≤pi<n1≤pi<n. The position pipi means that you can swap elements a[pi]a[pi] and a[pi+1]a[pi+1]. You can apply this operation any number of times for each of the given positions.
Your task is to determine if it is possible to sort the initial array in non-decreasing order (a1≤a2≤⋯≤ana1≤a2≤⋯≤an) using only allowed swaps.
For example, if a=[3,2,1]a=[3,2,1] and p=[1,2]p=[1,2], then we can first swap elements a[2]a[2] and a[3]a[3] (because position 22 is contained in the given set pp). We get the array a=[3,1,2]a=[3,1,2]. Then we swap a[1]a[1] and a[2]a[2] (position 11 is also contained in pp). We get the array a=[1,3,2]a=[1,3,2]. Finally, we swap a[2]a[2] and a[3]a[3] again and get the array a=[1,2,3]a=[1,2,3], sorted in non-decreasing order.
You can see that if a=[4,1,2,3]a=[4,1,2,3] and p=[3,2]p=[3,2] then you cannot sort the array.
You have to answer tt independent test cases.

Input:

The first line of the input contains one integer tt (1≤t≤1001≤t≤100) — the number of test cases.
Then tt test cases follow. The first line of each test case contains two integers nn and mm (1≤m<n≤1001≤m<n≤100) — the number of elements in aa and the number of elements in pp. The second line of the test case contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤1001≤ai≤100). The third line of the test case contains mm integers p1,p2,…,pmp1,p2,…,pm (1≤pi<n1≤pi<n, all pipiare distinct) — the set of positions described in the problem statement.

Output:

For each test case, print the answer — “YES” (without quotes) if you can sort the initial array in non-decreasing order (a1≤a2≤⋯≤ana1≤a2≤⋯≤an) using only allowed swaps. Otherwise, print “NO”.
input:
6
3 2
3 2 1
1 2
4 2
4 1 2 3
3 2
5 1
1 2 3 4 5
1
4 2
2 1 4 3
1 3
4 2
4 3 2 1
1 3
5 2
2 1 2 3 3
1 4

Output:

YES
NO
YES
YES
NO
YES
感觉该题还比较麻烦,做题花费了大量的时间,但感觉思路还不是特别的清晰
题意:该题在样例中第一行的两个数字,第二个数字代表可以移动的下标,要求我们能否将该数组排好序。可移动的数组的元素只能与i+1的元素交换位置
思路:先将原来的数组排序,得到原数组和排序后的数组,通过两个Boolean的数组对两个数组中元素的使用进行记录,防止同一个数据使用两次,接下来找到两个数组中相等的元素,查看之间可移动的位位置是否连续,若不连续直接ans=false ,终止所有循环,输出结果,反之则记录下这两个元素的位置,避免重复
代码:

import java.util.*;
public class Main{
	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		for(int t=sc.nextInt();t>0;t--) {
			int m=sc.nextInt();
			int n=sc.nextInt();
			int arr[]=new int [m];
			int arr1[]=new int [m];
			int a[]=new int [n];
			boolean bool[]=new boolean[m+1];
			boolean bool1[]=new boolean[m];//标记已用的位置
			boolean bool2[]=new boolean[m];//标记已用的位置
			for(int i=0;i<m;i++) {
				arr[i]=sc.nextInt();
				arr1[i]=arr[i];
			}
			Arrays.sort(arr1);
			for(int i=0;i<n;i++){
				a[i]=sc.nextInt();
				bool[a[i]-1]=true;
			}
			boolean ans=true;
			for(int i=0;i<m;i++) {//排序后的顺序
				for(int j=0;j<m;j++) {				
					if(arr1[i]==arr[j]&&!bool1[j]&&!bool2[i]) {
						//System.out.println(i+" "+j);
						int max=Math.max(i,j);
						int min=Math.min(i,j); 
						for(int o=min;o<max;o++) {
							if(bool[o]==false) {
								ans=false;
								break;
							}
						}
						bool1[j]=true;
						bool2[i]=true;
						continue;
					}
				}
				if(!ans)break;
			}
			if(ans)System.out.println("YES");
			else System.out.println("NO");
		}
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值