Codeforces Round #658 (Div. 2)-D Unmerge题解

2020/7/22
不会吧不会真的有人连A都能WA吧,不会吧,不会那个人就是我吧?!
在这里插入图片描述
说正事,D题本身并不算太难。
题目:
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:

If one of the arrays is empty, the result is the other array. That is, merge(∅,b)=b and merge(a,∅)=a. In particular, merge(∅,∅)=∅.
If both arrays are non-empty, and a1 If both arrays are non-empty, and a1>b1, then merge(a,b)=[b1]+merge(a,[b2,…,bm]). That is, we delete the first element b1 of b, merge the remaining arrays, then add b1 to the beginning of the result.
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].

A permutation is an array consisting of n distinct integers from 1 to n 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).

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).
大致题意:
定义一个合并对于a,b两个数组,每次比较a和b数组的最前面那个值,取出较小的那个放进序列中,现在题目给出一个2*n的一个排列,问该排列能否为上述2个各有n个数的a,b数组构成。
分析:
对于一个数,其后面比他小的数必须跟他是相同的一个数组之内的,直到遇到一个比他大的数位置。
例如:6 1 3 7 4 5 8 2
分组为:

  • 6 1 3
  • 7 4 5
  • 8 2

至于为什么,也不难得出:例如6 1 3,6输出出来的前提是另一个数组的最前面的值是>6的,因此接下来若<6则必然是与6所在的数组是同一个数组,直到出现了一个>6的数为止;
那么通过以上方法就可以将上述的序列分成k个长度为pi的子序列.要求每个子序列内的所有值都必须在同一个数组中.因此这题变成了给你若干个数,问你能不能把它们组成成n+n的形式. 那就是直接上dp即可.
需要注意的是,题解给出了一个优化方案能把原来n^2复杂度化为n根号n,因为原来至多存在n个子序列,每个子序列循环n次.但是我们可以发现2*n个数他至多有根号n种长度的子序列,先把子序列长度计算出来,然后排序.分批处理长度相同的子序列即可.
代码:
这里只给出前者n^2的代码,优化后的看题解的implement即可

#include
#define ll long long
using namespace std;
vector<int> v;
int x[20005];
int main()
{
    int t;
    cin >> t;
    while (t--)
    {
        int n;
        cin >> n;
        int i;map<int, int> m;
        vector<int> v;
        int ma = -1;
        int count = 0;
        for (i = 0; i < 2*n; i++)
        {
            cin >> x[i];
            if (ma < x[i])
            {
                if(ma!=-1)v.push_back(count);
                count = 0;
                ma = x[i];
            }
            count++;
        }
        if (count)v.push_back(count);
        map<int, int> vis;
        map<int, int> a;
        vis[0] = 1;
        for (int i = 0; i < v.size(); i++)
        {
            for (int j = n; j >= v[i]; j--)
            {
                if (!vis[j] && vis[j - v[i]])
                {
                    vis[j] = 1;
                }
            }
        }
        cout << (vis[n] ? "YES" : "NO");
        cout << endl;
    }
    return 0;
}

你可能感兴趣的:(刷题记录,算法,动态规划)