[Usaco2017 Open]Modern Art 2

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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章