P3558 [POI2013]BAJ-Bytecomputer
题目描述
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
- 显然-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的更多相关文章
- Luogu P3558 [POI2013]BAJ-Bytecomputer(线性dp)
P3558 [POI2013]BAJ-Bytecomputer 题意 给一个只包含\(-1,0,1\)的数列,每次操作可以让a[i]+=a[i-1],求最少操作次数使得序列单调不降.若无解则输出BRA ...
- 【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 ...
- 【BZOJ】3427: Poi2013 Bytecomputer
题意: 给定一个长度为\(n\)的\(\{-1, 0, 1\}\)组成的序列,你可以进行\(x_i=x_i+x_{i-1}\)这样的操作,求最少操作次数使其变成不降序列.(\(n \le 100000 ...
- BZOJ3427 Poi2013 Bytecomputer
可以YY一下嘛= = 最后一定是-1, -1, ..., -1, 0, 0, ... 0, 1, 1, ..., 1的一个数列 于是f[i][j]表示到了第i个数,新数列的第j项为-1 or 0 or ...
- BZOJ3427 Poi2013 Bytecomputer 【dp】
题目链接 BZOJ3427 题解 容易发现最终序列一定是\(\{-1,0,1\}\)组成的 因为如果有一个位置不是,那么这个位置一定大于\(1\),那么上一个位置一定为\(1\),所以该位置一定加到过 ...
- POI2013 Bytecomputer
题目:http://www.lydsy.com/JudgeOnline/problem.php?id=3427 可以证明最终序列为-1...0....1 因为首先如果 a(i-1) 为-1或0,执行操 ...
- POI2013题解
POI2013题解 只做了BZ上有的\(13\)道题. 就这样还扔了两道神仙构造和一道计算几何题.所以只剩下十道题了. [BZOJ3414][Poi2013]Inspector 肯定是先二分答案,然后 ...
- bzoj 1138: [POI2009]Baj 最短回文路 dp优化
1138: [POI2009]Baj 最短回文路 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 161 Solved: 48[Submit][Sta ...
- [POI2013]Łuk triumfalny
[POI2013]Łuk triumfalny 题目大意: 一棵\(n(n\le3\times10^5)\)个结点的树,一开始\(1\)号结点为黑色.\(A\)与\(B\)进行游戏,每次\(B\)能选 ...
随机推荐
- 7行代码看EntityFramework是如何运行
这段时间在项目中运用Entity Framework作为底层数据交互框架.一个字,爽.不仅提高了开发效率,省了很多代码,而且数据库也规范了很多.按照网上的一些教程初步学习,然后实际运用了,再结合MVC ...
- javax.naming.NamingException: Cannot create resource instance报错修改
//下面内容为网上获取 avax.naming.NamingException: Cannot create resource instance at org.apache.naming.factor ...
- 【转】HttpServletRequestWrapper 实现xss注入
这里说下最近项目中我们的解决方案,主要用到commons-lang3-3.1.jar这个包的org.apache.commons.lang3.StringEscapeUtils.escapeHtml4 ...
- Spring中的Bean的配置形式
Spring中Bean的配置形式有两种,基于XML文件的方式和基于注解的方式. 1.基于XML文件的方式配置Bean <?xml version="1.0" encoding ...
- tcpcopy架构
tcpCopy 1.0 的最新架构分为三个角色: Online Server(OS):上面要部署 TCPCopy,从数据链路层(pcap 接口)抓请求数据包,发包是从IP层发出去: Test Serv ...
- Java虚拟机基础知识你知道多少?
http://www.cnblogs.com/qlky/p/7401841.html java虚拟机结构 http://liuwangshu.cn/java/jvm/1-runtime-data-ar ...
- 关于HSQLDB访问已有数据库文件的操作说明
关于HSQLDB数据库的创建,本文不做过多描述,可以在百度上搜索一下,有许多. 对于访问已存在的库文件,网上找了半天,没有整理的很清楚的参考资料,现将自己的操作过程整理如下,以供参考. 1.先下载一个 ...
- canvas框架::createjs入门
createjs是一个轻量级的框架,稍微有点时间和耐心,就可以把全部源代码都看一遍,毕竟只有三十几个js文件.地址:http://www.createjs.com/ 开发createjs的动画或游戏, ...
- replace的运用
replace() 方法用于在字符串中用一些字符替换另一些字符, 或替换一个与正则表达式匹配的子串. 语法: stringObject.replace(regexp / substr, replace ...
- CIO在数字化转型中如何正确定位?
在数字化转型的大潮下,CIO和传统企业应如何抓住数字生态系统中的机遇?CIO该如何面对领导力.资金.技术和人才的挑战? Gartner研究总监陈勇表示:IT部门在企业中应转变成为一个引领创新的部门,C ...