P3558 [POI2013]BAJ-Bytecomputer

Posted zutter

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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


  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的主要内容,如果未能解决你的问题,请参考以下文章

[POI2013]?uk triumfalny

[POI2013]BAJ-Bytecomputer

BZOJ_3427_Poi2013 Bytecomputer_DP

BZOJ3425Poi2013 Polarization 猜结论+DP

解题:POI 2013 Triumphal arch

[POI2013]BAJ-Bytecomputer