山再高,往上爬,总能登顶;
路再长,走下去,定能到达。
UPC-变音量
题目描述
你将要在元旦演奏一场吉他专场。但你不希望声音平淡,所以你希望每个曲之间都有变化。现在你已经确定了每个曲可以与上一个曲之间的音量的变化量,即每首曲开始,你可以对音量选择增加或减少一个指定的变化值。当然音量不可能为负数,也不能太高,因此必需保证每首曲音量在0和maxLevel之间(包含)。
你的任务是,根据已有的开始音量beginLevel 和每首曲之间的变化量,求出最后一首曲的最大可能音量。如果没有方案,输出 -1。
输入
第一行有三个整数,n, beginLevel, maxLevel,分别表示曲目数,开始量,最大限制音量。
下面有n-1行整数,第i行整数表示第i首曲与第i+1首曲之间的变化量。
输出
只一行一个数,答案。
Sample
Input 1
4 5 10
5
3
7
Output 1
10
扫描二维码关注公众号,回复: 10444450 查看本文章
Input 2
5 8 20
15
2
9
10
Output 2
-1
题目分析
这题的意思就是说,接下来这些音量你应该选择增还是降。有个前提是不能出现负分贝,而且不能超过最高分贝。
因为这题的数据量不大,可以直接剪枝dfs
很显然这就是一个简单的二叉树,直接01dfs走起。
如果到当前的时候的音量正好跑到当前的时候,接下来的结果一定是走完了。那么就不用继续往下走了。
好放代码
#include<unordered_map>
#include<algorithm>
#include<iostream>
#include<string.h>
#include<utility>
#include<stdio.h>
#include<vector>
#include<string>
#include<math.h>
#include<cmath>
#include<queue>
#include<stack>
#include<deque>
#include<map>
#pragma warning(disable:4244)
#define PI 3.1415926536
#pragma GCC optimize(2)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll ll_inf = 9223372036854775807;
const int int_inf = 2147483647;
const short short_inf = 32767;
const char char_inf = 127;
inline ll read() {
ll c = getchar(), Nig = 1, x = 0;
while (!isdigit(c) && c != '-')c = getchar();
if (c == '-')Nig = -1, c = getchar();
while (isdigit(c))x = ((x << 1) + (x << 3)) + (c ^ '0'), c = getchar();
return Nig * x;
}
inline void out(ll a)
{
if (a < 0)putchar('-'), a = -a;
if (a >= 10)out(a / 10);
putchar(a % 10 + '0');
}
ll qpow(ll x, ll n, ll mod) {
ll res = 1;
while (n > 0) {
if (n & 1)res = (res * x) % mod;
x = (x * x) % mod; n >>= 1;
}
return res;
}
#define read read()
int maxn, n;
int save[100000];
int ans = -1;//初始化-1,如果不发生更改证明没有改变
map<int, map<int, bool>>mp;
void dfs(int t, int num)
{
if (num<0 || num>maxn)return;
if (mp[t][num])return;
else mp[t][num] = 1;//判断当前的情况在之前有没有出现过
if (t == n)
{
ans = max(ans, num);
return;
}
dfs(t + 1, num - save[t]);
dfs(t + 1, num + save[t]);
return;
}
int main()
{
n = read;
save[0] = read;
maxn = read;
for (int i = 1; i < n; i++)save[i] = read;
dfs(1, save[0]);
cout << ans << endl;
}
DP解法
方法就是看看就是当前把所有可能性给存储一波,然后最后查询就可以了
#include<unordered_map>
#include<algorithm>
#include<iostream>
#include<string.h>
#include<utility>
#include<stdio.h>
#include<vector>
#include<string>
#include<math.h>
#include<cmath>
#include<queue>
#include<stack>
#include<deque>
#include<map>
#pragma warning(disable:4244)
#define PI 3.1415926536
#pragma GCC optimize(2)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll ll_inf = 9223372036854775807;
const int int_inf = 2147483647;
const short short_inf = 32767;
const char char_inf = 127;
inline ll read() {
ll c = getchar(), Nig = 1, x = 0;
while (!isdigit(c) && c != '-')c = getchar();
if (c == '-')Nig = -1, c = getchar();
while (isdigit(c))x = ((x << 1) + (x << 3)) + (c ^ '0'), c = getchar();
return Nig * x;
}
inline void out(ll a)
{
if (a < 0)putchar('-'), a = -a;
if (a >= 10)out(a / 10);
putchar(a % 10 + '0');
}
ll qpow(ll x, ll n, ll mod) {
ll res = 1;
while (n > 0) {
if (n & 1)res = (res * x) % mod;
x = (x * x) % mod; n >>= 1;
}
return res;
}
#define read read()
int save[10000];
int dp[1001][1001];
int main()
{
int n = read, beginl = read, maxl = read;
for (int i = 1; i < n; i++)save[i] = read;
dp[0][beginl] = 1;//第1层前即第0层的geginl已经可以了,那么就标记一下
for (int i = 1; i < n; i++)
for (int j = 0; j <= maxl; j++)
if (dp[i - 1][j])//看看当前的状态能否由上一层的某状态转移过来
{
if (j + save[i] <= maxl)dp[i][j + save[i]] = 1;//如果可,那么就标记一下当前可达到此处
if (j >= save[i])dp[i][j - save[i]] = 1;//同上
}
for (int i = maxl; i + 1; i--)//在最后一层从后往前找最大可能
if (dp[n - 1][i]) {
cout << i << endl;
return 0;
}
cout << -1 << endl;
return 0;
}
By-轮月