传送门

还是一个字——栈

然后加一大堆特判

至少我是这么做的

我的代码

#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

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

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

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

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

  2. [LuoguP3668][USACO17OPEN]现代艺术2

    [LuoguP3668][USACO17OPEN]Modern Art2(Link) 现在你有一块长为\(N\)的画布,每次可以选择一段连续的区间进行颜色填涂,新颜色会覆盖旧颜色.每一次填涂都要耗费一 ...

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

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

  4. lesson 18 Electric currents in modern art

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

  5. [Usaco2017 Open]Modern Art 2

    Description Having become bored with standard 2-dimensional artwork (and also frustrated at others c ...

  6. BZOJ2368 : Modern Art Plagiarism 树同构

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

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

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

  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. 使用POI创建word表格合并单元格兼容wps

    poi创建word表格合并单元格代码如下: /** * @Description: 跨列合并 */ public void mergeCellsHorizontal(XWPFTable table, ...

  2. Azure 项目构建 – 构建稳定的直播和点播教学系统

    本课程主要介绍了如何在 Azure 平台上快速构建和部署基于 Azure 虚拟机的点播和直播教学系统, 实践讲解如何使用 Azure 门户创建虚拟机,配置视频服务,连接 CDN 加速 等. 具体包括项 ...

  3. selenium-Python之鼠标事件

    通过click()来模拟鼠标的单击操作,鼠标还具有鼠标右击,双击,悬停甚至鼠标拖动等功能.在webdriver中,将这些鼠标操作方法封装在ActionChains类提供. ActionChains类提 ...

  4. PHP开发基础视频教程

    PHP现今作为互联网运用很广泛的编程语言,市场需求量也越来越高,而PHP开发工程师的薪资也是一路水涨船高,更多的人看到了PHP的发展前景,纷纷都想投入到PHP的开发大军中来,那么对于很多转行或者零基础 ...

  5. JAVASCRIPT闭包以及原型链

    方法内部还有个方法,实例化父方法后,再次调用父方法,可以运行父方法内部的子方法,这样的程序就叫做闭包 DEMO如下: //function outerFn() { // var outerVar = ...

  6. hydra 中文文档

    hydra(九头蛇)是一款开源的协议爆破工具,功能十分强大!!! 具体使用如下: -R   继续从上一次进度接着破解 -I 忽略已破解的文件进行破解 -S 采用SSL链接 -s 端口 指定非默认服务端 ...

  7. 46.Maximum Product Subarray(最大乘积子数组)

    Level:   Medium 题目描述: Given an integer array nums, find the contiguous subarray within an array (con ...

  8. 升级nodejs 与短小的n模块

    要用指令升级nodejs到新版本要先安装n模块 window用不了n模块  可以用 nvm-windows : https://github.com/coreybutler/nvm-windows n ...

  9. Fortran学习笔记2(变量声明)

    常数的申明方式 变量初始化 等价申明EQUIALENCE 类型转化 自定义类型 KIND用法 常数的申明方式 程序中所有处理的数据,有些事固定不变的常数,如圆周率π和重力加速度G等. 此时,程序员可以 ...

  10. PAT Basic 1053

    1053 住房空置率 在不打扰居民的前提下,统计住房空置率的一种方法是根据每户用电量的连续变化规律进行判断.判断方法如下: 在观察期内,若存在超过一半的日子用电量低于某给定的阈值 e,则该住房为“可能 ...