• 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]));
    }
    
  • 相关阅读:
    使用python-docx生成Word文档
    python 日期格式转换
    Android开发:关于WebView
    用 jQuery.ajaxSetup 实现对请求和响应数据的过滤
    Mysql 命令大全
    旋转木马的小效果!
    CSS3 background-image背景图片相关介绍
    PHP的高效IOC框架——CanoeDI
    CSS3与页面布局学习总结(一)——概要、选择器、特殊性与刻度单位
    PHP入门介绍与环境配置
  • 原文地址:https://www.cnblogs.com/ZUTTER/p/9835805.html
Copyright © 2020-2023  润新知