题目描述

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. Spring界的HelloWorld

    1.Spring依赖的jar包下载网址: https://repo.spring.io/webapp/#/artifacts/browse/tree/General/libs-release-loca ...

  2. spring Boot的配置

    一.配置文件 SpringBoot使用一个全局的配置文件,配置文件名是固定的: application.properties application.yml 配置文件的作用:修改SpringBoot自 ...

  3. HTTP协议之内容协商

    一个URL常常需要代表若干不同的资源.例如那种需要以多种语言提供其内容的网站站点.如果某个站点有说法语的和说英语的两种用户,它可能想用这两种语言提供网站站点信息.理想情况下,服务器应当向英语用户发送英 ...

  4. 动态We API层(动态生成js)

    ABP动态webapi前端怎么调用? 研究abp项目时,页面js文件中一直不明白abp.services... 是从哪里来的 在调试SimpleTaskSystem的AngularJs demo时,一 ...

  5. MySql基本学习知识点:

    1.Mysql的简介: (1):常识: MySQL是一种关系数据库管理系统,是一种开源软件 由瑞典MySQL AB公司开发,2008年1月16号被Sun公司收购.2009年,SUN又被Oracle收购 ...

  6. 【代码笔记】iOS-iphone开发之获取系统字体

    代码: - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. NS ...

  7. druapl7:"Notice: A non well formed numeric value encountered 在 _hierarchical_select_hierarchy_generate() "

    这个是很诡异的一个Notice错误提醒,因为我在Drupal7.54+PHP7.0.1的环境下,并没有报这个错.但是我再Drupal7.59+PHP7.1.7的环境下就报错了.很奇怪,按照报错信息bi ...

  8. 在已配置成功的opencv3.2.0下配置opencv_contrib模块

    简介: 之前在Ubuntu下配置OpenCV时,因为对opencv3..0不是特别了解,没有把opencv_contrib进行安装,这里提醒大家尽量要一次性安装完毕,减少不必要的麻烦. .0文件夹 ( ...

  9. Web 系统架构一般组成

    负载层技术       负载分配层,是单指利用软件实现的计算机系统上的狭义负载均衡.它是根据业务形态设计一种架构方式,将来自外部客户端的业务请求分担到每一个可用的业务节点上 . 1.用户终端不只包括类 ...

  10. sql 去重关键字 distinct

    单列去重: mysql: drop table test;create table test(id int(4));insert into test values(1),(2),(3),(4),(1) ...