题目描述

A sequence of integers from the set is given.

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

incrementing by 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.

Solution

DP。

比较明显的是我们最多也只需要把一个数位加到1,最少减到-1就可以了。用$f[i][j],i\in Z,1\le i \le n,j\in Z,0 \le j \le 2 $表示第i个数的第j状态需要怎么转移过来,然后暴力枚举之前的可能情况,然后直接根据当前的情况进行转移就可以了。

Code

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <string>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
#define re register
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define ms(arr) memset(arr, 0, sizeof(arr))
const int inf = 0x3f3f3f3f;
int f[1000001][3],n,m,a[1000001],ans;
inline int read()
{
int x=0,c=1;
char ch=' ';
while((ch>'9'||ch<'0')&&ch!='-')ch=getchar();
while(ch=='-') c*=-1,ch=getchar();
while(ch<='9'&&ch>='0')x=x*10+ch-'0',ch=getchar();
return x*c;
}
int main()
{
//freopen("date.in","r",stdin);
n=read();
memset(f,126,sizeof(f));
for(re int i=1;i<=n;i++)
a[i]=read();
f[1][a[1]+1]=0;
for(re int i=2;i<=n;i++){
if(a[i]==-1){
f[i][0]=f[i-1][0];
f[i][2]=f[i-1][2]+2;
}else if(a[i]==0){
f[i][0]=f[i-1][0]+1;
f[i][1]=min(f[i-1][0],f[i-1][1]);
f[i][2]=f[i-1][2]+1;
}else{
f[i][0]=f[i-1][0]+2;
f[i][1]=f[i-1][0]+1;
f[i][2]=min(min(f[i-1][0],f[i-1][1]),f[i-1][2]);
}
}
ans=min(min(f[n][0],f[n][1]),f[n][2]);
if(ans>200000000) cout<<"BRAK";
else cout<<ans;
return 0;
}

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

  1. 【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 ...

  2. 【BZOJ】3427: Poi2013 Bytecomputer

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

  3. BZOJ3427 Poi2013 Bytecomputer

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

  4. BZOJ3427 Poi2013 Bytecomputer 【dp】

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

  5. POI2013 Bytecomputer

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

  6. POI2013题解

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

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

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

  8. [POI2013]Łuk triumfalny

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

  9. [POI2013]Polaryzacja

    [POI2013]Polaryzacja 题目大意: 给定一棵\(n(n\le250000)\)个点的树,可以对每条边定向成一个有向图,这张有向图的可达点对数为树上有路径从\(u\)到达\(v\)的点 ...

  10. [POI2013]Taksówki

    [POI2013]Taksówki 题目大意: ABC三地在同一条直线上,AC相距\(m(m\le10^{18})\)米,AB相距\(d\),B在AC之间.总共有\(n(n\le5\times10^5 ...

随机推荐

  1. darknet(yolov2)移植到caffe框架

    yolov2到caffe的移植主要分两个步骤:一.cfg,weights转换为prototxt,caffemodel1.下载源码:git clone https://github.com/marvis ...

  2. hdu4328(经典dp用悬线法求最大子矩形)

    http://wenku.baidu.com/view/728cd5126edb6f1aff001fbb.html 关于悬线法,这里面有详解. 我当时只想到了记录最大长度,却没有想到如果连最左边和最右 ...

  3. HDU 3572(Task Schedule) ISAP做法

    题目链接:传送门 题目大意:有n个任务,m个机器.每个机器最早在 Si 天开始,最晚在 Ei 天结束,需要 Pi 天完成.可以中途换机器做,也可以中途打断不做,过后再做   只要在规定时间内都行.每个 ...

  4. The Thinking of AutomaticTest(有关自动化测试的思考)

    考虑因素: 容易维护 简洁易懂 代码重用性好 系统的稳定性强 UI自动化:   数据的获取:装载的数据文件类型.数据的形式.数据的解析方法定义. 1.利用Junit单元测试组织用例,明确输入数据.预期 ...

  5. 网站漏洞扫描工具(appscan,mdcsoft-ips)

    网站漏洞扫描工具:主要应用网站漏洞扫描工具,其原理是通过工具通过对网站的代码阅读,发现其可被利用的漏洞进行告示,通过前人收集的漏洞编成数据库,根据其扫描对比做出. 具体网站扫描工具有:appscan, ...

  6. test-event-create

    # 1 创建存储过程 /* delimiter // create procedure test() begin update test SET name = date_format(now(),'% ...

  7. Squid 缓存代理服务器的完整配置

    Squid 缓存代理服务器 Squid 的作用 1.通过缓存的方式为用户提供web访问加速 2.对用户的web访问进行过滤控制 缓存代理服务器又分为普通代理服务器,透明代理服务器,和反向代理服务器. ...

  8. emo前端

    1 点击按钮可以在form中添加input控件,以name给input编号,然后点击按钮ajax上传表单,在回调函数中弹框显示结果: <form id="newfriends" ...

  9. Go语言版本的helloworld

    新建一个project,然后建立一个main目录,在main目录下新建一个go类文件:main.go 内容如下: package main import "fmt" func ma ...

  10. Redis2.8配置文件详解(转)

    add by zhj : 没找到本文的原文.另外,redis配置文件中文翻译 也翻译的不错,可以与本文对照看.两篇文章都是以Redis2.8来介绍的 在Redis中直接启动redis-server服务 ...