zoukankan      html  css  js  c++  java
  • 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]));
    }
    
  • 相关阅读:
    代腾飞(为自己名字作诗)
    愈到老愈受用:人生成功生涯规划100诀
    漫步白沫江畔有感
    成功自有成功的方法和道理(一堂价值百万的课)
    你的终点在哪里?决定成功与否的十种智力
    为你痴狂
    苏竹青(帮别人名字作诗)
    何韵仪(帮别人名字作诗)
    腹有诗书又如何
    漫步茶马古道有感
  • 原文地址:https://www.cnblogs.com/ZUTTER/p/9835805.html
Copyright © 2011-2022 走看看