Description

Having become bored with standard 2-dimensional artwork (and also frustrated at others copying her work), the great bovine artist Picowso has decided to switch to a more minimalist, 1-dimensional style.Although, her paintings can now be described by a 1-dimensional array of colors of length N(1≤N≤100,000), her painting style remains unchanged: she starts with a blank canvas and layers upon it a sequence of "rectangles" of paint, which in this 1-dimensional case are simply intervals. Sheuses each of the colors 1…Nexactly once, although just as before, some colors might end up being completely covered up by the end.To Picowso's great dismay, her competitor Moonet seems to have figured out how to copy even these 1-dimensional paintings, using a similar strategy to the preceding problem: Moonet will paint a set ofdisjoint intervals, wait for them to dry, then paint another set of disjoint intervals, and so on.Moonet can only paint at most one interval of each color over the entire process. Please compute thenumber of such rounds needed for Moonet to copy a given 1-dimensional Picowso painting.

伟大的牛艺术家皮科沃已经厌倦了标准的二维艺术作品,也在伤心其他人复制她的作品,她决定转向更简约,一维的风格。尽管如此,她的作品现在可以表示描述颜色的一维数组长度N(1≤N≤100000),她的绘画风格没有改变:她以一个空白的画布开始,一次涂色只能涂上连续几个单位的颜料,同样新的颜料可以完全覆盖旧的颜料,每次涂完要等上1day才能完全干,只有旧颜料干了以后才能用新颜料覆盖。皮科沃十分沮丧的是,她的对手Moonet似乎已经找到了如何复制这些一维的绘画,使用类似的策略。前面的问题:Moonet会画几组不相交区间,等待他们干,然后画几组不相交区间,等等。Moonet同一种颜色只能使用一次。请计算该轮Moonet复制一个给定的一维picowso绘画所需要的时间。

Input

The first line of input contains N, and the next N lines contain an integer in the range 0…Nindicating the color of each cell in the 1-dimensional painting (0 for a blank cell).

第一行为N,画条长度从第2行至N行每行一个数表示要涂颜色

Output

Please output the minimum number of rounds needed to copy this painting, or -1 if this could not have possibly been an authentic work of Picowso (i.e., if she could not have painted it using a layered sequence of intervals, one of each color).

输出一个整数表示最少天数。数据若不合法则输出-1

Sample Input

7

0

1

4

5

1

3

3

Sample Output

2

In this example, the interval of color 1 must be painted in an earlier round than the intervals of colors 4 and 5, so at least two rounds are needed.


由于每次都是涂一段连续的区间,因此我们可以将其转换成括号序列,只要求出最大深度即可。不过这样写有个问题,假定我出现了一个1 3 1 3的序列,这显然是不可行的,所以括号序列上要记录一个信息,或者可以用栈来进行维护

#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define inf 0x7f7f7f7f
using namespace std;
typedef long long ll;
typedef unsigned int ui;
typedef unsigned long long ull;
inline int read(){
int x=0,f=1;char ch=getchar();
for (;ch<'0'||ch>'9';ch=getchar()) if (ch=='-') f=-1;
for (;ch>='0'&&ch<='9';ch=getchar()) x=(x<<1)+(x<<3)+ch-'0';
return x*f;
}
inline void print(int x){
if (x>=10) print(x/10);
putchar(x%10+'0');
}
const int N=1e5;
int stack[N+10],fir[N+10],val[N+10],las[N+10];
int main(){
int n=read(),top=0,ans=0;
for (int i=1,x;i<=n;i++){
x=val[i]=read();
if (!fir[x]) fir[x]=i;
las[x]=max(las[x],i); //对于每种颜色,记录开始点和最后的结束点
}
val[las[0]=n+1]=0;
for (int i=0;i<=n+1;i++){
int x=val[i];
if (i==fir[x]) stack[++top]=x,ans=max(ans,top); //遇到开始点就入栈
if (stack[top]!=x){puts("-1");return 0;} //如果一个点不是不是开始点,并且不与栈顶元素相同,则不合法
if (i==las[x]) top--; //是结束点就减栈
}
printf("%d\n",ans-1);
return 0;
}

[Usaco2017 Open]Modern Art 2的更多相关文章

  1. bzoj 4780: [Usaco2017 Open]Modern Art 2

    4780: [Usaco2017 Open]Modern Art 2 Time Limit: 10 Sec  Memory Limit: 128 MB Description Having becom ...

  2. [BZOJ4776] [Usaco2017 Open]Modern Art(差分 + 思维?)

    传送门 可以预处理出每种颜色的上下左右的位置,这样就框出来了一个个矩形,代表每种颜色分别涂了哪里. 然后用二维的差分. 就可以求出来每个位置至少涂了几次,如果 > 1 的话,就肯定不是先涂的, ...

  3. lesson 18 Electric currents in modern art

    lesson18 Electric currents in modern art electricity n. 电力:电流; electric adj. 电的:电动的; electronic adj. ...

  4. 洛谷P3668 [USACO17OPEN]Modern Art 2 现代艺术2

    P3668 [USACO17OPEN]Modern Art 2 现代艺术2 题目背景 小TY的同学HF也想创作艺术 HF只有一块长条状的画布(画条),所以每一次涂色只能涂上连续几个单位的颜料,同样新的 ...

  5. BZOJ2368 : Modern Art Plagiarism 树同构

    枚举$T_1$的树根,然后DP,设$f[i][j]$表示$T_1$的子树$i$是否存在包括i的连通子树与$T_2$的子树$j$同构. 若$j$是叶子,那么显然可以. 若$deg_i<deg_j$ ...

  6. [luoguP3668] [USACO17OPEN]Modern Art 2 现代艺术2(栈)

    传送门 还是一个字——栈 然后加一大堆特判 至少我是这么做的 我的代码 #include <cstdio> #include <iostream> #define N 1000 ...

  7. USACO比赛题泛刷

    随时可能弃坑. 因为不知道最近要刷啥所以就决定刷下usaco. 优先级排在学习新算法和打比赛之后. 仅有一句话题解.难一点的可能有代码. 优先级是Gold>Silver.Platinum刷不动. ...

  8. Lesson 26 The best art critics

    Text I am an art student and I paint a lot of pictures. Manay people pretend that they understand mo ...

  9. Lesson 23 A new house

    Text I had a letter from my sister yesterday. She lives in Nigeria. In her letter, she said that she ...

随机推荐

  1. FIREDAC连MYSQL中文乱码的解决办法

    FIREDAC连MYSQL中文会乱码,因为字符集的原因,字符集设为gb2312以后,不再乱码. if SameText(DatabaseParams.driveId, 'MySQL') then fd ...

  2. 分布式RPC框架性能大比拼

    https://github.com/grpc/grpc http://colobu.com/2016/09/05/benchmarks-of-popular-rpc-frameworks/ http ...

  3. NoSQL之Memcached

    一.Memcached概念 Memcached是NoSQL产品之中的一个,是一个暂时性键值存储NoSQL数据库,过去被大量使用在互联网站点中,作为应用和数据库之间的缓存层,大大提高查询和訪问速度. M ...

  4. Pacemaker 安装与使用

    Pacemaker 仅仅做资源管理器(CRM).底下的消息系统採用 corosync. 安装 以 ubuntu 为例, sudo aptitude install -y pacemaker coros ...

  5. Sql Server 导入还有一个数据库中的表数据

    在涉及到SQL Server编程或是管理时一定会用到数据的导入与导出, 导入导出的方法有多种,此处以SQL Server导入表数据为例.阐述一下: 1.打开SQL Server Management ...

  6. CASE函数 sql server——分组查询(方法和思想) ref和out 一般处理程序结合反射技术统一执行客户端请求 遍历查询结果集,update数据 HBuilder设置APP状态栏

    CASE函数   作用: 可以将查询结果集的某一列的字段值进行替换 它可以生成一个新列 相当于switch...case和 if..else 使用语法: case 表达式/字段 when 值 then ...

  7. Intel为Google的物联网平台Brillo推出开发板Edison

    Brillo* is a solution from Google* for building connected devices. Incorporating aspects of the Andr ...

  8. Centos 6.4 搭建LANMP一键安装版

    lanmp一键安装包是wdlinux官网2010年开始推出的lamp,lnmp,lnamp(apache,nginx,php,mysql,zend,eAccelerator,pureftpd)应用环境 ...

  9. cmd下复制粘贴

    cmd下复制粘贴的快捷操作方式 工具/原料 系统cmd 步骤/方法 1 如右图,右键命令提示符窗口的标题栏,选择属性. 2 选择“编辑选项”里的“快速编辑模式”,并确定之: 3 在弹出的应用选择提示框 ...

  10. debug找到source lookup path以及,debug跑到另外的解决办法

    在我们使用eclipse调试的时候,有时候会出一些奇葩的问题,比如找不到Source  lookup path, 这时我们可以点击Edit Source Lookup Path.接着回弹出一个 我们只 ...