题目描述

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. 【转】使用Freemarker实现网页静态化

    使用Freemarker实现网页静态化 2017年08月20日 20:45:51 阅读数:1981 1.1. 什么是freemarker FreeMarker是一个用Java语言编写的模板引擎,它基于 ...

  2. java基础之运算符与语句

    一.运算符 1.算数运算符 运算符 名称 举例 + 加法 A等于10,B等于3 则A+B=13 - 减法 A等于10,B等于3 则A-B=7 * 乘法 A等于10,B等于3 则A*B=30 / 除法 ...

  3. Intellij IDEA run coverage之覆盖率测试

    Intellij IDEA run coverage之覆盖率测试 idea 的coverage + 我们自己写的测试用例.最后看一下,我们要测的代码有没有测试到,这是一个不错的提高代码质量的方法. i ...

  4. 通过编写聊天程序来熟悉python中多线程及socket的用法

    1.引言 Python中提供了丰富的开源库,方便开发者快速就搭建好自己所需要的应用程序.本文通过编写基于tcp/ip协议的通信程序来熟悉python中socket以及多线程的使用. 2.python中 ...

  5. ci 3.0 默认路由放在子文件夹 无法访问的解决办法

      比方说你想配置默认路由为: $route['default_controller'] = 'index/home'; ci3.0之前是可以放在 controllers中的子文件夹中的,但是到了ci ...

  6. JQuery和原生JavaScript实现网页定位导航特效

    慕课网的一个小课程,练习了一遍,不足之处,欢迎指正(照片在本地,大家可以着重看代码哈): <!DOCTYPE html> <html lang="en"> ...

  7. c# 后台异步请求接口

    第一步:引用程序集:Systen.Net.Http 第一种方式: 异步 Get请求 HttpClient client = new HttpClient();            //client. ...

  8. 矩阵分解---QR正交分解,LU分解

    相关概念: 正交矩阵:若一个方阵其行与列皆为正交的单位向量,则该矩阵为正交矩阵,且该矩阵的转置和其逆相等.两个向量正交的意思是两个向量的内积为 0 正定矩阵:如果对于所有的非零实系数向量x ,都有 x ...

  9. MySQL的索引与优化

    写在前面:索引对查询的速度有着至关重要的影响,理解索引也是进行数据库性能调优的起点.考虑如下情况,假设数据库中一个表有10^6条记录,DBMS的页面大小为4K,并存储100条记录.如果没有索引,查询将 ...

  10. Android Apk增量更新

    前言 有关APK更新的技术比较多,例如:增量更新.插件式开发.热修复.RN.静默安装. 下面简单介绍一下: 什么是增量更新?   增量更新就是原有app的基础上只更新发生变化的地方,其余保持原样. 与 ...