zoukankan      html  css  js  c++  java
  • 【BZOJ2748】音量调节(动态规划)

    【BZOJ2748】音量调节(动态规划)

    题面

    Description

    一个吉他手准备参加一场演出。他不喜欢在演出时始终使用同一个音量,所以他决定每一首歌之前他都要改变一次音量。在演出开始之前,他已经做好了一个列表,里面写着在每首歌开始之前他想要改变的音量是多少。每一次改变音量,他可以选择调高也可以调低。
    音量用一个整数描述。
    输入中给定整数beginLevel,代表吉他刚开始的音量,以及整数maxLevel,代表吉他的最大音量。音量不能小于0也不能大于maxLevel。输入文件中还给定了n个整数c1,c1,...,cn,表示在第i首歌开始之前吉他手想要改变的音量是多少。
    吉他手想以最大的音量演奏最后一首歌,你的任务是找到这个最大音量是多少。

    Input

    第一行依次为三个整数:n, beginLevel, maxlevel。
    第二行依次为n个整数:c1,c1,...,cn。

    Output

    输出演奏最后一首歌的最大音量。如果吉他手无法避免音量低于0或者高于maxLevel,输出-1。

    Sample Input

    输入1:
    3 5 10
    5 3 7

    输入2:
    4 8 20
    15 2 9 10

    Sample Output

    输出1:
    10

    输出2:
    -1

    题解

    比较简单的DP题
    (f[i][j])表示当前第(i)首歌,音量为(j)是否合法
    初始状态为(f[0][beginLevel]=1)
    转移就是每首歌调整的音量

    #include<iostream>
    #include<cstdio>
    #include<cstdlib>
    #include<cstring>
    #include<cmath>
    #include<algorithm>
    #include<set>
    #include<map>
    #include<vector>
    #include<queue>
    using namespace std;
    #define MAX 55
    inline int read()
    {
    	int x=0,t=1;char ch=getchar();
    	while((ch<'0'||ch>'9')&&ch!='-')ch=getchar();
    	if(ch=='-')t=-1,ch=getchar();
    	while(ch<='9'&&ch>='0')x=x*10+ch-48,ch=getchar();
    	return x*t;
    }
    int n,bl,ml,C[MAX];
    int f[MAX][2000];
    int main()
    {
    	n=read();bl=read();ml=read();
    	for(int i=1;i<=n;++i)C[i]=read();
    	memset(f,0,sizeof(f));
    	f[0][bl]=1;
    	for(int i=1;i<=n;++i)
    	{
    		for(int j=0;j<=ml;++j)
    		{
    			if(j-C[i]>=0)f[i][j]|=f[i-1][j-C[i]];
    			if(j+C[i]<=ml)f[i][j]|=f[i-1][j+C[i]];
    		}
    	}
    	int ans=-1;
    	for(int i=ml;i>=0;--i)
    		if(f[n][i])
    		{
    			ans=i;
    			break;
    		}
    	printf("%d
    ",ans);
    	return 0;
    }
    
    
  • 相关阅读:
    关于求 p_i != i and p_i != i+1 的方案数的思考过程
    poj 3041 Asteroids 二分图最小覆盖点
    poj 1325 Machine Schedule 最小顶点覆盖
    poj 1011 Sticks 减枝搜索
    poj 1469 COURSES 最大匹配
    zoj 1516 Uncle Tom's Inherited Land 最大独立边集合(最大匹配)
    Path Cover (路径覆盖)
    hdu 3530 SubSequence TwoPoint单调队列维护最值
    zoj 1654 Place the Rebots 最大独立集转换成二分图最大独立边(最大匹配)
    poj 1466 Girls and Boys 二分图最大独立子集
  • 原文地址:https://www.cnblogs.com/cjyyb/p/7794898.html
Copyright © 2011-2022 走看看