[POI2013]BAJ-Bytecomputer

题目描述

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.

给一个只包含-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.

Solution

DP。

比较明显的是我们最多也只需要把一个数位加到1,最少减到-1就可以了。用$f[i][j],i\in Z,1\le i \le n,j\in Z,0 \le j \le 2 $表示第i个数的第j状态需要怎么转移过来,然后暴力枚举之前的可能情况,然后直接根据当前的情况进行转移就可以了。

Code

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <string>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
#define re register
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define ms(arr) memset(arr, 0, sizeof(arr))
const int inf = 0x3f3f3f3f;
int f[1000001][3],n,m,a[1000001],ans;
inline int read()
{
    int x=0,c=1;
    char ch=' ';
    while((ch>'9'||ch<'0')&&ch!='-')ch=getchar();
    while(ch=='-') c*=-1,ch=getchar();
    while(ch<='9'&&ch>='0')x=x*10+ch-'0',ch=getchar();
    return x*c;
}
int main()
{
    //freopen("date.in","r",stdin);
    n=read();
    memset(f,126,sizeof(f));
    for(re int i=1;i<=n;i++)
        a[i]=read();
    f[1][a[1]+1]=0;
    for(re int i=2;i<=n;i++){
        if(a[i]==-1){
            f[i][0]=f[i-1][0];
            f[i][2]=f[i-1][2]+2;
        }else if(a[i]==0){
            f[i][0]=f[i-1][0]+1;
            f[i][1]=min(f[i-1][0],f[i-1][1]);
            f[i][2]=f[i-1][2]+1;
        }else{
            f[i][0]=f[i-1][0]+2;
            f[i][1]=f[i-1][0]+1;
            f[i][2]=min(min(f[i-1][0],f[i-1][1]),f[i-1][2]);
        }
    }
    ans=min(min(f[n][0],f[n][1]),f[n][2]);
    if(ans>200000000) cout<<"BRAK";
    else cout<<ans;
    return 0;
}

原文地址:https://www.cnblogs.com/victorique/p/8877070.html

时间: 2024-08-02 23:32:34

[POI2013]BAJ-Bytecomputer的相关文章

POI2013 Bytecomputer

题目:http://www.lydsy.com/JudgeOnline/problem.php?id=3427 可以证明最终序列为-1...0....1 因为首先如果 a(i-1) 为-1或0,执行操作不会让答案变优. 然后,如果可以加到大于1的某个数字,一定可以加到1,显然加到1更佳. 然后简单dp,f[i][j]表示第i位为j的最少步数. #include <cstdio> #include <cstring> #include <algorithm> #defin

BZOJ3427 Poi2013 Bytecomputer

可以YY一下嘛= = 最后一定是-1, -1, ..., -1, 0, 0, ... 0, 1, 1, ..., 1的一个数列 于是f[i][j]表示到了第i个数,新数列的第j项为-1 or 0 or 1的的最小代价 然后就没有然后了 1 /************************************************************** 2 Problem: 3427 3 User: rausen 4 Language: C++ 5 Result: Accepted

bzoj 1138: [POI2009]Baj 最短回文路 dp优化

1138: [POI2009]Baj 最短回文路 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 161  Solved: 48[Submit][Status] Description N个点用M条有向边连接,每条边标有一个小写字母. 对于一个长度为D的顶点序列,回答每对相邻顶点Si到Si+1的最短回文路径. 如果没有,输出-1. 如果有,输出最短长度以及这个字符串. Input 第一行正整数N和M ( 2 ≤ N ≤ 400 , 1 ≤ M ≤

[POI 2013]Bytecomputer(DP)

题目链接 http://main.edu.pl/en/archive/oi/20/baj 题目大意 给你一个长度为n的序列a,序列里每个元素要么是0,要么是-1,要么是1,每次操作可以让a[x]=a[x]+a[x?1],问至少要做多少次操作,才能让整个序列变成非降序列 思路 可以发现,最终的序列是一定是-1 -1 -1--1 -1 -1 0 0 0-0 0 0 1 1 1-1 1 1的形式,肯定没有2或者更大的数字,因为出现这样大的数字是毫无必要的,会增加操作次数.那么可以通过DP解决此题,用f

(DP) POI Bytecomputer

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 bytecomput

【BZOJ3425】Poi2013 Polarization 猜结论+DP

[BZOJ3425]Poi2013 Polarization Description 给定一棵树,可以对每条边定向成一个有向图,这张有向图的可达点对数为树上有路径从u到达v的点对(u,v)个数.求最小可达点对数和最大可达点对数 n<=250000 Sample Input 4 1 2 1 3 1 4 Sample Output 3 5 题解:想了一晚上,怎么想怎么是个搭建双塔,结果看题解发现还真tm是搭建双塔. 本题的结论有点神,不过很好猜,证明见Claris博客. 第一问的答案一定是n-1,因

【BZOJ3417】Poi2013 Tales of seafaring 分层图BFS

[BZOJ3417]Poi2013 Tales of seafaring Description 一个n点m边无向图,边权均为1,有k个询问 每次询问给出(s,t,d),要求回答是否存在一条从s到t的路径,长度为d 路径不必是简单路(可以自交) 2<=N<=5000,1<=M<=5000,1<=K<=1000000,1<=d<=1000000000 Sample Input 8 7 4 1 2 2 3 3 4 5 6 6 7 7 8 8 5 2 3 1 1

[POI2013]?uk triumfalny

[POI2013]?uk triumfalny 题目大意: 一棵\(n(n\le3\times10^5)\)个结点的树,一开始\(1\)号结点为黑色.\(A\)与\(B\)进行游戏,每次\(B\)能选择不超过\(k\)个结点染成黑色,然后\(A\)从当前点出发走到一个相邻的结点.若\(A\)从\(1\)号结点出发,则\(k\)最小取多少能保证\(A\)每次走到的点都是黑点? 思路: 二分答案\(k\)后使用树形DP判断是否可行. 从叶子往根考虑,\(f_i\)表示将\(i\)的子树全部染黑需要从

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 ar