题目描述

A sequence of integers is given.

The bytecomputer is a device that allows the following operation on the sequence:

incrementing for any.

There is no limit on the range of integers the bytecomputer can store, i.e., each can (in principle) have arbitrarily small or large value.

Program the bytecomputer so that it transforms the input sequence into a non-decreasing sequence (i.e., such that ) with the minimum number of operations.

给一个只包含-1,0,1的数列,每次操作可以让a[i]+=a[i-1],求最少操作次数使得序列单调不降

输入输出格式

输入格式:

The first line of the standard input holds a single integer , the number of elements in the (bytecomputer's) input sequence.

The second line contains integers that are the successive elements of the (bytecomputer's) input sequence, separated by single spaces.

In tests worth 24% of the total points it holds that , and in tests worth 48% of the total points it holds that .

输出格式:

The first and only line of the standard output should give one integer, the minimum number of operations the bytecomputer has to perform to make its input sequence non-decreasing, of the single word BRAK (Polish for none) if obtaining such a sequence is impossible.

输入输出样例

输入样例#1: 复制

6

-1 1 0 -1 0 1

输出样例#1: 复制

3


  1. 显然-2及以下是不可能出现所以不会出现的
  2. 显然2及以上是因为愚蠢所以不会出现的

综上,操作后的序列还是只有\(-1,0,1\)三种数字

因为操作由前到后进行显然会更优,所以线性地推即可

对于每一种情况暴力处理即可


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#define LL long long
#define max(a,b) ((a)>(b)? (a):(b))
#define min(a,b) ((a)<(b)? (a):(b)) using namespace std; int d[100001],f[100001][4],n,b[1000001][4],i,j; int main()
{
memset(f,0x3f,sizeof(f));
scanf("%d",&n);
for(i=1;i<=n;i++) scanf("%d",&d[i]);
b[1][d[1]+1]=1; f[1][d[1]+1]=0;
for(i=2;i<=n;i++)
{
if(d[i]==-1 && (!b[i-1][0]) && (!b[i-1][2]))
{
printf("BRAK");
return 0;
} if(d[i]==-1)
{
if(b[i-1][0]) b[i][0]=1, f[i][0]=f[i-1][0];
if(b[i-1][2]) b[i][2]=1, f[i][2]=f[i-1][2]+2;
} if(d[i]==0)
{
if(b[i-1][0])
{
b[i][1]=1; f[i][1]=f[i-1][0];
b[i][0]=1; f[i][0]=f[i-1][0]+1;
}
if(b[i-1][1]) b[i][1]=1, f[i][1]=min(f[i][1],f[i-1][1]);
if(b[i-1][2]) b[i][2]=1, f[i][2]=min(f[i][2],f[i-1][2]+1);
}
if(d[i]==1)
{
if(b[i-1][0])
{
b[i][1]=1; f[i][1]=f[i-1][0]+1;
b[i][2]=1; f[i][2]=f[i-1][0];
b[i][0]=1; f[i][0]=f[i-1][0]+2;
}
if(b[i-1][1]) b[i][2]=1, f[i][2]=min(f[i][2], f[i-1][1]);
if(b[i-1][2]) b[i][2]=1, f[i][2]=min(f[i][2], f[i-1][2]);
}
}
printf("%d",min(min(f[n][0],f[n][1]),f[n][2]));
}

P3558 [POI2013]BAJ-Bytecomputer的更多相关文章

  1. Luogu P3558 [POI2013]BAJ-Bytecomputer(线性dp)

    P3558 [POI2013]BAJ-Bytecomputer 题意 给一个只包含\(-1,0,1\)的数列,每次操作可以让a[i]+=a[i-1],求最少操作次数使得序列单调不降.若无解则输出BRA ...

  2. 【bzoj3427】Poi2013 Bytecomputer dp

    题目描述 A sequence of N  integers I1,I2…In from the set {-1,0,1} is given. The bytecomputer is a device ...

  3. 【BZOJ】3427: Poi2013 Bytecomputer

    题意: 给定一个长度为\(n\)的\(\{-1, 0, 1\}\)组成的序列,你可以进行\(x_i=x_i+x_{i-1}\)这样的操作,求最少操作次数使其变成不降序列.(\(n \le 100000 ...

  4. BZOJ3427 Poi2013 Bytecomputer

    可以YY一下嘛= = 最后一定是-1, -1, ..., -1, 0, 0, ... 0, 1, 1, ..., 1的一个数列 于是f[i][j]表示到了第i个数,新数列的第j项为-1 or 0 or ...

  5. BZOJ3427 Poi2013 Bytecomputer 【dp】

    题目链接 BZOJ3427 题解 容易发现最终序列一定是\(\{-1,0,1\}\)组成的 因为如果有一个位置不是,那么这个位置一定大于\(1\),那么上一个位置一定为\(1\),所以该位置一定加到过 ...

  6. POI2013 Bytecomputer

    题目:http://www.lydsy.com/JudgeOnline/problem.php?id=3427 可以证明最终序列为-1...0....1 因为首先如果 a(i-1) 为-1或0,执行操 ...

  7. POI2013题解

    POI2013题解 只做了BZ上有的\(13\)道题. 就这样还扔了两道神仙构造和一道计算几何题.所以只剩下十道题了. [BZOJ3414][Poi2013]Inspector 肯定是先二分答案,然后 ...

  8. bzoj 1138: [POI2009]Baj 最短回文路 dp优化

    1138: [POI2009]Baj 最短回文路 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 161  Solved: 48[Submit][Sta ...

  9. [POI2013]Łuk triumfalny

    [POI2013]Łuk triumfalny 题目大意: 一棵\(n(n\le3\times10^5)\)个结点的树,一开始\(1\)号结点为黑色.\(A\)与\(B\)进行游戏,每次\(B\)能选 ...

随机推荐

  1. Java学习--Cookie 和session

  2. 为什么要用 C# 来作为您的首选编程语言

    因为您可以用,并且也是您的最佳选择!之所以可用,是因为 C# 能够很好地在 Mac.Linux.Android 和 iOS 上运行(对了,还有 Windows):它可以在您最喜爱的编辑器上运行:它在一 ...

  3. WCF中DataContract和MessageContract的区别

    一.代码案例 首选建立2个WCF Service,它们分别使用不同的Contract,同时创建一个Console控制台项目,作为Client: 其中,WcfServiceWithDataContrac ...

  4. Linux学习6-Linux常用命令(2)

    目录处理命令     命令名称:mkdir 命令英文原意:make directories 命令所在路径:/bin/mkdir 执行权限:所有用户 功能描述:创建新目录 语法:mkdir -p[目录名 ...

  5. js-权威指南学习笔记15.2

    1.读取Element的innerHTML属性作为字符串标记返回那个元素的内容. 2.当设置元素的outerHTML时,元素本身被新的内容所替换.只有Element节点定义了outerHTML属性,D ...

  6. springboot项目的重定向和转发

    下面是idea软件创建的项目目录,这里总结了一下转发与重定向的问题,详解如下. 首先解释一下每个文件夹的作用,如果你是用的是idea创建的springboot项目,会在项目创建的一开始resource ...

  7. 微信小程序发布一个月,世界并没有什么不同

    从某种意义上说,在张小龙身上,最可怕的事情莫过于微信小程序发布一个月,一开始的大红大紫居然渐归沉寂,曾经的风光无限已无人谈起,世界并没有什么不同. 这真像一场噩梦,一切都可怕地颠倒了.一款微信的战略级 ...

  8. ecloipse背景修改豆沙

    Eclipse背景色的修改 Eclipse背景色的修改,修改为豆沙色  值是85 123 205 一.修改编辑区   ①这个比较简单一般都会不多说. 1.首先点击Window 然后选择Preferen ...

  9. oracle常见的等待事件说明

    转自 http://blog.itpub.net/29371470/viewspace-1063994/ 1. Buffer busy waits 从本质上讲,这个等待事件的产生仅说明了一个会话在等待 ...

  10. 在 Azure 中备份 Linux 虚拟机

    可以通过定期创建备份来保护数据. Azure 备份可创建恢复点,这些恢复点存储在异地冗余的恢复保管库中. 从恢复点还原时,可以还原整个 VM,或只是还原特定的文件. 本文介绍如何将单个文件还原到运行 ...