Codeforces #550 (Div3) - G.Two Merged Sequences(dp / 贪心)
Problem Codeforces #550 (Div3) - G.Two Merged Sequences
Time Limit: 2000 mSec
Problem Description
Two integer sequences existed initially, one of them was strictly increasing, and another one — strictly decreasing.
Strictly increasing sequence is a sequence of integers [x1<x2<⋯<xk][x1<x2<⋯<xk] . And strictly decreasing sequence is a sequence of integers [y1>y2>⋯>yl][y1>y2>⋯>yl] . Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing.
Elements of increasing sequence were inserted between elements of the decreasing one (and, possibly, before its first element and after its last element) without changing the order. For example, sequences [1,3,4][1,3,4] and [10,4,2][10,4,2] can produce the following resulting sequences: [10,1,3,4,2,4][10,1,3,4,2,4] , [1,3,4,10,4,2][1,3,4,10,4,2] . The following sequence cannot be the result of these insertions: [1,10,4,4,3,2][1,10,4,4,3,2] because the order of elements in the increasing sequence was changed.
Let the obtained sequence be aa . This sequence aa is given in the input. Your task is to find any two suitable initial sequences. One of them should be strictly increasing, and another one — strictly decreasing. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing.
If there is a contradiction in the input and it is impossible to split the given sequence aa into one increasing sequence and one decreasing sequence, print "NO".
Input
The first line of the input contains one integer nn (1≤n≤2⋅1051≤n≤2⋅105) — the number of elements in aa.
The second line of the input contains nn integers a1,a2,…,ana1,a2,…,an (0≤ai≤2⋅1050≤ai≤2⋅105), where aiai is the ii-th element of a.
Output
If there is a contradiction in the input and it is impossible to split the given sequence aa into one increasing sequence and one decreasing sequence, print "NO" in the first line.
Otherwise print "YES" in the first line. In the second line, print a sequence of nn integers res1,res2,…,resnres1,res2,…,resn, where resiresi should be either 00 or 11 for each ii from 11 to nn. The ii-th element of this sequence should be 00 if the ii-th element of aa belongs to the increasing sequence, and 11 otherwise. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing.
Sample Input
5 1 3 6 8 2 9 0 10
Sample Output
YES
1 0 0 0 0 1 0 1 0
题解:两种做法,先说贪心,维护下降序列当前最小值M和上升序列当前最大值m
1、a[i] > M && a[i] < m,自然无解。
2、a[i] < M && a[i] < m,只能加到下降序列。
3、a[i] > M && a[i] > m,只能加到上升序列。
4、a[i] < M && a[i] > m,此时需要考虑a[i+1]与a[i]的大小关系,不妨假设a[i+1] > a[i],那么此时应将a[i]加入上升序列,原因很简单,如果把a[i]加入下降序列,则a[i+1]只能加入上升序列,显然这种方案不如把a[i]与a[i+1]都加入上升序列(下降的没动,上升的变化相同),另一种情况同理。
以上四点给出贪心算法并说明贪心成立。
第二种动态规划,分段决策类的动态规划,无非就是考虑第i个数加到上升还是下降,所以很容易想到二维dp,第一维表处理到第i个数,第二维表加入哪个序列,难想的地方在于要优化什么东西,这里的状态定义就很值得学习了:
dp(i, 0)表示处理完前i个数,将i加入递增序列后递减序列元素中最后一个元素的最大值。
dp(i, 1)表示处理完前i个数,将i加入递减序列后递增序列元素中最后一个元素的最小值。
我们肯定是希望前者越大越好,后者越小越好,这样给后面的数字提供更大的选择空间,其实这样定义状态看似有点绕,其实很合理,因为把i加入递增序列后,递增序列的最小值就有了,所以只需要再维护一下递减的最大值即可,加入递减序列同理。再说状态转移的问题,一般动态规划都是难在状态,此题也不例外,转移不难,就是枚举a[i]和a[i-1]分别放在哪种序列中即可,转移时要记录路径,方便最后输出。
贪心代码没啥说的就不贴了,只给出dp代码。
#include <bits/stdc++.h> using namespace std; #define REP(i, n) for (int i = 1; i <= (n); i++)
#define sqr(x) ((x) * (x)) const int maxn = + ;
const int maxm = + ;
const int maxs = + ; typedef long long LL;
typedef pair<int, int> pii;
typedef pair<double, double> pdd; const LL unit = 1LL;
const int INF = 0x3f3f3f3f;
const LL mod = ;
const double eps = 1e-;
const double inf = 1e15;
const double pi = acos(-1.0); int n;
int a[maxn], dp[maxn][];
int path[maxn][];
int ans[maxn]; int main()
{
ios::sync_with_stdio(false);
cin.tie();
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
cin >> n;
for (int i = ; i <= n; i++)
{
cin >> a[i];
}
dp[][] = INF, dp[][] = -INF;
for (int i = ; i <= n; i++)
{
dp[i][] = -INF, dp[i][] = INF;
if (a[i - ] < a[i] && dp[i][] < dp[i - ][])
{
dp[i][] = dp[i - ][];
path[i][] = ;
}
if (dp[i - ][] > a[i] && dp[i][] > a[i - ])
{
dp[i][] = a[i - ];
path[i][] = ;
}
if (a[i] > dp[i - ][] && dp[i][] < a[i - ])
{
dp[i][] = a[i - ];
path[i][] = ;
}
if (a[i] < a[i - ] && dp[i][] > dp[i - ][])
{
dp[i][] = dp[i - ][];
path[i][] = ;
}
}
if(dp[n][] > -INF)
{
cout << "YES" << endl;
int opt = ;
for(int i = n; i >= ; i--)
{
ans[i] = opt;
opt = path[i][opt];
}
for(int i = ; i <= n; i++)
{
cout << ans[i] << " ";
}
}
else if(dp[n][] < INF)
{
cout << "YES" << endl;
int opt = ;
for(int i = n; i >= ; i--)
{
ans[i] = opt;
opt = path[i][opt];
}
for(int i = ; i <= n; i++)
{
cout << ans[i] << " ";
}
}
else
{
cout << "NO";
}
return ;
}
Codeforces #550 (Div3) - G.Two Merged Sequences(dp / 贪心)的更多相关文章
- Codeforces 1144G Two Merged Sequences dp
Two Merged Sequences 感觉是个垃圾题啊, 为什么过的人这么少.. dp[ i ][ 0 ]表示处理完前 i 个, 第 i 个是递增序列序列里的元素,递减序列的最大值. dp[ i ...
- Codeforces 429C Guess the Tree(状压DP+贪心)
吐槽:这道题真心坑...做了一整天,我太蒻了... 题意 构造一棵 $ n $ 个节点的树,要求满足以下条件: 每个非叶子节点至少包含2个儿子: 以节点 $ i $ 为根的子树中必须包含 $ c_i ...
- Codeforces Round #276 (Div. 1)D.Kindergarten DP贪心
D. Kindergarten In a kindergarten, the children are being divided into groups. The teacher put t ...
- [CodeForces - 1272D] Remove One Element 【线性dp】
[CodeForces - 1272D] Remove One Element [线性dp] 标签:题解 codeforces题解 dp 线性dp 题目描述 Time limit 2000 ms Me ...
- codeforces #579(div3)
codeforces #579(div3) A. Circle of Students 题意: 给定一个n个学生的编号,学生编号1~n,如果他们能够在不改变顺序的情况下按编号(无论是正序还是逆序,但不 ...
- Codeforces 219D. Choosing Capital for Treeland (树dp)
题目链接:http://codeforces.com/contest/219/problem/D 树dp //#pragma comment(linker, "/STACK:10240000 ...
- (第二场)D Money 【dp\贪心】
题目:https://www.nowcoder.com/acm/contest/140/D 题目描述: White Cloud has built n stores numbered from 1 t ...
- 【bzoj4027】[HEOI2015]兔子与樱花 树形dp+贪心
题目描述 很久很久之前,森林里住着一群兔子.有一天,兔子们突然决定要去看樱花.兔子们所在森林里的樱花树很特殊.樱花树由n个树枝分叉点组成,编号从0到n-1,这n个分叉点由n-1个树枝连接,我们可以把它 ...
- BZOJ 2021 [Usaco2010 Jan]Cheese Towers:dp + 贪心
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2021 题意: John要建一个奶酪塔,高度最大为m. 他有n种奶酪.第i种高度为h[i]( ...
随机推荐
- python3 购物车
今天干了啥?喂了喂龟,看了看鱼... 然后就是学习了两个模块:sys模块和os模块,突然觉得python真的好,只要英语学的好,看代码超级舒服的说,嗯,我要好好学英语,今天背了几个啥,唉.写完博客再背 ...
- 重新发现MATLAB
现场编辑 创建不仅捕获代码的脚本 - 它们讲述了可以与他人共享的故事.自动化的上下文提示可让您在编程时快速移动,并将结果和可视化与代码一起显示. 学到更多 访问MATLAB实时脚本库 ...
- 服务端渲染和nuxt简单介绍
概述 最近研究了一下服务端渲染,有一些心得,记录下来供以后开发时参考,相信对其他人也有用. 参考资料: Vue SSR指南 nuxt.js官网 服务端渲染介绍 服务端渲染简单来说,就是分别对项目用we ...
- dubbo源码研究(一)
1. dubbo源码研究(一) 1.1. dubbo启动加载过程 我们知道,现在流行注解方式,用spring管理服务,dubbo最常用的就是@Reference和@Service了,那么我首先找到这两 ...
- Vue+abp微信扫码登录
最近系统中要使用微信扫码登录,根据微信官方文档和网络搜索相关文献实现了.分享给需要的人,也作为自己的一个笔记.后端系统是基于ABP的,所以部分代码直接使用了abp的接口,直接拷贝代码编译不通过. 注册 ...
- python --- 冒泡排序算法
别想太多了,这个冒泡排序就是我们脑海中想到的那个冒泡,就好像是气泡一样,较小的元素比较轻,从而要往上浮出来, 冒泡排序算法. 要对‘气泡’序列处理若干遍.所谓一遍处理,就是自底向上检查一遍这个序列,并 ...
- JavaScript第一回-来龙去脉
简写:JavaScript-JS ECMAScript-ES 写在前面的话:啃文字大多时间不是件愉快的事情,但是我们必须过这个坎,让自己习惯,让自己不讨厌,至于喜欢不喜欢,我们等时间给出答案. J ...
- height:auto 火狐没边框
css高度设置为auto后,设置的边框 ie正常 火狐 就没有边框了,解决方法 之前是这样写的 #right_bottom { width: 790px; height:auto; border: # ...
- ASP.NET Core MVC应用程序中的后台工作任务
在应用程序的内存中缓存常见数据(如查找)可以显着提高您的MVC Web应用程序性能和响应时间.当然,这些数据必须定期刷新. 当然你可以使用任何方法来更新数据,例如Redis中就提供了设定缓存对象的生命 ...
- 《深入理解Java虚拟机》-----第5章 jvm调优案例分析与实战
案例分析 高性能硬件上的程序部署策略 例 如 ,一个15万PV/天左右的在线文档类型网站最近更换了硬件系统,新的硬件为4个CPU.16GB物理内存,操作系统为64位CentOS 5.4 , Resin ...