zoukankan      html  css  js  c++  java
  • 动态规划

    目录

     

    1003.Max Sum

    1087.Super Jumping! Jumping! Jumping!

    1159.Common Subsequence

    1176.免费馅饼


    1003.Max Sum

    Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max sum of a sub-sequence. For example, given (6,-1,5,4,-7), the max sum in this sequence is 6 + (-1) + 5 + 4 = 14.

    Input

    The first line of the input contains an integer T(1<=T<=20) which means the number of test cases. Then T lines follow, each line starts with a number N(1<=N<=100000), then N integers followed(all the integers are between -1000 and 1000).

    Output

    For each test case, you should output two lines. The first line is "Case #:", # means the number of the test case. The second line contains three integers, the Max Sum in the sequence, the start position of the sub-sequence, the end position of the sub-sequence. If there are more than one result, output the first one. Output a blank line between two cases.

    Sample Input

    2 5 6 -1 5 4 -7 7 0 6 -1 1 -6 7 -5

    Sample Output

    Case 1: 14 1 4 Case 2: 7 1 6

    解析:用数组a[]记录序列中的数,对于a[i]只有两种可能   1.为一个序列的首;2.为一个序列的尾;用数组d[i]记录以第i个数结尾的序列的最大和,则
    d[i]=max(d[i-1]+a[i],a[i]),d[i-1]+a[i]和a[i]分别对应a[i]的两种情况。
    #include<iostream>
    #include<cstdio>
    using namespace std;
    int a[100000+11];
    int d[100000+11];
    int main()
    {
        int n;
        cin>>n;
        int k=0;
        while(n--){
            int sum=0;
            int t,begin,end;
            int max0=-1001;
            cin>>t;
            for(int i=1;i<=t;i++)
            {
                scanf("%d",&a[i]);
                d[i]=max(d[i-1]+a[i],a[i]);
                if(max0<d[i])
                {
                    max0=d[i];
                    end=i;
                }
            }
            for(int i=end;i>=1;i--)
            {
                sum+=a[i];
                if(sum==max0)
                    begin=i;
            }
            printf("Case %d:
    ",++k);
            printf("%d %d %d
    ",max0,begin,end);
            if(n)
                cout<<endl;
        }
    
    }
    

    1087.Super Jumping! Jumping! Jumping!

    Nowadays, a kind of chess game called “Super Jumping! Jumping! Jumping!” is very popular in HDU. Maybe you are a good boy, and know little about this game, so I introduce it to you now.


    The game can be played by two or more than two players. It consists of a chessboard(棋盘)and some chessmen(棋子), and all chessmen are marked by a positive integer or “start” or “end”. The player starts from start-point and must jumps into end-point finally. In the course of jumping, the player will visit the chessmen in the path, but everyone must jumps from one chessman to another absolutely bigger (you can assume start-point is a minimum and end-point is a maximum.). And all players cannot go backwards. One jumping can go from a chessman to next, also can go across many chessmen, and even you can straightly get to end-point from start-point. Of course you get zero point in this situation. A player is a winner if and only if he can get a bigger score according to his jumping solution. Note that your score comes from the sum of value on the chessmen in you jumping path.
    Your task is to output the maximum value according to the given chessmen list.

    Input

    Input contains multiple test cases. Each test case is described in a line as follow:
    N value_1 value_2 …value_N 
    It is guarantied that N is not more than 1000 and all value_i are in the range of 32-int.
    A test case starting with 0 terminates the input and this test case is not to be processed.

    Output

    For each case, print the maximum according to rules, and one line one case.

    Sample Input

    3 1 3 2 4 1 2 3 4 4 3 3 2 1 0

    Sample Output

    4 10 3

    题目思路:dp数组表示是包含当前这个数的最大递增子序列和。dp[i]表示的是前i个并且包含第i个的最大递增子序列和;给个数据:3 1 4 显然dp[1]=3,dp[2]=1表示两个数的最大值。因为分两种情况讨论,如果第二个数大于第一个数,就加上,即dp[2]=dp[1]+num[2];如果不大,dp[2]=num[2];dp[3]=7表示三个数的最大值。首先比较num[3]和num[1],如果num[3]>num[1],dp[3]=7先存下来,如果num[3]>num[2],dp[3]=5依旧存下来;还有一种如果num[3]比前两个值都小,dp[3]=num[3];最后在存下来的dp[3]中找到一个最大的!

    #include<iostream>
    #include<cstring>
    #include<cstdio>
    using namespace std;
    int num[1001];
    int dp[1001];
    int main()
    {
        int n,Max;
        while(cin>>n)
        {
            if(n==0) break;
            Max=0;
            memset(dp,0,sizeof(dp));
            for(int i=0;i<n;i++)
                cin>>num[i];
            dp[0]=num[0];
            for(int i=1;i<n;i++){
                for(int j=0;j<i;j++)
                {
                    if(num[i]>num[j])
                        dp[i]=max(dp[i],dp[j]+num[i]);
                }
                dp[i]=max(dp[i],num[i]);
            }
            for(int i=0;i<n;i++)
                Max=max(Max,dp[i]);
                printf("%d
    ",Max);
        }
        return 0;
    }
    

    1159.Common Subsequence

    A subsequence of a given sequence is the given sequence with some elements (possible none) left out. Given a sequence X = <x1, x2, ..., xm> another sequence Z = <z1, z2, ..., zk> is a subsequence of X if there exists a strictly increasing sequence <i1, i2, ..., ik> of indices of X such that for all j = 1,2,...,k, xij = zj. For example, Z = <a, b, f, c> is a subsequence of X = <a, b, c, f, b, c> with index sequence <1, 2, 4, 6>. Given two sequences X and Y the problem is to find the length of the maximum-length common subsequence of X and Y. 
    The program input is from a text file. Each data set in the file contains two strings representing the given sequences. The sequences are separated by any number of white spaces. The input data are correct. For each set of data the program prints on the standard output the length of the maximum-length common subsequence from the beginning of a separate line. 

    Sample Input

    abcfbc abfcab programming contest abcd mnp

    Sample Output

    4 2 0

    #include<iostream>
    #include<cstring>
    #include<cstdio>
    using namespace std;
    #define N 1000
    int dp[N][N];
    char a[N],b[N];
    int main()
    {
        while(scanf("%s %s",&a,&b)!=EOF)
        {
            memset(dp,0,sizeof(dp));
            int lena=strlen(a);
            int lenb=strlen(b);
            for(int i=1;i<=lena;i++)
            {
                for(int j=1;j<=lenb;j++)
                {
                    if(a[i-1]==b[j-1])
                        dp[i][j]=dp[i-1][j-1]+1;
                    else
                        dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
                }
            }
            printf("%d
    ",dp[lena][lenb]);
        }
        return 0;
    }
    

    1176.免费馅饼

    都说天上不会掉馅饼,但有一天gameboy正走在回家的小径上,忽然天上掉下大把大把的馅饼。说来gameboy的人品实在是太好了,这馅饼别处都不掉,就掉落在他身旁的10米范围内。馅饼如果掉在了地上当然就不能吃了,所以gameboy马上卸下身上的背包去接。但由于小径两侧都不能站人,所以他只能在小径上接。由于gameboy平时老呆在房间里玩游戏,虽然在游戏中是个身手敏捷的高手,但在现实中运动神经特别迟钝,每秒种只有在移动不超过一米的范围内接住坠落的馅饼。现在给这条小径如图标上坐标:


    为了使问题简化,假设在接下来的一段时间里,馅饼都掉落在0-10这11个位置。开始时gameboy站在5这个位置,因此在第一秒,他只能接到4,5,6这三个位置中其中一个位置上的馅饼。问gameboy最多可能接到多少个馅饼?(假设他的背包可以容纳无穷多个馅饼)

    Input

    输入数据有多组。每组数据的第一行为以正整数n(0<n<100000),表示有n个馅饼掉在这条小径上。在结下来的n行中,每行有两个整数x,T(0<T<100000),表示在第T秒有一个馅饼掉在x点上。同一秒钟在同一点上可能掉下多个馅饼。n=0时输入结束。

    Output

    每一组输入数据对应一行输出。输出一个整数m,表示gameboy最多可能接到m个馅饼。
    提示:本题的输入数据量比较大,建议用scanf读入,用cin可能会超时。

    Sample Input

    6 5 1 4 1 6 1 7 2 7 2 8 3 0

    Sample Output

    4

    #include<stdio.h>
    #include<string.h>
    #include<iostream>
    #include<algorithm> 
    using namespace std;
    int dp[100001][12];//一开始数组开到11 WA了好几遍
    int maxn(int a,int b,int c)
    {
    	int max1;
    	max1=a>b?a:b;
    	max1=max1>c?max1:c;
    	return max1;
    }
    int main()
    {
    	int n,x,t;
    	while(scanf("%d",&n)!=EOF&&n)
    	{
    		int i,j,m=0;
    		memset(dp,0,sizeof(dp));
    		for(i=0;i<n;i++)
    		{
    			scanf("%d%d",&x,&t);
    			dp[t][x]++;
    			if(t>m)
    			m=t;
    		}
    		for(i=m-1;i>=0;i--)
    		{
    			//dp[i][0] += max(dp[i+1][0],dp[i+1][1]); 
    			for(j=0;j<=10;j++)
    			dp[i][j]+=maxn(dp[i+1][j+1],dp[i+1][j],dp[i+1][j-1]);//从三个方向寻找最大权值的方向
    		} 
    		printf("%d
    ",dp[0][5]);
    	}
    	return 0;
    }
    
  • 相关阅读:
    利用CMake生成动态或静态链接库工程
    pdfminer的TextConverter得到文件字符无空格解决方法
    从面向对象的设计模式看软件设计
    google python/c++ code style naming
    python入门之random模块
    python入门之time模块和datetime模块
    关于使用rancher部署k8s集群的一些小问题的解决
    centos虚拟机安装指定版本docker
    k8s的nfs存储外挂设置过程
    【Jenkins】执行yarn install报错:error An unexpected error occurred:"... ... :Unexpected end of JSON input"
  • 原文地址:https://www.cnblogs.com/strawqqhat/p/10602543.html
Copyright © 2011-2022 走看看