zoukankan      html  css  js  c++  java
  • bzoj2748 [HAOI2012]音量调节

    Description

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

    Input

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

    Output

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

    Sample Input

    3 5 10
    5 3 7

    Sample Output

    10

    HINT 

    1<=N<=50,1<=Ci<=Maxlevel 1<=maxlevel<=1000
    0<=beginlevel<=maxlevel
     

    正解:dp。

    对于i,枚举每种可能的音量j,然后它必定是从j+c[i]或j-c[i]转移过来。那就或一下,最后从大往小扫,判断合法就行了。

     1 //It is made by wfj_2048~
     2 #include <algorithm>
     3 #include <iostream>
     4 #include <cstring>
     5 #include <cstdlib>
     6 #include <cstdio>
     7 #include <vector>
     8 #include <cmath>
     9 #include <queue>
    10 #include <stack>
    11 #include <map>
    12 #include <set>
    13 #define inf (1<<30)
    14 #define il inline
    15 #define RG register
    16 #define ll long long
    17 #define File(s) freopen(s".in","r",stdin),freopen(s".out","w",stdout)
    18 
    19 using namespace std;
    20 
    21 int f[60][1010],c[60],n,bg,maxl,ans;
    22 
    23 il int gi(){
    24     RG int x=0,q=1; RG char ch=getchar();
    25     while ((ch<'0' || ch>'9') && ch!='-') ch=getchar();
    26     if (ch=='-') q=-1,ch=getchar();
    27     while (ch>='0' && ch<='9') x=x*10+ch-48,ch=getchar();
    28     return q*x;
    29 }
    30 
    31 il void work(){
    32     n=gi(),bg=gi(),maxl=gi(); f[0][bg]=1;
    33     for (RG int i=1;i<=n;++i) c[i]=gi();
    34     for (RG int i=1;i<=n;++i)
    35     for (RG int j=0;j<=maxl;++j){
    36         if (j-c[i]>=0) f[i][j]|=f[i-1][j-c[i]];
    37         if (j+c[i]<=maxl) f[i][j]|=f[i-1][j+c[i]];
    38     }
    39     ans=maxl; while (ans && !f[n][ans]) ans--;
    40     printf("%d
    ",f[n][ans] ? ans : -1); return;
    41 }
    42 
    43 int main(){
    44     File("level");
    45     work();
    46     return 0;
    47 }
  • 相关阅读:
    [数据结构]图的DFS和BFS的两种实现方式
    [算法]两个栈实现一个队列
    [数据结构]手动实现队列
    [数据结构]手动实现栈
    [数据结构]手动实现单链表
    Hive分组取Top K数据
    HBase解决海量图片存储方案
    非结构化数据存储方案
    头条面试题之实现两个线程轮流打印字符串
    [算法]最大连续子数组和,最长重复子串,最长无重复字符子串
  • 原文地址:https://www.cnblogs.com/wfj2048/p/6416560.html
Copyright © 2011-2022 走看看