HDU 初级DP专题

最大报销额

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 20128    Accepted Submission(s): 5974


Problem Description
现有一笔经费可以报销一定额度的发票。允许报销的发票类型包括买图书(A类)、文具(B类)、差旅(C类),要求每张发票的总额不得超过1000元,每张发票上,单项物品的价值不得超过600元。现请你编写程序,在给出的一堆发票中找出可以报销的、不超过给定额度的最大报销额。
 

Input
测试输入包含若干测试用例。每个测试用例的第1行包含两个正数 Q 和 N,其中 Q 是给定的报销额度,N(<=30)是发票张数。随后是 N 行输入,每行的格式为:
m Type_1:price_1 Type_2:price_2 ... Type_m:price_m
其中正整数 m 是这张发票上所开物品的件数,Type_i 和 price_i 是第 i 项物品的种类和价值。物品种类用一个大写英文字母表示。当N为0时,全部输入结束,相应的结果不要输出。
 

Output
对每个测试用例输出1行,即可以报销的最大数额,精确到小数点后2位。
 

Sample Input
   
   
   
   
200.00 3 2 A:23.50 B:100.00 1 C:650.00 3 A:59.99 A:120.00 X:10.00 1200.00 2 2 B:600.00 A:400.00 1 C:200.50 1200.50 3 2 B:600.00 A:400.00 1 C:200.50 1 A:100.00 100.00 0
Sample Output
   
   
   
   
123.50 1000.00 1200.50
Source
浙大计算机研究生复试上机考试-2007年

坑点:

注意题目中说的  “每项” 是指 每一种,不是每一个

方法1.直接sort(),不用dp,因为背包的每项所占的体积都是1

方法二:将报销的数额作为背包,将实数扩大100 倍。

方法3:

#include<bits/stdc++.h>
using namespace std;
double sum[10000],dp[10000];
int solve(char s[],double &l)  //将字符串转化为实数
{
    int i=0;
    while(s[i]<'0'||s[i]>'9')
    i++;
    while(s[i]>='0'&&s[i]<='9')
    {
        l*=10;
        l+=s[i]-'0';
        i++;
    }
    if(s[i]!='.')
    return 0;
    i++;
    double k=0.1;
    while(s[i]>='0'&&s[i]<='9')
    {
         l+= k*(s[i]-'0');
         k*=0.1;
         i++;
    }
}
char s[1000000];
double a[10];
int main()
{
    double v;
    int n;
    while(~scanf("%lf%d",&v,&n))
    {
        if(n==0)
            break;
        for(int i=0;i<n;i++)
        {
            sum[i]=0;
            int m,flag=0;
            scanf("%d",&m);
            for(int i=0;i<3;i++)
                a[i]=0;
            for(int j=0;j<m;j++)
            {
                scanf("%s",s);      //也可以通过scanf(" %c:%lf");进行处理输入问题
                                    //就不必再有solve()
               // cout<<"#"<<s<<endl;
                if(flag)
                    continue;
                int len=strlen(s);
                if(s[0]!='A'&&s[0]!='B'&&s[0]!='C')
                {
                    sum[i]=0;
                    flag=1;
                    continue;
                }
                double l=0;
                solve(s,l);
                a[s[0]-'A']+=l;
                if(a[s[0]-'A']>600) //表示每一项
                {
                    flag=1;
                    sum[i]=0;
                    continue;
                }
                sum[i]+=l; 
                if(sum[i]>1000)
                {
                    flag=1;
                    sum[i]=0;
                    continue;
                }
            }
        }
        memset(dp,0,sizeof(dp));
        for(int i=0;i<n;i++)
        {
            for(int j=n;j>=1;j--) //以支票个数为容量储存
            {
                dp[j]=max(dp[j],dp[j-1]+sum[i]);
            }
        }
        double Max=0;
        for(int i=n;i>=0;i--)
        {
            if(dp[i]<=v&&Max<dp[i])
            {
                Max=dp[i];
            }
        }
        printf("%.2lf\n",Max);
    }
}
HDU1231

最大连续子序列

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 24172    Accepted Submission(s): 10848


Problem Description
给定K个整数的序列{ N1, N2, ..., NK },其任意连续子序列可表示为{ Ni, Ni+1, ...,
Nj },其中 1 <= i <= j <= K。最大连续子序列是所有连续子序列中元素和最大的一个,
例如给定序列{ -2, 11, -4, 13, -5, -2 },其最大连续子序列为{ 11, -4, 13 },最大和
为20。
在今年的数据结构考卷中,要求编写程序得到最大和,现在增加一个要求,即还需要输出该
子序列的第一个和最后一个元素。
Input
测试输入包含若干测试用例,每个测试用例占2行,第1行给出正整数K( < 10000 ),第2行给出K个整数,中间用空格分隔。当K为0时,输入结束,该用例不被处理。
Output
对每个测试用例,在1行里输出最大和、最大连续子序列的第一个和最后一个元
素,中间用空格分隔。如果最大连续子序列不唯一,则输出序号i和j最小的那个(如输入样例的第2、3组)。若所有K个元素都是负数,则定义其最大和为0,输出整个序列的首尾元素。
Sample Input
   
   
   
   
6 -2 11 -4 13 -5 -2 10 -10 1 2 3 4 -5 -23 3 7 -21 6 5 -8 3 2 5 0 1 10 3 -1 -5 -2 3 -1 0 -2 0
Sample Output
   
   
   
   
20 11 13 10 1 4 10 3 5 10 10 10 0 -1 -2 0 0 0
Source
浙大计算机研究生复试上机考试-2005年
状态方程:sum[i]=max(sum[i-1]+a[i],a[i]);最后从头到尾扫一边
 
#include<bits/stdc++.h>
using namespace std;
int a[10010];
struct node
{
    int l,r,num;
}dp[10010];
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        if(n==0)break;
        for(int i=0;i<n;i++)
        {
            scanf("%d",&a[i]);
            dp[i].num=0;
        }
      //  memset(dp,0,sizeof(dp));
        dp[0].num=a[0];
        dp[0].l=a[0];
        dp[0].r=a[0];
        for(int i=1;i<n;i++)
        {
            if(dp[i-1].num<=0)
            {
                dp[i-1].r=a[i-1];
                dp[i].num=a[i];
                dp[i].l=a[i];
            }
            else
            {
                dp[i].l=dp[i-1].l;
                dp[i].num=dp[i-1].num+a[i];
                dp[i].r=a[i];
            }
        }
        int Max=0,flag=-1;
        for(int i=0;i<n;i++)
        {
            if(Max==dp[i].num&&Max==0)
            {
                Max=dp[i].num;
                flag=i;
            }
           else if(Max<dp[i].num)
                { Max=dp[i].num;
                  flag=i;
                }
        }
        if(flag==-1)
            printf("0 %d %d\n",a[0],a[n-1]);
        else
            printf("%d %d %d\n",Max,dp[flag].l,dp[flag].r);
    }
    return 0;
}

HUD1003  Max Sum

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 189626    Accepted Submission(s): 44171


Problem Description
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
 
#include<bits/stdc++.h>
using namespace std;  //数组开小了会TLE
int a[100010];
struct node
{
    int l,r,num;
} dp[100010];
int main()
{int n,Case=0,T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        for(int i=0; i<n; i++)
        {
            scanf("%d",&a[i]);
            dp[i].num=0;
        }
        dp[0].num=a[0];dp[0].l=1;dp[0].r=1;
        for(int i=1;i<n;i++)
        {
            if(dp[i-1].num<0)
            {
                dp[i-1].r=i;
                dp[i].l=i+1;
                dp[i].num=a[i];
            }
            else
            {
                dp[i-1].r=i;
                dp[i].r=i+1;
                dp[i].num=dp[i-1].num+a[i];
                dp[i].l=dp[i-1].l;
            }
        }
        int Max=dp[0].num,flag=0;
        for(int i=1; i<n; i++)
        {
            if(Max<dp[i].num)
            {
                Max=dp[i].num;
                flag=i;
            }
        }
        if(Case!=0)
            printf("\n");
        Case++;
        printf("Case %d:\n",Case);
        printf("%d %d %d\n",Max,dp[flag].l,dp[flag].r);
    }
    return 0;
}



你可能感兴趣的:(HDU 初级DP专题)