[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[i][j]表示序列中前i个元素,最后一个元素是数字j的最少操作次数。我们可以枚举f[i][j]转移到f[i+1][j′],枚举第i+1个元素被执行了多少次操作即可。

代码

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>

#define MAXN 1100000
#define INF 0x3f3f3f3f

using namespace std;

int f[MAXN][3],a[MAXN],n;

int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
        scanf("%d",&a[i]);
    memset(f,INF,sizeof(f));
    f[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(f[i][j]==INF) continue;
                int newj=a[i+1]+(j-1)*k;
                if(newj>=-1&&newj<=1&&newj>=(j-1))
                    f[i+1][newj+1]=min(f[i+1][newj+1],f[i][j]+k);
            }
    int ans=INF;
    for(int i=0;i<=2;i++)
        ans=min(ans,f[n][i]);
    if(ans==INF) printf("BRAK\n");
    else printf("%d\n",ans);
    return 0;
}
时间: 2024-08-01 17:25:47

[POI 2013]Bytecomputer(DP)的相关文章

解题:POI 2013 Triumphal arch

题面 1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 using namespace std; 5 const int N=300005; 6 int n,t1,t2,cnt,l,r,mid,ans; 7 int p[N],noww[2*N],goal[2*N]; 8 int son[N],dp[N]; 9 void link(int f,int t) 10 { 11 noww[++cnt]=

Uva 1331 - Minimax Triangulation(最优三角剖分 区间DP)

题目大意:按照顺时针或者逆时针的顺序给出多边的点,要将这个多边形分解成n-2个三角形,要求使得这些三角行中面积最大的三角形面积尽量小,求最小值. 思路:用区间DP可以很方便解决,多边形可能是凹边形,注意剖分的三角形必须在多边形内部,所以可以去掉剖分的三角形中包含其他点,但是其他的在多边形外部的三角形没想到其他方法去除,却ac了,不懂为何 // Accepted C++ 0.042 #include<cstdio> #include<iostream> #include<alg

树形dp①

树形dp大概是在树上的dp,可以有一些父亲节点和儿子节点的关系来dp 一道经典例题: 没有上司的舞会Ural 大学有 N 个职员,编号为 1-N.他们有从属关系,也就是说他们的关系就像一棵以校长为根的树,父结点就是子结点的直接上司.每个职员有一个快乐指数.现在有个周年庆宴会,要求与会职员的快乐指数最大.但是,没有职员愿和直接上司一起与会.tyvj1052 简单来讲就是某个职员的直接上司参加,这些职员都不会参加,发现上司和职员的关系像一颗树,可以先思考方程 少女祈祷中...... dp[i][1/

(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

Bucharest, Romania 2013 G Points DP

题意:一条线上面有n个目标,每个目标有三个值,表示不取相邻的,取一个相邻的,取两个相邻的值,问你怎么选才能最大 解题思路:每个点有个5种情况dp,dp状态转移方程在程序里, 5种情况分别是 1) 不取  2) 取自己 3) 取自己和左边,4)取自己和右边,5)取自己和左右边 解题代码: 1 Name: 12901.cpp 2 // Author: darkdream 3 // Created Time: 2014年08月16日 星期六 14时18分16秒 4 5 #include<vector>

[2013山东ACM]省赛 The number of steps (可能DP,数学期望)

The number of steps nid=24#time" style="padding-bottom:0px; margin:0px; padding-left:0px; padding-right:0px; color:rgb(83,113,197); text-decoration:none; padding-top:0px"> Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描写叙述 Mary

CUGBACM_Summer_Tranning3 2013长沙现场赛(二分+bfs模拟+DP+几何)

A题:二分 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4791 用lower_bound可以轻松解决,不过比赛的时候逗逼了. 刚开始没有预处理,所以队友给出一组数据的时候没通过,然后一时紧张又想不出什么好的解决办法,所以就没再继续敲代码.实在有点可惜了. #include<iostream> #include<cstring> #include<cstdio> #include<cmath> #includ

DP: windows 2013

题目描述 Description 话说adamyi编的Windows 2013超时了(- -!),所以他不得不在自己家门口亲眼见证这个电影般的场景.虽然他不想错过这个美妙的时刻,但是他的肚子一再抗议,要求先吃完这最后的晚餐. 现在adamyi准备依次吃n道菜.但是他只使用勺子和筷子吃,勺子和筷子可以交替使用,但是同一种菜只能用一种餐具,在开饭前他拿的是筷子.用勺子和筷子吃第i道菜的时间分别是a_i和b_i.吃第i道菜之前筷子和勺子交换的时间为c_i. 现在请你告诉adamyi按他的计划依次吃完这

HDU - 4734 F(x) (2013成都网络赛,数位DP)

题意:求0-B的满足<=F[A]的所有可能 思路:数位DP,记忆化搜索 #include <iostream> #include <cstring> #include <algorithm> #include <cstdio> using namespace std; int A, B; int dp[20][200000]; int bit[20]; int dfs(int cur, int num, int flag) { if (cur == -