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]));
    }
    
  • 相关阅读:
    Spring Boot 返回 XML 数据,一分钟搞定!
    Spring Cloud Alibaba Sentinel 整合 Feign 的设计实现
    周末去面试,进去 5 分钟就出来了…
    Spring Boot 返回 JSON 数据,一分钟搞定!
    Java 11 已发布,String 还能这样玩!
    Hashtable 为什么不叫 HashTable?
    Java 中初始化 List 集合的 6 种方式!
    HashMap 和 Hashtable 的 6 个区别,最后一个没几个人知道!
    毕业不到一年,绩效打了个D!
    poj 3111 K Best (二分搜索之最大化平均值之01分数规划)
  • 原文地址:https://www.cnblogs.com/ZUTTER/p/9835805.html
Copyright © 2011-2022 走看看