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

传送门

还是一个字——栈

然后加一大堆特判

至少我是这么做的

我的代码

#include <cstdio>
#include <iostream>
#define N 100001
#define max(x, y) ((x) > (y) ? (x) : (y))

int s[N], a[N], b[N];
int n, top, ans;

inline int read()
{
	int x = 0, f = 1;
	char ch = getchar();
	for(; !isdigit(ch); ch = getchar()) if(ch == ‘-‘) f = -1;
	for(; isdigit(ch); ch = getchar()) x = (x << 1) + (x << 3) + ch - ‘0‘;
	return x * f;
}

int main()
{
	int i, x;
	n = read();
	for(i = 1; i <= n; i++)
	{
		a[i] = read();
		b[a[i]]++;
	}
	for(i = 1; i <= n; i++)
	{
		if(!top && !a[i]) continue;
		else if(!a[i])
		{
			s[++top] = a[i];
			continue;
		}
		if(s[top] == a[i] && b[a[i]] > 1)
		{
			ans = max(ans, top);
			b[a[i]]--;
		}
		else if(s[top] == a[i] && b[a[i]] == 1)
		{
			ans = max(ans, top);
			b[a[i]]--;
			top--;
		}
		else if(b[a[i]] == 1) ans = max(ans, top + 1);
		else
		{
			s[++top] = a[i];
			b[a[i]]--;
		}
	}
	if(top) puts("-1");
	else printf("%d\n", ans);
	return 0;
}

  

看了题解

发现预处理出来每种颜色最左边和最右边的位置会更好处理

如果两种颜色有交集,直接输出-1

题解代码

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
int n;
int a[100008],l[100008],r[100008];
int s[100008],top;
int tmp,ans;
int main()
{
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>a[i];
        if(!l[a[i]])    l[a[i]]=i;
        r[a[i]]=i;
    }
    for(int i=1;i<=n;i++){
        if(a[i]==0){
            if(top){
                cout<<-1;
                return 0;
            }
            else continue;
        }
        if(l[a[i]]==i){
            if(top&&r[a[i]]>r[s[top]]){
                cout<<-1;
                return 0;
            }
            s[++top]=a[i];
            tmp++;
            ans=max(ans,tmp);
        }
        if(r[a[i]]==i){
            top--;
            tmp--;
        }
    }
    cout<<ans;
    return 0;
}

  

5

1 2 1 2 1

这组数据,我输出-1,题解输出2

好像题解错了,但因为数据水,所以,你懂的

时间: 2024-07-30 04:23:41

[luoguP3668] [USACO17OPEN]Modern Art 2 现代艺术2(栈)的相关文章

[LuoguP3668][USACO17OPEN]现代艺术2

[LuoguP3668][USACO17OPEN]Modern Art2(Link) 现在你有一块长为\(N\)的画布,每次可以选择一段连续的区间进行颜色填涂,新颜色会覆盖旧颜色.每一次填涂都要耗费一天时间.在所有的填涂中每一种颜色只能用1次.求将画布变为目标序列的最小天数.如果不能完成填涂那么输出\(-1\). 首先我们要知道填涂的方式以及什么叫做填涂不合法. 上面的这个方案明显是合法的,我们先涂上"底色"\(Green\)然后再涂\(Red\)就好. 上面的这个情况,你发现无论怎么

bzoj 4780: [Usaco2017 Open]Modern Art 2

4780: [Usaco2017 Open]Modern Art 2 Time Limit: 10 Sec  Memory Limit: 128 MB Description Having become bored with standard 2-dimensional artwork (and also frustrated at others copying her w ork), the great bovine artist Picowso has decided to switch t

New Concept English Two 10 28

$课文26 最佳艺术评论家 256. I am an art student and I paint a lot of pictures. 我是个学艺术的学生,画了很多画. 257. Many people pretend that they understand modern art 有很多人装成很懂现代艺术的样子,. 258. They always tell you what a picture is 'about'. 总是告诉你一幅画的"意思"是什么. 259. Of cour

bzoj2592: [Usaco2012 Feb]Symmetry

Description After taking a modern art class, Farmer John has become interested in finding geometric patterns in everything around his farm. He carefully plots the locations of his N cows (2 <= N <= 1000), each one occupying a distinct point in the 2

新概念英语第三册1-20课(转)

Lesson 1  A Puma at large 逃遁的美洲狮 1.美洲狮是大的,Pumas are large,猫一样的 cat-like动物 animals它们被发现 which are found在美洲. in America. 美洲狮是一种大型猫科动物Pumas are large, cat-like animals它们被发现在美洲. which are found in America. 美洲狮一种生活在美洲的大型猫科动物, 2.当时-报告When reports提交到伦敦动物园 c

新概念英语第二册01-30课(转)

lesson 1 一个私人的谈话   1.上个星期 Last week 我去了剧院.I went to the theatre.    上星期我去看戏.Last week I went to the theatre.   上星期我去看戏.     2.我有I had一个很好的座位.a very good seat.   我坐的位置很好.I had a very good seat. 我坐的位置很好.     3.那部戏The play是 was很有趣. very interesting.   那

洛克菲勒

十大名言编辑 1.我不靠天赐的运气活着,但我靠策划运气发达. 2.要让金钱当我的奴隶,而不能让我当金钱的奴隶. 3.一个人不是在计划成功,就是在计划失败. 4.命运给予我们的不是失望之酒,而是机会之杯. 5.我的信念是抢在别人之前达到目的. 6.智慧之书的第一章,也就是最后一章,就是天下没有白吃的午餐. 7.把一头猪好好夸奖一番,它都能爬到树上去. 8.永远不能让自己的个人偏见妨碍自己的成功. 9.往上爬的时候要对别人好一点,因为你走下坡的时候会碰到他们. 10.即使输了,唯一该做的就是光明磊落

Paths on a Grid(规律)

Paths on a Grid Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 23270   Accepted: 5735 Description Imagine you are attending your math lesson at school. Once again, you are bored because your teacher tells things that you already mastere

【字源大挪移—读书笔记】 第二部分:字根

[2] 字根:[2.1]表示[否定]的字根.[2.2]表示[方位]的字根.[2.3]表示[程度]的字根.[2.4]表示[状态]的字根.[2.5]表示[现象]的字根.[2.6]表示[身体]的字根.[2.7]表示[姿势]的字根.[2.8]表示[心,心里活动]的字根.[2.9]表示[行为动作]的字根.[2.10]表示[感官动作]的字根.[2.11]表示[感觉]的字根.[2.12]表示[生命]的字根.[2.13]表示[死亡]的字根.[2.14]表示[社会]的字根 [2.1]表示[否定]的字根 -neg-