HDU 2697 WOJ 【DP】


传送门:HDU 2697


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

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
1
3
1
5
3
2
5
5
5

Sample Output
10




题解:
dp[i][j]代表从前i个问题中挑出且成本不超过j的最大收益。
假设以a[i]为结尾的符合条件的最长成功问题串长度为k,那么我们就要比较 dp[i-k][j-sum]+k*k和
dp[i][j]谁大;
同时还要比较不加a[i]的成本不超过j的最大收益谁大,即比较(dp[i][j]和dp[i-1][j]);




AC代码:

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;
const int N=110;
int dp[N][N];
int a[N];
int n,m,t;
int main()
{
  scanf("%d",&t);
  while(t--)
  {
    int minn=inf;
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
    {
      scanf("%d",&a[i]);
      if(a[i]<minn)
      {
        minn=a[i];
      }
    }
    if(minn>m)//如果最少花费时间的题目所需都大于总成本,则无法做任何题目
    {
      printf("0\n");
      continue;
    }
    if(minn==m)//等于总成本只能做一题
    {
      printf("1\n");
      continue;
    }
    int maxn=0;
    memset(dp,0,sizeof(dp));
    for(int i=1;i<=n;i++)
    {
      for(int j=0;j<=m;j++)
      {
        int sum=0;
        for(int k=1;k<=i;k++)
        {
          sum+=a[i-k+1];
          if(sum>j) break;
          dp[i][j]=max(dp[i][j],dp[i-k][j-sum]+k*k);
        }
        dp[i][j]=max(dp[i][j],dp[i-1][j]);
        maxn=max(maxn,dp[i][j]);
      }
    }
    printf("%d\n",maxn);
  }
  return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值