日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

WeirdSort CodeForces - 1311B(暴力)

發(fā)布時(shí)間:2023/12/15 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 WeirdSort CodeForces - 1311B(暴力) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

You are given an array a of length n.

You are also given a set of distinct positions p1,p2,…,pm, where 1≤pi<n. The position pi means that you can swap elements a[pi] and 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≤?≤an) using only allowed swaps.

For example, if a=[3,2,1] and p=[1,2], then we can first swap elements a[2] and a[3] (because position 2 is contained in the given set p). We get the array a=[3,1,2]. Then we swap a[1] and a[2] (position 1 is also contained in p). We get the array a=[1,3,2]. Finally, we swap a[2] and a[3] again and get the array a=[1,2,3], sorted in non-decreasing order.

You can see that if a=[4,1,2,3] and p=[3,2] then you cannot sort the array.

You have to answer t independent test cases.

Input
The first line of the input contains one integer t (1≤t≤100) — the number of test cases.

Then t test cases follow. The first line of each test case contains two integers n and m (1≤m<n≤100) — the number of elements in a and the number of elements in p. The second line of the test case contains n integers a1,a2,…,an (1≤ai≤100). The third line of the test case contains m integers p1,p2,…,pm (1≤pi<n, all pi are 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≤?≤an) using only allowed swaps. Otherwise, print “NO”.

Example
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
總感覺這個(gè)題有什么比較好的解法的,但是我太菜了,沒想到。我的做法就是直接暴力。因?yàn)閿?shù)據(jù)量不大,感覺循環(huán)幾次就可以判斷出能否實(shí)現(xiàn)。
代碼如下:

#include<bits/stdc++.h> #define ll long long using namespace std;const int maxx=1e2+100; int a[maxx]; int n,m;inline bool check() {int flag=1;for(int i=1;i<n;i++){if(a[i]>a[i+1]) return 0;}return 1; } int main() {int t;scanf("%d",&t);while(t--){scanf("%d%d",&n,&m);for(int i=1;i<=n;i++) scanf("%d",&a[i]);set<int> s;int x;for(int i=1;i<=m;i++) {scanf("%d",&x);s.insert(x);}int flag=1;while(1){if(check()) break;for(int i=1;i<n;i++){if(a[i]>a[i+1]){set<int> ::iterator it=s.lower_bound(i);if(*it!=i){flag=0;break;}swap(a[i],a[i+1]);}}if(flag==0) break;//for(int i=1;i<=n;i++) cout<<a[i]<<" ";cout<<endl;}if(flag) cout<<"YES"<<endl;else cout<<"NO"<<endl;}return 0; }

努力加油a啊,(o)/~

總結(jié)

以上是生活随笔為你收集整理的WeirdSort CodeForces - 1311B(暴力)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。