[ACM] HDU 5074 Hatsune Miku (簡單DP),hduhatsune
Hatsune Miku
Problem DescriptionHatsune 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.
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 a1, a2, . . . , an, is simply the sum of score(ai, ai+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?
InputThe 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, a1, a2, . . . , an (-1 ≤ ai ≤ m, ai ≠ 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.
OutputFor each test case, output the answer in one line.
Sample Input
25 383 86 7715 93 3586 92 493 3 3 1 210 536 11 68 67 2982 30 62 23 6735 29 2 22 5869 67 93 56 1142 29 73 21 19-1 -1 5 -1 4 -1 -1 -1 4 -1
Sample Output
270625
Source2014 Asia AnShan Regional Contest
解題思路:
題意為有m種音符,編號1到m,我們要用這m種音符來創造一首帶有n個音符的曲子(當然,一種音符可以用多次),假設有兩個連續的音符 i ,j ,那麼定義score(i,j)為這兩個音符的得分,題目中預先給出所有的score(i,j) 1<=i,j<=m, 那麼我們創造出的n個音符的曲子的得分為 score( note[i] , note[i+1] ) + score (note[i+1] ,note[i+2) +......score(note[n-1],note[n]) , i從1開始。 note [i] 代表n個音符的曲子中第i個音符是第幾種音符, 1<=note[i]<=m, 比如 note[i]=3,就表示n個音符中第i個位置用的是第3種音符(一共有m種),預先給出 這n個音符,note[1] 到note[n] ,其中note[ i] 或者等於-1 ,或者 大於等於1小於等於m,對於後者,該位置的音符不能改變,對於前者,該位置可以換成任意的音符j(1<=j<=m), 問 這n個音符所獲得的最大得分是多少。
雖然是簡單Dp,但還是Dp題做的少,不敏感,一開始還用暴搜來做了。。
定義dp [i ][j],為前i個音符且第i個音符為第j種音符所獲得的最大得分。
分情況討論:
當note[i] >0 (i>=2)時:
當note[i-1]>0時: dp[i][note[i]]=dp[i-1][note[i-1]]+score[note[i-1]][note[i]];
當note[i-1]<0時: dp[i][note[i]]=max(dp[i][note[i]],dp[i-1][j]+score[j][note[i]]); 1<=j<=m, 枚舉第i-1個音符是第幾種音符
當note[i]<0 (i>=2)時:
當note[i-1]>0時:dp[i][j]=max(dp[i][j],dp[i-1][note[i-1]]+score[note[i-1]][j]); 1<=j<=m, 枚舉第i個音符是第幾種音符
當note[i-1]<0時:dp[i][j]=max(dp[i][j],dp[i-1][k]+score[k][j]); 1<=j,k<=m, 枚舉第i個音符和第i-1個音符是第幾種音符
代碼:
#include <iostream>#include <string.h>#include <algorithm>using namespace std;int n,m;int score[52][52];int dp[102][52];//dp[i][j]表示為前i個音符,以第j種音符結尾所獲得的最大分數int note[102];int ans;void DP(){ memset(dp,0,sizeof(dp)); for(int i=2;i<=n;i++) { if(note[i]>0) { if(note[i-1]>0) dp[i][note[i]]=dp[i-1][note[i-1]]+score[note[i-1]][note[i]]; else { for(int j=1;j<=m;j++) dp[i][note[i]]=max(dp[i][note[i]],dp[i-1][j]+score[j][note[i]]); } } else { if(note[i-1]>0) { for(int j=1;j<=m;j++) dp[i][j]=max(dp[i][j],dp[i-1][note[i-1]]+score[note[i-1]][j]); } else { 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]+score[k][j]); } } } ans=-1; if(note[n]>0) ans=dp[n][note[n]]; else { for(int j=1;j<=m;j++) { if(ans<dp[n][j]) ans=dp[n][j]; } }}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>>score[i][j]; for(int i=1;i<=n;i++) cin>>note[i]; DP(); cout<<ans<<endl; } return 0;}
杭電1004(簡單題),為何總是wr ? http://acmhdueducn/showproblemphp?pid=10
#include<iostream>
#include<string>
using namespace std;
const int Max=1001; // 改成1001
struct Node
{
string s;
int m;
};
int main()
{
int n;
while(cin>>n&&n)
{
Node T[Max];
string str;
int i,j,flag=0,k=1;
for(i=1;i<=n;i++)
{
cin>>str;
T[i].s=str;
T[i].m=1;
for(j=1;j<i;j++)
{
if(T[j].s==str)
{
T[j].m++;
if(T[j].m>flag)
{
flag=T[j].m;
k=j;
}
}
}
}
cout<<T[k].s<<endl;
}
return 0;
}
http://acmhdueducn/showproblemphp?pid=2571命運,簡單DP,但是WA好心人幫忙下,
#include<iostream>
using namespace std;
int map[21][1001];
int sum[21][1001];
int MAX(int a,int b)
{
if(a>b)return a;
return b;
}
int main()
{
int c,i,j,n,m,max,k;
cin>>c;
while(c--)
{
memset(sum,0,sizeof(sum));
cin>>n>>m;
for(i=1;i<=n;i++)
for(j=1;j<=m;j++)
cin>>map[i][j];
sum[1][1]=map[1][1];
sum[0][1]=-1000;
sum[1][0]=-1000;
for(i=1;i<=n;i++)
sum[i][0]=-1000;
for(i=1;i<=m;i++)
sum[0][j]=-1000;
for(i=2;i<=n;i++)
sum[i][1]=sum[i-1][1]+map[i][1];
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++) // j也需要從1開始
{
if(j-1==0&&i-1==0) // 如果是左上方第一格
max=0; // max=0;
else if(j-1==0) // 如果是第一列
max=sum[i-1][j]; // max等於上面一個
else if(i-1==0) // 如果是第一行
max=sum[i][j-1]; // max等於左邊一個
else // 否則
max=MAX(sum[i][j-1],sum[i-1][j]); // max取兩者中的大值
for(k=1;k<=j/2;k++)
if(j%k==0)
max=MAX(sum[i][k],max);
sum[i][j]=max+map[i][j];
}
}
/*for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
cout<<sum[i][j]<<" ";
cout<<endl;
}*/
cout<<sum[n][m]<<endl;
}
return 0;
}...餘下全文>>