题目描述

A sequence of N  integers I1,I2…In from the set {-1,0,1} is given. The bytecomputer is a device that allows the following operation on the sequence: incrementing I(i+1) by I(i) for any 1<=I<=N. There is no limit on the range of integers the bytecomputer can store, i.e., each I(i) 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 I1<=I2<=…I(n)) with the minimum number of operations.
给定一个{-1,0,1}组成的序列,你可以进行x[i]=x[i]+x[i-1]这样的操作,求最少操作次数使其变成不降序列。

输入

The first line of the standard input holds a single integer N(1<=N<=1000000) , the number of elements in the (bytecomputer's) input sequence.
The second line contains N  integers I1,I2…I(n) Ii from set {-1,0,1}  that are the successive elements of the (bytecomputer's) input sequence, separated by single spaces.

输出

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.

样例输入

6
-1 1 0 -1 0 1

样例输出

3


题解

显而易见,最后的数列一定只包含-1、0和1.

于是用dp。

f[i][p]表示第i个数为p-1时的最小次数。

然后判断能否改变即可。

注意不要除0,实在不行也可以用多条if else语句判断。

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int f[1000001][3] , a[1000001];
int main()
{
int n , i , j , k;
scanf("%d" , &n);
for(i = 1 ; i <= n ; i ++ )
scanf("%d" , &a[i]);
memset(f , 0x3f , sizeof(f));
f[1][a[1] + 1] = 0;
for(i = 2 ; i <= n ; i ++ )
for(j = -1 ; j <= 1 ; j ++ )
for(k = -1 ; k <= j ; k ++ )
if(j == a[i] || ((j - a[i]) * k > 0 && (j - a[i]) % k == 0))
f[i][j + 1] = min(f[i][j + 1] , f[i - 1][k + 1] + (k ? (j - a[i]) / k : 0));
i = min(f[n][0] , min(f[n][1] , f[n][2]));
if(i > 2 * n)
printf("BRAK\n");
else
printf("%d\n" , i);
return 0;
}

【bzoj3427】Poi2013 Bytecomputer dp的更多相关文章

  1. 【BZOJ3425】Poi2013 Polarization 猜结论+DP

    [BZOJ3425]Poi2013 Polarization Description 给定一棵树,可以对每条边定向成一个有向图,这张有向图的可达点对数为树上有路径从u到达v的点对(u,v)个数.求最小 ...

  2. 【BZOJ3416】Poi2013 Take-out 栈

    [BZOJ3416]Poi2013 Take-out Description 小F喜欢玩一个消除游戏——take-out 保证k+1|n,保证输入数据有解这是一个单人游戏 游戏者的目标是消除初始时给定 ...

  3. 【BZOJ3417】Poi2013 Tales of seafaring 分层图BFS

    [BZOJ3417]Poi2013 Tales of seafaring Description 一个n点m边无向图,边权均为1,有k个询问 每次询问给出(s,t,d),要求回答是否存在一条从s到t的 ...

  4. 【题解】POJ1934 Trip (DP+记录方案)

    [题解]POJ1934 Trip (DP+记录方案) 题意: 传送门 刚开始我是这么设状态的(谁叫我DP没学好) \(dp(i,j)\)表示钦定选择\(i\)和\(j\)的LCS,然而你会发现这样钦定 ...

  5. 【题解】剪纸条(dp)

    [题解]剪纸条(dp) HRBUST - 1828 网上搜不到题解?那我就来写一篇吧哈哈哈 最优化问题先考虑\(dp\),设\(dp(i)\)表示将前\(i\)个字符(包括\(i\))分割成不相交的回 ...

  6. 【题解】地精部落(DP)

    [题解]地精部落(DP) 设\(f_i\)表示强制第一个是谷的合法方案数 转移枚举一个排列的最大值在哪里,就把序列分成了互不相干的两个部分,把其中\(i-1\choose j-1\)的数字分配给前面部 ...

  7. 【BZOJ-1068】压缩 区间DP

    1068: [SCOI2007]压缩 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 1001  Solved: 615[Submit][Status][ ...

  8. 【BZOJ-1492】货币兑换Cash DP + 斜率优化 + CDQ分治

    1492: [NOI2007]货币兑换Cash Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 3396  Solved: 1434[Submit][Sta ...

  9. 【递归】油桶问题dp

    问题 : [递归]油桶问题 题目描述 楚继光扬扬得意道:“当日华山论剑,先是他用黯然销魂掌破了我的七十二路空明拳,然后我改打降龙十八掌,却不防他伸开食指和中指,竟是六脉神剑,又胜我一筹.可见天下武学彼 ...

随机推荐

  1. VS2013 执行Enable-Migrations,产生错误的解决办法

    我的错误是:提示文件或资源没有找到”Microsoft.VisualStudio.Shell, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b0 ...

  2. 洛谷P4136 谁能赢呢?

    题目描述 小明和小红经常玩一个博弈游戏.给定一个n×n的棋盘,一个石头被放在棋盘的左上角.他们轮流移动石头.每一回合,选手只能把石头向上,下,左,右四个方向移动一格,并且要求移动到的格子之前不能被访问 ...

  3. 北京Uber优步司机奖励政策(1月19日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  4. 北京Uber优步司机奖励政策(12月31日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  5. 一个例子说明substr(), mb_substr() 和 mb_strcut()之间的区别

    例子来自PHP官方文档,我只是翻译下. http://www.php.net/manual/zh/function.mb-strcut.php header( 'Content-Type:text/h ...

  6. hdu1257最少拦截系统(暴力)

    最少拦截系统 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  7. asp.net core2.1项目应用Ant Design(一)

    无意中发现了Ant Design这个组件库后,深深被他丰富的组件吸引了,大家感兴趣的可以去官网感受下,组件的应用和效果真是的太强大了,对于我们这些小公司,无自主研发前端团队的来说,无疑特别方便:htt ...

  8. Linux命令应用大词典-第29章 SELinux管理

    29.1 sestaus:显示SElinux的状态 29.2 getenforce:显示当前SELinux的应用模式 29.3 setenforce:修改SELinux的应用模式 29.4 getfa ...

  9. TPO-10 C2 Return a literature book

    TPO-10 C2 Return a literature book 第 1 段 1.Listen to a conversation between a student and an employe ...

  10. jQuery筛选器及对DOM修改(学习笔记)

    1.jQuery筛选器 注意:请先在管理Nuget程序包中查找jQuery包,并安装.也可以在jQuery官网下载. 实现: <!DOCTYPE html> <html xmlns= ...