码迷,mamicode.com
首页 > 其他好文 > 详细

(DP) POI Bytecomputer

时间:2015-05-20 16:18:44      阅读:148      评论:0      收藏:0      [点我收藏+]

标签:

Bytecomputer

Memory limit: 128 MB

A sequence of 技术分享 integers 技术分享 from the set 技术分享 is given. The bytecomputer is a device that allows the following operation on the sequence: incrementing 技术分享 by 技术分享 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.

Input

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 技术分享.

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.

Example

For the input data:

6
-1 1 0 -1 0 1

the correct result is:

3

Explanation of the example: with three operations, the bytecomputer can obtain the sequence 技术分享.

Sample grading tests:

  • 0grade技术分享, a small test with the answer BRAK;
  • 1grade技术分享, a small test with the answer 技术分享;
  • 2grade技术分享, all the elements in the sequence equal 技术分享;
  • 3grade技术分享技术分享技术分享技术分享;
  • 4grade技术分享技术分享技术分享技术分享 and 技术分享.

Task author: Jacek Tomasiewicz.

 

题意:

a[x]=a[x-1]+a[x]要让序列递增

 

那么无非是 -1 -1 -1 -1 0 0 0  0 0  0 0 1 1 1 1 1 1 11 11 1 1

 

f[i][j]填写到i个数字这个数字为j然后转移就好了

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
#include<cstdlib>
#include<queue>
#include<vector>
#include<stack>
#define INF 100000000
using namespace std;
int dp[1000005][3],n,a[1000005];
int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
        scanf("%d",&a[i]);
    for(int i=1;i<=n;i++)
    {
        for(int j=0;j<=2;j++)
            dp[i][j]=INF;
    }
    dp[1][a[1]+1]=0;
    for(int i=1;i<n;i++)
    {
        for(int j=0;j<=2;j++)
        {
            for(int k=0;k<=2;k++)
            {
                if(dp[i][j]==INF)
                    continue;
                int newj;
                newj=a[i+1]+(j-1)*k;
                if(newj>=-1&&newj<=1&&newj>=(j-1))
                    dp[i+1][newj+1]=min(dp[i+1][newj+1],dp[i][j]+k);
            }
        }
    }
    int ans=INF;
    for(int i=0;i<=2;i++)
        ans=min(ans,dp[n][i]);
    if(ans==INF)
        printf("BRAK\n");
    else
        printf("%d\n",ans);
    return 0;
}

  

(DP) POI Bytecomputer

标签:

原文地址:http://www.cnblogs.com/water-full/p/4517405.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!