hdu2697

来源:互联网 发布:程序员真的每天很累吗 编辑:程序博客网 时间:2024/05/29 03:52

WOJ

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 197    Accepted Submission(s): 89


Problem Description
Alex likes solving problems on WOJ (http://acm.whu.edu.cn/oak). As we all know, a beautiful balloon will appears when a
problem is solved. Alex loves balloons, especially when they're in a consecutive column, from which he can get a sense of
accomplishment.

Problems on WOJ have a variety of difficulties. Hard problems cost more time, while easy ones may be "killed in 1
second". Now we know that WOJ has N problems, numbered from 1 to N. Alex calls the solved problems in one
consecutive column as a "successful string". The length of a successful string is the number of problems it contains. Also
he defines the value of a successful string as the square of the string's length. Alex hopes to solve a certain number of
problems in M time, so he can get some successful strings, now he wants to maximize the sum of all the strings' value.
 

Input
The input consists of multiple test cases. The first line of input contains an integer T, which is the number of test cases.

The input consists of several test cases. Each test case starts with a line containing two integers N and M.
Each of the following N lines contains a single integer Mi indicating the time cost on solving the i-th problem.
(1<=N, M<=100)

[Technical Specification]
T is an integer, and T <= 15;
N and M are integers, 1 <= N, M <= 100.
Mi are integers and, 0 <= Mi <= 100
 

Output
For each test case, print a single line containing a number indicating the maximum sum.
 

Sample Input
1 10 9 6 13 1 5 3 2 5 5 5
 

Sample Output

10

AC代码:

#include<cstdio>#include<cstring>#include<algorithm>#include<iostream>using namespace std;const int N =110;int a[N];int dp[N][N];int main(){        int t;        int n,m;        scanf("%d",&t);        while(t--)        {                scanf("%d%d",&n,&m);                for(int i=1;i<=n;i++)                {                        scanf("%d",&a[i]);                }                memset(dp,0,sizeof(dp));                for(int i=1;i<=n;i++)                {                        for(int j=0;j<=m;j++)                        {                                int cnt=0;                                int sum=0;                                dp[i][j]=dp[i-1][j];                                for(int h=i;h>=1;h--)                                {                                        cnt++;                                        sum+=a[h];                                        if(sum>j)                                                break;                                        dp[i][j]=max(dp[i][j],dp[h-1][j-sum]+cnt*cnt);                                }                        }                }                printf("%d\n",dp[n][m]);        }        return 0;}