3427: Poi2013 Bytecomputer

3427: Poi2013 Bytecomputer

Time Limit: 10 Sec   Memory Limit: 128 MB
Submit: 202   Solved: 116
[ Submit][ Status][ Discuss]

Description

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]这样的操作,求最少操作次数使其变成不降序列。

Input

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.

Output

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.

Sample Input

6
-1 1 0 -1 0 1

Sample Output

3
Explanation of the example: with three operations, the bytecomputer can obtain the sequence -1,-1,-1,-1,0,1

HINT

Source

[ Submit][ Status][ Discuss]

显然,最后数列中的情况一定是-1-1-1...000...111
定义f[i][j]:第i位为j的最优方案代价是多少,暴力转移即可
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<bitset>
#include<ext/pb_ds/priority_queue.hpp>
using namespace std;

const int maxn = 1E6 + 10;
const int INF = ~0U>>1;

int n,A[maxn],f[maxn][3];

int getint()
{
	char ch = getchar(); int ret = 0,a = 1;
	while (ch < '0' || '9' < ch)
	{
		if (ch == '-') a = -1;
		ch = getchar();
	}
	while ('0' <= ch && ch <= '9')
		ret = ret*10 + ch - '0',ch = getchar();
	return ret * a;
}

int main()
{
	#ifdef DMC
		freopen("DMC.txt","r",stdin);
	#endif
	
	n = getint();
	for (int i = 1; i <= n; i++) 
	{
		A[i] = getint();
		for (int j = 0; j < 3; j++) f[i][j] = INF;
	}
	f[1][A[1] + 1] = 0;
	for (int i = 2; i <= n; i++)
		for (int j = 0; j < 3; j++)
		{
			if (f[i-1][j] == INF) continue;
			for (int k = j; k < 3; k++)
			{
				if (A[i] + 1 == k) f[i][k] = min(f[i][k],f[i-1][j]);
				else if (A[i] + 1 < k && j == 2)
					f[i][k] = min(f[i][k],f[i-1][j] + k - A[i] - 1);
				else if (A[i] + 1 > k && j == 0)
					f[i][k] = min(f[i][k],f[i-1][j] + A[i] + 1 - k);
			}
		}
	int Ans = INF;
	for (int j = 0; j < 3; j++) Ans = min(Ans,f[n][j]);
	if (Ans == INF) puts("BRAK"); else cout << Ans;
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值