題目:ios
Let a and b be two arrays of lengths n and m, respectively, with no elements in common. We can define a new array merge(a,b) of length n+m recursively as follows:app
This algorithm has the nice property that if a and b are sorted, then merge(a,b) will also be sorted. For example, it is used as a subroutine in merge-sort. For this problem, however, we will consider the same procedure acting on non-sorted arrays as well. For example, if a=[3,1] and b=[2,4],then merge(a,b)=[2,3,1,4].ide
A permutation is an array consisting of nn distinct integers from 11 to nn in arbitrary order. For example, [2,3,1,5,4] is a permutation, but [1,2,2] is not a permutation (2 appears twice in the array) and [1,3,4] is also not a permutation (n=3 but there is 4 in the array).this
There is a permutation p of length 2n. Determine if there exist two arrays a and b, each of length n and with no elements in common, so that p=merge(a,b).spa
思路:咱們容易想到「3 2」,「7 1」這些狀況,這兩個數必定屬於同一個集合且是連續的,根據這個規律看「7 1 6」,咱們發現若是6和 7 1不屬於同一個集合的話,那麼6必定時另外一個集合的頭元素,那麼6必定不可能出如今7的後面,能夠推出「7 1 6」屬於同一個集合,這樣咱們能夠找到一個規律,例如:code
6 1 3 7 4 5 8 2,咱們能夠分紅[6 1 3] [7 4 5] [8 2];3 2 6 1 5 7 8 4,咱們能夠分紅[3 2] [6 1 5] [7] [8 4].這樣咱們能夠把每一個塊的個數統計,而後咱們只須要肯定這些數字是否是能夠組成n便可。blog
1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 #include <queue> 5 #include <string> 6 #include <vector> 7 #include <cmath> 8 9 using namespace std; 10 11 #define ll long long 12 #define pb push_back 13 #define fi first 14 #define se second 15 16 const int N = 2e5 + 10; 17 bool f[N]; 18 int a[N]; 19 20 void solve() 21 { 22 int T; 23 cin >> T; 24 while(T--){ 25 int n; 26 cin >> n; 27 n <<= 1; 28 for(int i = 1; i <= n; ++i) cin >> a[i]; 29 // for(int i = 1; i <= n; ++i) cout << a[i] << " "; 30 // cout << endl; 31 vector<int > v; 32 int x = a[1]; 33 int cnt = 0, inx = 1; 34 while(1){ 35 if(a[inx] <= x) cnt++, inx++; 36 else{ 37 v.pb(cnt); 38 cnt = 0; 39 x = a[inx]; 40 } 41 if(inx > n) break; 42 } 43 if(cnt > 0) v.pb(cnt); 44 // cout << "n = " << n << endl; 45 n >>= 1; 46 for(int i = 0; i <= n; ++i) f[i] = false; 47 f[0] = true; 48 //cout << " f[n] = " << f[n] << endl; 49 for(auto vv : v){ 50 for(int i = n; i >= 0; --i){ 51 if(i - vv < 0) break; 52 if(f[i - vv] == true) f[i] = true; 53 } 54 } 55 //cout << "n = " << n << endl; 56 if(f[n] == true) cout << "YES" << endl; 57 else cout << "NO" << endl; 58 } 59 } 60 61 int main() 62 { 63 ios::sync_with_stdio(false); 64 cin.tie(0); 65 cout.tie(0); 66 solve(); 67 68 return 0; 69 }