音量を変更しますUPC-

どんなに高い山、登る、常に登る;
道路は、上に行く、長くなることが到達することができるようになります。

音量を変更しますUPC-

タイトル説明

あなたは、新年にギターのコンサートを演奏することになるでしょう。しかし、あなたはそれぞれの曲間の変化を持たせたいので、鈍い音にしたくありません。今、あなたが開始あたりの最初の曲、歌にすることができ、各曲の間の体積の変化量を決定したことを、あなたは価値の所与の変化のために音量を増減するかを選択できます。ボリュームがない高い、負ではないようであるもちろん、最初の蛇行及びmaxLevel 0の各々との間の容積を(含む)ことを保証する必要があります。
あなたの仕事は、各ボリュームbeginLevelと最初の曲の開始との間の変化の存在量に基づいて、可能な最大音量最後の曲を見つけています。いいえプログラム、-1の出力の場合。

エントリー

最初の行は三つの整数、nは、beginLevel、maxLevel、それぞれ、トラックの開始を表す最大音量の制限を含んでいます。
n-1の整数行の下に、i行目のi番目の第I + 1-湾曲した第1の曲間の変動の整数を表します。

輸出

行番号、答えは1つだけ。

サンプル
入力1

4 5 10
5
3
7

出力1

10

入力2

5 8 20
15
2
9
10

出力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;
}

バイ・ホイール月

公開された32元の記事 ウォン称賛10 ビュー1171

おすすめ

転載: blog.csdn.net/qq_35339563/article/details/104954084
おすすめ