zoukankan      html  css  js  c++  java
  • TOJ 1717 WOJ

    描述

    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.

    输入

    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

    输出

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

    样例输入

    1
    10 9
    6
    1
    3
    1
    5
    3
    2
    5
    5
    5

    样例输出

    10

    题目来源

    4th Baidu Cup Central China

     

    记忆化DP,参考了大牛的代码,不过没有注释看的累。

    置底向上对问题进行求解,每一次解得

    k1:如果该层的m>=mi,则连续的层数+1。

    k2:当然也可以选择不加,那么层数变为0。

    选择k1,k2中比较大的值。

     

    代码中有两处地方:

    1)为什么是k>n返回,而不是k==n时候返回。是因为每一次的计算是下层开始计算的。

    2)为什么每次连续累加2*se-1。因为等差数列的变形 (1+2+3+4+...+n)*2-n=n*n => (1*2-1)+(2*2-1)+(3*2-1)+...+(n*2-1)

    http://blog.csdn.net/yobobobo/article/details/7657047

     

    #include <stdio.h>
    #include <string.h>
    #define MAXN 105
    
    int n,m; 
    int dp[MAXN][MAXN][MAXN];
    int mi[MAXN];
    
    int max(int a, int b){
    	if(a>b)return a;
    	else return b;
    }
    int dfs(int k, int m, int se){
    	if(dp[k][m][se])return dp[k][m][se]; 
    	if(k>n)return 0;
    	int ans,k1=0,k2=0;
    	if(m-mi[k]>=0){
    		k1=dfs(k+1,m-mi[k],se+1);
    	}
    	k2=dfs(k+1,m,0);	
    	ans=max(k1,k2);
    	if(!se){
    		return dp[k][m][se]=ans; 
    	}else{
    		return dp[k][m][se]=se*2-1+ans;
    	}
    }
    
    int main(int argc, char *argv[])
    {
    	int t;
    	scanf("%d",&t);
    	while(t--){
    		memset(dp,0,sizeof(dp));
    		scanf("%d%d",&n,&m);
    		for(int i=0; i<n; i++){
    			scanf("%d",&mi[i]);
    		}
    		dfs(0,m,0);
    		printf("%d
    ",dp[0][m][0]);
    	} 
    	return 0;
    }
    
  • 相关阅读:
    【分享】如何给视频码流添加PTS和用户自定义信息
    【分享】在MPSoC ZCU106单板的HDMI-Tx上基于eglfs_kms的运行QT应用程序
    检查Linux DRM显示设备ID的脚本
    【分享】更新的 AXI performance monitors (APM)测试工具
    window下安装clickhouse
    计算机组成原理学习
    FX DocuCentre S2110富士施乐打印机驱动
    打开网站出现Service Unavailable错误解决方法
    Java高德地图两点间距离、地理编码、逆地理编码
    IDEA运行Main报错Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.6.0:exec (default-cli) on project education: Command execution failed.
  • 原文地址:https://www.cnblogs.com/chenjianxiang/p/3532601.html
Copyright © 2011-2022 走看看