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\)能选 ...
随机推荐
- MyBatis源码解析之日志记录
一 .概述 MyBatis没有提供日志的实现类,需要接入第三方的日志组件,但第三方日志组件都有各自的Log级别,且各不相同,但MyBatis统一提供了trace.debug.warn.error四个级 ...
- Java的工厂模式(二)
除了上文提到的方法之外,还可以使用Java的反射机制,这样就能使用类名称来加载所需要的类.我们只需改变工厂类和驱动类就可以了. FruitFactory.java package com.muggle ...
- 使用css实现炫酷的横屏滚动效果
炫酷的横屏滚动效果css实现 DEMO: https://codepen.io/kobako/pen/BxVLLm 我们对滚动条都不陌生.平时浏览的网页,进度条通常是垂直方向的,内容从上往下排列.但是 ...
- CSS3 linear-gradient线性渐变实现虚线等简单实用图形
一.作为图片存在的CSS3 gradient渐变 我觉得CSS3 Backgrounds比较厉害的一个地方就是支持多背景,也就是背景图片个数可以无限累加,正好CSS3的gradient渐变性质是bac ...
- iReport(模版) 与Jasper(数据填充)生成pdf文档
报表模板生成软件:iReport . 润乾.水晶. 一.Jaspersoft iReport Desiginer 5.60 的使用 1.软件jar包的下载地址与配置 百度云盘下载链接:https:// ...
- BZOJ1935: [Shoi2007]Tree 园丁的烦恼(树状数组 二维数点)
题意 题目链接 Sol 二维数点板子题 首先把询问拆成四个矩形 然后离散化+树状数组统计就可以了 // luogu-judger-enable-o2 #include<bits/stdc++.h ...
- 线上Bug修复流程
- 获取本地IP地址的vc代码
作者:朱金灿 来源:http://blog.csdn.net/clever101 获取本地IP地址有两种做法.一种是使用gethostname函数,代码如下: bool CSocketComm::Ge ...
- Mac下git的环境搭建和基本使用
前言本文将介绍git的基本概念.环境搭建.日常使用,主要针对刚接触git,或接触不久,或好久没用忘记的同学们,当然是基于mac环境的,window系统也是大同小异!本文将从以下几个模块介绍,希望能帮助 ...
- 网络 TCP三次握手及滑动窗口
三次握手客户端向服务器发出触发请求syn=1:因为这时还没有得到服务器的回应,所以ack=0服务器接收到客户端的触发请求,回复ack=1,表示已经接收到客户端的请求:同时服务器也向客户端发出触发请求, ...