• DSY2748*音量调节


    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
     
     
    动归,答案一定在0~maxlevel之间,用f[i][j]表示调了i次后能否到达j这个音量,注意音量不能大于maxlevel,不能小于0。
     
     1 #include<cstdio>
     2 #include<iostream>
     3 #include<cstdlib>
     4 #include<cstring>
     5 using namespace std;
     6 bool f[55][2222];
     7 int c[55]={0};
     8 
     9 int main()
    10 {
    11     int n=0,bl=0,ml=0;
    12     cin>>n>>bl>>ml;
    13     for (int i=1;i<=n;++i)
    14       cin>>c[i];
    15     memset(f,false,sizeof(f));
    16     f[0][bl]=true;
    17     for (int i=1;i<=n;++i)
    18       for (int j=0;j<=ml;++j)
    19       {
    20           if (((j-c[i]>=0)&&(f[i-1][j-c[i]]==true))||((j+c[i]<=ml)&&(f[i-1][j+c[i]]==true))) //判断
    21             f[i][j]=true;
    22       }
    23     int ans=-1;
    24     for (int i=0;i<=ml;++i)
    25       if (f[n][i]==true)
    26         ans=i;
    27     cout<<ans<<endl;
    28     return 0;
    29 }

      

  • 相关阅读:
    POJ 2251 Dungeon Master(BFS)
    POJ 1321 棋盘问题 (DFS + 回溯)
    POJ 3009 Curling 2.0(DFS + 模拟)
    Codeforces 702D Road to Post Office(模拟 + 公式推导)
    Codeforces 702A Maximum Increase(dp)
    Codeforces 702C Cellular Network(二分)
    Codeforces 702B Powers of Two
    POJ 3083 Children of the Candy Corn (DFS + BFS + 模拟)
    POJ 2488 A Knight's Journey (回溯法 | DFS)
    POJ1094 Sorting It All Out (拓扑排序)
  • 原文地址:https://www.cnblogs.com/Maxxzy/p/6224599.html
Copyright © 2020-2023  润新知