3427: Poi2013 Bytecomputer
来源:互联网 发布:sublime text3配置php 编辑:程序博客网 时间:2024/04/20 11:38
3427: Poi2013 Bytecomputer
Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 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
-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
Explanation of the example: with three operations, the bytecomputer can obtain the sequence -1,-1,-1,-1,0,1
HINT
Source
#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 DMCfreopen("DMC.txt","r",stdin);#endifn = 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;}
0 0
- 3427: Poi2013 Bytecomputer
- BZOJ3427: Poi2013 Bytecomputer
- BZOJ3427 Poi2013 Bytecomputer
- 洛谷3558[POI2013]BAJ-Bytecomputer
- POI2013
- POI2013 题解
- [POI 2013]Bytecomputer(DP)
- 【DP】BAJ-Bytecomputer
- BZOJ3420: Poi2013 Triumphal arch
- BZOJ3419: Poi2013 Taxis
- BZOJ3424: Poi2013 Multidrink
- BZOJ3414: Poi2013 Inspector
- BZOJ3416: Poi2013 Take-out
- BZOJ3415: Poi2013 Price List
- BZOJ3421: Poi2013 Walk
- BZOJ3416 Poi2013 Take-out
- BZOJ3425 Poi2013 Polarization
- 3415: Poi2013 Price List
- JSP 页面中对Cookie的操作
- Unity中调用文件选择对话框
- #632 – 使用PreviewTextInput事件阻止用户输入(Block Input Using PreviewTextInput)
- 【深度学习:CNN】BP算法
- 自动化测试需要的环境变量
- 3427: Poi2013 Bytecomputer
- Namenode HA
- 决策树
- runOnUiThread更新主线程
- 最全ASCII对应码表-键值
- JSP学习笔记七之Cookie
- MapReduce编程之倒排索引的实现
- remusic:仿网易云音乐 安卓版
- Java设计模式之适配器模式