↓ Skip to main content
  1. Posts/

BZOJ 2748: [HAOI2012]音量调节 (dp)

·820 words·2 mins
Note: This article is available in Chinese only. 本文暂无英文版本。 View original

2748: [HAOI2012]音量调节
#

Time Limit: 3 Sec Memory Limit: 128 MB Submit: 1814 Solved: 1148 [Submit][Status][Discuss]

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的脸…

dp[i][j]表示经过i次调整后能否达到音量j.

初始化dp[0][beginlevel] = true.

按顺序转移就好了.

复杂度O(n*MAXlevel)

代码实现
 1/* ***********************************************
 2Author :111qqz
 3Created Time :2017年04月02日 星期日 14时36分58秒
 4File Name :code/bzoj/2748.cpp
 5************************************************ */
 6
 7#include <cstdio>
 8#include <cstring>
 9#include <iostream>
10#include <algorithm>
11#include <vector>
12#include <queue>
13#include <set>
14#include <map>
15#include <string>
16#include <cmath>
17#include <cstdlib>
18#include <ctime>
19#define PB push_back
20#define fst first
21#define sec second
22#define lson l,m,rt<<1
23#define rson m+1,r,rt<<1|1
24#define ms(a,x) memset(a,x,sizeof(a))
25typedef long long LL;
26#define pi pair < int ,int >
27#define MP make_pair
28
29using namespace std;
30const double eps = 1E-8;
31const int dx4[4]={1,0,0,-1};
32const int dy4[4]={0,-1,1,0};
33const int inf = 0x3f3f3f3f;
34bool dp[51][1005]; //dp[i][j]表示经过i次操作,能否达到音量j
35int n,L,R;
36int a[1005];
37int main()
38{
39	#ifndef  ONLINE_JUDGE
40	freopen("code/in.txt","r",stdin);
41  #endif
42	ms(dp,0);
43	cin>>n>>L>>R;
44	for ( int i = 1 ; i <= n ; i++ ) cin>>a[i];
45	dp[0][L] = true;
46	for ( int i = 1 ; i <= n ; i++)
47	{
48	    for (int j = 0 ; j <= R ; j++)
49	    {
50		if (dp[i-1][j])
51		{
52		    if (j-a[i]>=0) dp[i][j-a[i]] = true;
53		    if (j+a[i]<=R) dp[i][j+a[i]] = true;
54		}
55	    }
56	}
57	int ans = -1 ; //无解输出-1.
58	for ( int i = 0 ; i <= R ; i++)if (dp[n][i]) ans = max(i,ans);
59	cout<<ans<<endl;
60  #ifndef ONLINE_JUDGE
61  fclose(stdin);
62  #endif
63    return 0;
64}

Related

BZOJ 1207: [HNOI2004]打鼹鼠 (LIS)

·965 words·2 mins
1207: [HNOI2004]打鼹鼠 # Time Limit: 10 Sec Memory Limit: 162 MB Submit: 2854 Solved: 1390 [Submit][Status][Discuss] Description # 鼹鼠是一种很喜欢挖洞的动物,但每过一定的时间,它还是喜欢把头探出到地面上来透透气的。根据这个特点阿Q编写了一个打鼹鼠的游戏:在一个nn的网格中,在某些时刻鼹鼠会在某一个网格探出头来透透气。你可以控制一个机器人来打鼹鼠,如果i时刻鼹鼠在某个网格中出现,而机器人也处于同一网格的话,那么这个鼹鼠就会被机器人打死。而机器人每一时刻只能够移动一格或停留在原地不动。机器人的移动是指从当前所处的网格移向相邻的网格,即从坐标为(i,j)的网格移向(i-1, j),(i+1, j),(i,j-1),(i,j+1)四个网格,机器人不能走出整个nn的网格。游戏开始时,你可以自由选定机器人的初始位置。现在你知道在一段时间内,鼹鼠出现的时间和地点,希望你编写一个程序使机器人在这一段时间内打死尽可能多的鼹鼠。

(dp专题006)hdu 2602 Bone Collector(01背包)

·296 words·1 min
题目链接 题意:容量为V的背包,n个骨头,给出价值和体积,问最多能装多少价值的背包。 思路:01背包裸体。 代码实现 1/* *********************************************** 2Author :111qqz 3Created Time :2016年11月16日 星期三 15时14分36秒 4File Name :code/hdu/2602.cpp 5************************************************ */ 6 7#include <cstdio> 8#include <cstring> 9#include <iostream> 10#include <algorithm> 11#include <vector> 12#include <queue> 13#include <set> 14#include <map> 15#include <string> 16#include <cmath> 17#include <cstdlib> 18#include <ctime> 19#define fst first 20#define sec second 21#define lson l,m,rt<<1 22#define rson m+1,r,rt<<1|1 23#define ms(a,x) memset(a,x,sizeof(a)) 24typedef long long LL; 25#define pi pair < int ,int > 26#define MP make_pair 27 28using namespace std; 29const double eps = 1E-8; 30const int dx4[4]={1,0,0,-1}; 31const int dy4[4]={0,-1,1,0}; 32const int inf = 0x3f3f3f3f; 33const int N=1E3+7; 34int dp[N],value[N],cost[N]; 35int n,V; 36void solve(int v,int c) 37{ 38 for ( int i = V; i >= c; i --) 39 dp[i] = max(dp[i],dp[i-c]+v); 40} 41int main() 42{ 43 #ifndef ONLINE_JUDGE 44 freopen("code/in.txt","r",stdin); 45 #endif 46 int T; 47 cin>>T; 48 while (T--) 49 { 50 scanf("%d%d",&n,&V); 51 ms(dp,0); 52 for ( int i = 1 ;i <= n ; i++) scanf("%d",&value[i]); 53 for ( int i = 1; i <= n ; i++) scanf("%d",&cost[i]); 54 for ( int i = 1 ; i <= n ; i++) solve(value[i],cost[i]); 55 int ans = 0 ; 56 57 for ( int i = 0 ; i <= V ; i++) ans = max(ans,dp[i]); 58 printf("%d\n",ans); 59 } 60 61 #ifndef ONLINE_JUDGE 62 fclose(stdin); 63 #endif 64 return 0; 65}

[dp专题005]hdu 1864最大报销额(01背包,垃圾题)

·467 words·1 min
hdu1864题目链接 题意:中文题目,不多说了。 思路:正解是01背包,呵呵呵。 出题人是傻逼吗? 不给数据范围? 以及,正解的01背包基于所有的发票额度的只有2位小数。这是让人猜? 本来看到这题这么恶心时不打算写的…

(dp专题004)hdu 2955Robberies(01背包变形)

·552 words·2 mins
题目链接 题意: 给出n个银行 ,以及抢劫每个银行可以得到的价值和被抓的概率,不同银行之间被抓的概率是相互独立的,现在给出安全概率p,只有当概率从小于安全概率时才是安全的,问最多能抢劫多少价值。

(dp专题003)hdu 4055 Number String(dp)

·799 words·2 mins
题目链接 题意:给出n(n<=1E3)个字符,字符可能为’D’,‘I’,’?’,第i位对应的字符分别表示,第i位大于第i+1位,第i位小于第i+1位,或者不确定。