题目链接:https://codeforces.com/contest/1382/problem/D

题意

给出一个大小为 $2n$ 的排列,判断能否找到两个长为 $n$ 的子序列,使得二者归并排序后能够得到该排列。

题解

将原排列拆分为一个个连续子序列,每次从大于上一子序列首部的元素处分出下一连续子序列,只要将这些子序列按照拆分先后排列,归并排序后一定可以得到原排列。

之后即判断能否将这些子序列排列为两个长为 $n$ 的序列即可,可以用状压 $dp$,也可以用 $01$ 背包。

状态 $dp$:每次将之前的每一个可行长度加上当前长度得到新一批的可行长度,然后将当前长度标记为可行。

$01$ 背包:将每个子序列的长度视为其花费与价值,最后判断花费为 $n$ 的背包总价值是否为 $n$ 即可。

代码一

状压 $dp$:$O_{(\frac{n^2}{w})}$ ($w$ 视机器字长而定,参考资料

#include <bits/stdc++.h>
using namespace std; void solve() {
int n; cin >> n;
int mx = 0;
vector<int> idx;
for (int i = 0; i < 2 * n; ++i) {
int x; cin >> x;
if (x > mx) {
mx = x;
idx.push_back(i);
}
}
idx.push_back(2 * n);
vector<int> len;
for (int i = 1; i < idx.size(); ++i) {
len.push_back(idx[i] - idx[i - 1]);
}
bitset<2020> dp;
for (auto i : len) {
dp |= dp << i;
dp[i] = 1;
}
cout << (dp[n] ? "YES" : "NO") << "\n";
} int main() {
int t; cin >> t;
while (t--) solve();
}

代码二

$01$ 背包:$O_{(vn)}$

#include <bits/stdc++.h>
using namespace std; void solve() {
int n; cin >> n;
int mx = 0;
vector<int> idx;
for (int i = 0; i < 2 * n; ++i) {
int x; cin >> x;
if (x > mx) {
mx = x;
idx.push_back(i);
}
}
idx.push_back(2 * n);
int N = idx.size() - 1, p = 0;
int cost[N] = {}, val[N] = {};
for (int i = 1; i < idx.size(); ++i) {
cost[p] = val[p] = idx[i] - idx[i - 1];
++p;
}
map<int, int> dp;
for (int i = 0; i < N; ++i) {
for (int j = n; j >= cost[i]; --j) {
dp[j] = max(dp[j], dp[j - cost[i]] + val[i]);
}
}
cout << (dp[n] == n ? "YES" : "NO") << "\n";
} int main() {
int t; cin >> t;
while (t--) solve();
}

Codeforces Round #658 (Div. 2) D. Unmerge(dp)的更多相关文章

  1. Codeforces Round #260 (Div. 2)C. Boredom(dp)

    C. Boredom time limit per test 1 second memory limit per test 256 megabytes input standard input out ...

  2. Codeforces Round #471 (Div. 2) F. Heaps(dp)

    题意 给定一棵以 \(1\) 号点为根的树.若满足以下条件,则认为节点 \(p\) 处有一个 \(k\) 叉高度为 \(m\) 的堆: 若 \(m = 1\) ,则 \(p\) 本身就是一个 \(k\ ...

  3. 【Codeforces】Codeforces Round #374 (Div. 2) -- C. Journey (DP)

    C. Journey time limit per test3 seconds memory limit per test256 megabytes inputstandard input outpu ...

  4. Codeforces Round #652 (Div. 2) D. TediousLee(dp)

    题目链接:https://codeforces.com/contest/1369/problem/D 题意 最初有一个结点,衍生规则如下: 如果结点 $u$ 没有子结点,添加 $1$ 个子结点 如果结 ...

  5. Codeforces Round #247 (Div. 2) C. k-Tree (dp)

    题目链接 自己的dp, 不是很好,这道dp题是 完全自己做出来的,完全没看题解,还是有点进步,虽然这个dp题比较简单. 题意:一个k叉树, 每一个对应权值1-k, 问最后相加权值为n, 且最大值至少为 ...

  6. Codeforces Round #165 (Div. 1) Greenhouse Effect(DP)

    Greenhouse Effect time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  7. Codeforces Round #119 (Div. 2) Cut Ribbon(DP)

    Cut Ribbon time limit per test 1 second memory limit per test 256 megabytes input standard input out ...

  8. Codeforces Round #368 (Div. 2) B. Bakery (模拟)

    Bakery 题目链接: http://codeforces.com/contest/707/problem/B Description Masha wants to open her own bak ...

  9. Codeforces Round #658 (Div. 2) D. Unmerge (思维,01背包)

    题意:有两个数组\(a\)和\(b\),每次比较它们最左端的元素,取小的加入新的数组\(c\),若\(a\)或\(b\)其中一个为空,则将另一个全部加入\(c\),现在给你一个长度为\(2n\)的数组 ...

随机推荐

  1. 【渲染引擎】Blender的2021年最佳渲染引擎(上)

    Blender最终摆脱了"古怪的孩子"的装束,并穿上了更为严肃和受人尊敬的" 3D强者". 它已在业界获得广泛认可,许多工作室和艺术家正在将其纳入他们的产品线. ...

  2. LeetCode 二分查找模板 I

    模板 #1: int binarySearch(vector<int>& nums, int target){ if(nums.size() == 0) return -1; in ...

  3. 【MySQL】centos6中/etc/init.d/下没有mysqld启动文件,怎么办

    如果/etc/init.d/下面没有mysqld的话,service mysqld start也是不好使的,同样,chkconfig mysqld on也是不能用 解决办法: 将mysql的mysql ...

  4. Python利用最优化算法求解投资内部收益率IRR【一】

    一. 内部收益率和净现值 内部收益率(Internal Rate of Return, IRR)其实要和净现值(Net Present Value, NPV)结合起来讲.净现值指的是某个投资项目给公司 ...

  5. Nginx(七):location的使用以及nginx关闭原理

    上一篇中,我们了解了如何nginx的配置原则及解析框架,以及解析location配置的具体实现,相信大家对该部分已经有了比较深刻的认识. 本篇,我们进一步来了解下,解析之后的配置,如何应用到实际中的吧 ...

  6. Kubernetes集群管理工具kubectl命令技巧大全

    一. kubectl概述 Kubectl是用于控制Kubernetes集群的命令行工具,通过kubectl能够对集群本身进行管理,并能够在集群上进行容器化应用的安装部署. kubectl命令的语法如下 ...

  7. ovs-actions

    1. 端口说明 OVS支持如下的标准OpenFlow端口名称(括号中是端口号): in_port (65528 or 0xfff8; 0xfffffff8) table (65529 or 0xfff ...

  8. ES 2021 来了,详细解读5个新特性,附案例

    ES 2021是世界上最受欢迎的编程语言的最新版本〜 本次迭代中包含了五个新特性,让我们来一睹为快. 1.全部替换replaceAll: js默认的replace 方法仅替换字符串中一个模式的第一个实 ...

  9. LOJ10092半连通子图

    Description 一个有向图G=(V,E)称为半连通的(Semi-Connected),如果满足:?u,v∈V,满足u→v或v→u,即对于图中任意两点u,v,存在一条u到v的有向路径或者从v到u ...

  10. FLOYD判圈

    转载一篇博客:http://blog.csdn.net/javasus/article/details/50015687 Floyd判圈算法(Floyd Cycle Detection Algorit ...