首页 > 其他分享 >hdu 5074(简单dp)

hdu 5074(简单dp)

时间:2023-05-29 22:31:46浏览次数:43  
标签:hdu song int beautifulness notes score dp 5074


Hatsune Miku


Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)




Problem Description


Hatsune Miku is a popular virtual singer. It is very popular in both Japan and China. Basically it is a computer software that allows you to compose a song on your own using the vocal package.


Today you want to compose a song, which is just a sequence of notes. There are only m different notes provided in the package. And you want to make a song with n notes.





hdu 5074(简单dp)_dp


Also, you know that there is a system to evaluate the beautifulness of a song. For each two consecutive notes a and b, if b comes after a, then the beautifulness for these two notes is evaluated as score(a, b).



So the total beautifulness for a song consisting of notes a

1, a

2, . . . , a

n, is simply the sum of score(a

i, a

i+1) for 1 ≤ i ≤ n - 1.



Now, you find that at some positions, the notes have to be some specific ones, but at other positions you can decide what notes to use. You want to maximize your song’s beautifulness. What is the maximum beautifulness you can achieve?


 



Input


The first line contains an integer T (T ≤ 10), denoting the number of the test cases.

For each test case, the first line contains two integers n(1 ≤ n ≤ 100) and m(1 ≤ m ≤ 50) as mentioned above. Then m lines follow, each of them consisting of m space-separated integers, the j-th integer in the i-th line for score(i, j)( 0 ≤ score(i, j) ≤ 100). The next line contains n integers, a 1, a 2, . . . , a n (-1 ≤ a i ≤ m, a i ≠ 0), where positive integers stand for the notes you cannot change, while negative integers are what you can replace with arbitrary notes. The notes are named from 1 to m.


 



Output


For each test case, output the answer in one line.


 



Sample Input


2 5 3 83 86 77 15 93 35 86 92 49 3 3 3 1 2 10 5 36 11 68 67 29 82 30 62 23 67 35 29 2 22 58 69 67 93 56 11 42 29 73 21 19 -1 -1 5 -1 4 -1 -1 -1 4 -1


 



Sample Output


270 625


 



解题思路:这道题目就要把-1的空填满,然后使得总分最大。挺简单的一个dp问题,直接dp[i][j]表示第i位上使用第j个音符所得到的前i个音符总和最大。因为第i位的音符只和第i-1位的音符有关,所以dp[i][j] = max{dp[i-1][k] + score[k][j]},然后一些音符固定的地方记得分类讨论下就ok!!总的来说是到很基础的dp



AC:


#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;

const int maxn = 55;
int n,m,a[maxn][maxn];
int note[maxn<<1],dp[maxn<<1][maxn];

int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		cin>>n>>m;
		for(int i = 1; i <= m; i++)
			for(int j = 1; j <= m; j++)
				cin>>a[i][j];
		for(int i = 1; i <= n; i++)
			cin>>note[i];
		memset(dp,0,sizeof(dp));
		for(int i = 2; i <= n; i++)
		{
			if(note[i] == -1)
			{
				if(note[i-1] == -1)
				{
					for(int j = 1; j <= m; j++)
						for(int k = 1; k <= m; k++)
						{
							dp[i][j] = max(dp[i][j],dp[i-1][k] + a[k][j]);
						}
				}
				else
				{
					for(int j = 1; j <= m; j++)
						dp[i][j] = max(dp[i][j],dp[i-1][note[i-1]] + a[note[i-1]][j]);
				}
			}
			else
			{
				if(note[i-1] == -1)
				{
					for(int j = 1; j <= m; j++)
					{
						dp[i][note[i]] = max(dp[i][note[i]],dp[i-1][j] + a[j][note[i]]);
					}
				}
				else
				{
					dp[i][note[i]] = dp[i-1][note[i-1]] + a[note[i-1]][note[i]];
				}
			}
		}
		int ans = 0;
		for(int i = 1; i <= m; i++)
			ans = max(ans,dp[n][i]);
		cout<<ans<<endl;
	}
	return 0;
}



标签:hdu,song,int,beautifulness,notes,score,dp,5074
From: https://blog.51cto.com/u_16143128/6374353

相关文章

  • 花店橱窗布置(dp)
    题目大意:n束花和m个花瓶(m>=n),一个花瓶最多放入一束花,每束花放入各个花瓶会产生对应的观赏值,要求n束花都必须按给出的顺序从左到右放入花瓶中,求能产生的最大观赏值和相应方案思路过程:1.先考虑求最大观赏值,用dp[i][j]来表示到第i个花瓶时放入第j束花能产生的最大观赏值,......
  • 【Socket】基于UDP的发送端和接收端
    UDP和TCP的差异UDP相比TCP,无需在连接状态下交换数据,因此UDP的server端和client端无需经过连接过程,即不必调用listen()和accept()函数。UDP中只有创建套接字和数据交换的过程。基于UDP的接收和发送函数当创建好TCP套接字后,传输数据时无需再添加地址信息,因此TCP套接字会保持与对......
  • 渗透---WordPress网站
    WordPress简介WordPress是使用PHP语言开发的博客平台,用户可以在支持PHP和MySQL数据库的服务器上架设属于自己的网站。也可以把WordPress当作一个内容管理系统(CMS)来使用。WordPress是一款个人博客系统,并逐步演化成一款内容管理系统软件,它是使用PHP语言和MySQL数据库开发的,用......
  • 渗透--WordPress网站
    WordPress简介WordPress是使用PHP语言开发的博客平台,用户可以在支持PHP和MySQL数据库的服务器上架设属于自己的网站。也可以把WordPress当作一个内容管理系统(CMS)来使用。WordPress是一款个人博客系统,并逐步演化成一款内容管理系统软件,它是使用PHP语言和MySQL数据库开发的,用......
  • hdu 3303(线段树+抽屉原理)
    解题思路:这题利用了抽屉原理,即1-M之间的所有数与M+1的模都不相同。那么可以利用它将要查找所有区间分成[1,Y-1],[Y,2*Y-1],[2*Y,3*Y-1].........一直下去,直到所有的区间都被找一遍,然后就是保存最小的模即可。。。这样就肯定要找每个区间的最小的模,实际上这里可以利用线段树去找,只需......
  • hdu 4417(树状数组+离线算法)
    解题思路:这道题要求某区间内比h小的个数,其实这里可以类似于树状数组求逆序数那样。关键是如何转换成树状数组的模型,这才是本题的难点。我们首先分析,如果知道h在该区间的哪个位置,那么剩下的就很好做了。我们还可以发现,如果找到了当前的比h小的所有点(大于的点我们先忽略掉),那么我们就......
  • hdu 3681(bfs+dfs+状态压缩)
    解题思路:这道题属于图上来回走的问题,可以把重复走的过程弱化,即只强调从u->v的结果,中间经过的节点都不考虑。这道题里面'G','F','Y'是重要的节点,其余的点我们是可以忽略的,也就是说,假设从u->v,我们只需要知道最短路径是多少就可以了,至于是怎么走的,中间绕过了多少个'D'都不是我们关心的......
  • hdu 4012(bfs+位压缩)
    思路:每次涂色以后必有一个格子的颜色是最终的颜色,否则这次涂色根本没意义,所以我们把每次涂色后哪些格子成为最终颜色的所有可能都入队,入队的元素是一个struct包含步数和最终颜色已确定的木块集合,这个集合必须用整数表示,所以用到状态压缩,因为最多只有16个格子,所以用16位的二进制来表......
  • hdu 1593(数学)
    往相反的方面跑,但是,最理想的初始位置并不是圆点和圆上的某一点,应该还有更理想的初始逃跑状态.这里有一点需要注意,就是逃跑者极力想达到理想逃跑初态,而追赶者极力阻止逃跑者达到这一状态,所以,理想初态应该是无论追赶者如何阻止,逃跑者仍然可以达到的理想状态.最理想的......
  • nyoj 304(区间dp)
    解题思路:这道题很明显是用区间dp,可是与以往的区间dp不同,因为对于区间[i,j],机器人所处的位置要么在i,要么在j(因为机器人要移动到某一点才能关闭灯泡,所以对于某一段区间来说,机器人最后肯定在两个端点上,否则将不能成立),那么既然要表示在左端点还是右端点,所以我们再开三维数组dp[i][j][0]......