2015 程序设计实习之动规作业2

来源:互联网 发布:聚宝盆营销软件 编辑:程序博客网 时间:2024/04/29 18:44

A:UNIMODAL PALINDROMIC DECOMPOSITIONS(1221POJ)

  • 查看
  • 提交
  • 统计
  • 提问
总时间限制: 
1000ms 
内存限制: 
65536kB
描述

A sequence of positive integers is Palindromic if it reads the same forward and backward. For example:
23 11 15 1 37 37 1 15 11 23
1 1 2 3 4 7 7 10 7 7 4 3 2 1 1
A Palindromic sequence is Unimodal Palindromic if the values do not decrease up to the middle value and then (since the sequence is palindromic) do not increase from the middle to the end For example, the first example sequence above is NOT Unimodal Palindromic while the second example is.
A Unimodal Palindromic sequence is a Unimodal Palindromic Decomposition of an integer N, if the sum of the integers in the sequence is N. For example, all of the Unimodal Palindromic Decompositions of the first few integers are given below:
1: (1)
2: (2), (1 1)
3: (3), (1 1 1)
4: (4), (1 2 1), (2 2), (1 1 1 1)
5: (5), (1 3 1), (1 1 1 1 1)
6: (6), (1 4 1), (2 2 2), (1 1 2 1 1), (3 3),
(1 2 2 1), ( 1 1 1 1 1 1)
7: (7), (1 5 1), (2 3 2), (1 1 3 1 1), (1 1 1 1 1 1 1)
8: (8), (1 6 1), (2 4 2), (1 1 4 1 1), (1 2 2 2 1),
(1 1 1 2 1 1 1), ( 4 4), (1 3 3 1), (2 2 2 2),
(1 1 2 2 1 1), (1 1 1 1 1 1 1 1)

Write a program, which computes the number of Unimodal Palindromic Decompositions of an integer.

输入
Input consists of a sequence of positive integers, one per line ending with a 0 (zero) indicating the end.
输出
For each input value except the last, the output is a line containing the input value followed by a space, then the number of Unimodal Palindromic Decompositions of the input value. See the example on the next page.
样例输入
2345678102324131213920
样例输出
2 23 24 45 36 77 58 1110 1723 10424 199131 5010688213 105585259092 331143
提示
N < 250


#include<iostream>#include<memory.h>using namespace std;long long f[250][250];//long long 据说不写longlong会WA    int num;long long wf(int n,int k){    if(n==k)        return f[n][n];    if(n<2*k)        return 0;    if(n==2*k)        return 1;    if(f[n-2*k][k]!=0)        return f[n-2*k][k];    else    {        int temp=0;        for(int i=k;i<=n-2*k;++i)            temp+=wf(n-2*k,i);        f[n-2*k][k]=temp;        return f[n-2*k][k];    }}void compute(int n){    if(f[n][1]!=0)        return ;    else    {        for(int i=1;i<=n;++i)            f[n][1]+=wf(n,i);    }    return ;}int main(){    memset(f,0,sizeof(f));    for(int i=1;i<250;++i)        f[i][i]=1;    while(cin>>num)    {        if(num==0)break;       compute(num);        cout<<num<<' '<<f[num][1]<<endl;    }    return 0;    }

B:Charm Bracelet

  • 查看
  • 提交
  • 统计
  • 提问
总时间限制: 
1000ms 
内存限制: 
65536kB
描述

Bessie has gone to the mall's jewelry store and spies a charm bracelet. Of course, she'd like to fill it with the best charms possible from the N(1 ≤ N≤ 3,402) available charms. Each charm iin the supplied list has a weight Wi(1 ≤ Wi≤ 400), a 'desirability' factor Di(1 ≤ Di≤ 100), and can be used at most once. Bessie can only support a charm bracelet whose weight is no more than M(1 ≤ M≤ 12,880).

Given that weight limit as a constraint and a list of the charms with their weights and desirability rating, deduce the maximum possible sum of ratings.


输入
Line 1: Two space-separated integers: N and M
Lines 2..N+1: Line i+1 describes charm i with two space-separated integers: Wi and Di
输出
Line 1: A single integer that is the greatest sum of charm desirabilities that can be achieved given the weight constraints
样例输入
4 61 42 63 122 7
样例输出
23
来源
USACO 2007 December Silver
#include<stdio.h>#include<string.h>#include<stdlib.h>#include<math.h>#include<algorithm>using namespace std;int N,M;int cost[3500];int desire[3500];int d[13000];//d[i][j]表示将前i件物品放入容量为j的背包的最大值int main(){    while(scanf("%d %d",&N,&M)!=EOF)    {        int i,j;        for(i=0;i<N;i++)        {            scanf("%d %d",&cost[i],&desire[i]);        }        for(i=0;i<=M;i++)        {            d[i]=0;        }        for(i=1;i<=N;i++)        {            for(j=M;j>=0;j--)            {                if(j-cost[i-1]<0)                {                    continue;                }                d[j]=d[j]>(d[j-cost[i-1]]+desire[i-1])?d[j]:(d[j-cost[i-1]]+desire[i-1]);            }        }        printf("%d\n",d[M]);    }    return 0;}



0 0
原创粉丝点击