dp 题目

Now Ithink you have got an AC in Ignatius. L's "Max Sum" problem. To be abrave ACMer, we always challenge ourselves to more difficult problems. Now youare faced with a more difficult problem.

Givena consecutive number sequence S 1, S 2, S 3, S 4 ... S x, ... S n (1 ≤ x ≤ n ≤1,000,000, -32768 ≤ S x ≤ 32767). We define a function sum(i, j) = S i + ... +S j (1 ≤ i ≤ j ≤ n).

给定一个连续的数字序列SS 2S 3S 4。年代x,…Sn(1 x n 1,000,000-32768S x 32767)。我们定义一个函数和(ij)=S i++S j(1i j n)

Nowgiven an integer m (m > 0), your task is to find m pairs of i and j whichmake sum(i 1, j 1) + sum(i 2, j 2) + sum(i 3, j 3) + ... + sum(i m, j m) maximal(i x ≤ i y ≤ j x or i x ≤ j y ≤ j x is not allowed).

现在给定一个整数m(m-大于0),你的任务是找到mij,它们组成和(i 1 j1)+sum(i 2 j 2)+sum(3j 3)++(i mj m)最大值(我是xyjxjx是不允许的)

ButI`m lazy, I don't want to write a special-judge module, so you don't have tooutput m pairs of i and j, just output the maximal summation of sum(i x, j x)(1≤ x ≤ m) instead. ^_^

但是我很懒,我不想写一个特殊的评判模块,所以你不需要输出mIj,只输出最大的和(xj x)(1x m)的和。^_ ^

Input

输入

Eachtest case will begin with two integers m and n, followed by n integers S 1, S2, S 3 ... S n.

每个测试用例从两个整数mn开始,后面是n个整数,S2S3S n

Processto the end of file.

进程到文件的末尾。

Output

输出

Outputthe maximal summation described above in one line.

输出上面描述的最大的总和。

SampleInput

样例输入

13 1 2 3

13 1 2 3

26 -1 4 -2 3 -2 3

26-1 4-2-3-2 3

SampleOutput

样例输出

6

8

 

 

 

#include<iostream>

#include<cstdio>

#include<algorithm>

using namespace std;

const int maxn=1000000;

#define INF 0x7fffffff

int dp[maxn+10];

int mmax[maxn+10];

int a[maxn+10];

int main()

{

    int n,m;

    int i,j,mmmax;

   while(scanf("%d%d",&m,&n)!=EOF)

    {

        for(i=1;i<=n;i++)

        {

           scanf("%d",&a[i]);

            mmax[i]=0;

            dp[i]=0;

        }

        dp[0]=0;

        mmax[0]=0;

        for(i=1;i<=m;i++)

        {

            mmmax=-INF;

            for(j=i;j<=n;j++)

            {

               dp[j]=max(dp[j-1]+a[j],mmax[j-1]+a[j]);

                mmax[j-1]=mmmax;

               mmmax=max(mmmax,dp[j]);

            }

        }

       printf("%d\n",mmmax);

    }

 

 return 0;

}

C - Monkey and Banana

 

A group of researchers are designing an experiment to test the IQ of a monkey. They will hang a banana at the roof of a building, and at the mean time, provide the monkey with some blocks. If the monkey is clever enough, it shall be able to reach the banana by placing one block on the top another to build a tower and climb up to get its favorite food. 

The researchers have n types of blocks, and an unlimited supply of blocks of each type. Each type-i block was a rectangular solid with linear dimensions (xi, yi, zi). A block could be reoriented so that any two of its three dimensions determined the dimensions of the base and the other dimension was the height. 

They want to make sure that the tallest tower possible by stacking blocks can reach the roof. The problem is that, in building a tower, one block could only be placed on top of another block as long as the two base dimensions of the upper block were both strictly smaller than the corresponding base dimensions of the lower block because there has to be some space for the monkey to step on. This meant, for example, that blocks oriented to have equal-sized bases couldn't be stacked. 

Your job is to write a program that determines the height of the tallest tower the monkey can build with a given set of blocks. 
Input
The input file will contain one or more test cases. The first line of each test case contains an integer n, 
representing the number of different blocks in the following data set. The maximum value for n is 30. 
Each of the next n lines contains three integers representing the values xi, yi and zi. 
Input is terminated by a value of zero (0) for n. 
Output
For each test case, print one line containing the case number (they are numbered sequentially starting from 1) and the height of the tallest possible tower in the format "Case case: maximum height = height". 
Sample Input
1
10 20 30
2
6 8 10
5 5 5
7
1 1 1
2 2 2
3 3 3
4 4 4
5 5 5
6 6 6
7 7 7
5
31 41 59
26 53 58
97 93 23
84 62 64
33 83 27
0
Sample Output
Case 1: maximum height = 40
Case 2: maximum height = 21
Case 3: maximum height = 28
Case 4: maximum height = 342

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=300;
struct blocks{
    int x,y,h;
};
blocks data[300];
int dp[300];
int i,j,n,a,b,c,len,s,ans;
void input(int x,int y,int z){
    len++;
    data[len].x = x; data[len].y = y; data[len].h = z;
}
bool cmp(blocks aa,blocks bb){
    if(aa.x>bb.x)return true;
    return false;
}
int main()
{
    s=0;
    while(scanf("%d",&n)!=EOF&&n)
    {
        len=0;s++;
        for(i=1;i<=n;i++){
            scanf("%d%d%d",&a,&b,&c);
            input(a,b,c);input(a,c,b);input(b,a,c);
            input(b,c,a);input(c,a,b);input(c,b,a);
        }
        sort(data+1,data+1+len,cmp);
        memset(dp,0,sizeof(dp));
        dp[1]=data[1].h;
        for(i=2;i<=len;i++){
            dp[i]=data[i].h;
            for(j=i-1;j>=1;j--)
            {
                if(data[j].x>data[i].x&&data[j].y>data[i].y)
                dp[i]=max(dp[i],dp[j]+data[i].h);
            }
        }
        ans=0;
        for(i=1;i<=len;i++)
        {
            if(dp[i]>ans)ans=dp[i];
        }
        printf("Case %d:maximum height = %d\n",s,ans);
    }return 0;
}

D - Doing Homework

 

Ignatius has just come back school from the 30th ACM/ICPC. Now he has a lot of homework to do. Every teacher gives him a deadline of handing in the homework. If Ignatius hands in the homework after the deadline, the teacher will reduce his score of the final test, 1 day for 1 point. And as you know, doing homework always takes a long time. So Ignatius wants you to help him to arrange the order of doing homework to minimize the reduced score.
Input
The input contains several test cases. The first line of the input is a single integer T which is the number of test cases. T test cases follow. 
Each test case start with a positive integer N(1<=N<=15) which indicate the number of homework. Then N lines follow. Each line contains a string S(the subject's name, each string will at most has 100 characters) and two integers D(the deadline of the subject), C(how many days will it take Ignatius to finish this subject's homework). 

Note: All the subject names are given in the alphabet increasing order. So you may process the problem much easier. 
Output
For each test case, you should output the smallest total reduced score, then give out the order of the subjects, one subject in a line. If there are more than one orders, you should output the alphabet smallest one. 
Sample Input
2
3
Computer 3 3
English 20 1
Math 3 2
3
Computer 3 3
English 6 3
Math 6 3
Sample Output
2
Computer
Math
English
3
Computer
English
Math

#include<cstdio>
#include<cstring>
struct { int x, y, p; }dp[1 << 16];
struct { int d, c; char s[111]; }a[16];
int i, j, jj, ii, yy, v[1 << 16], k, n;
void out(int x)//递归输出
{
if (!x) return;
int k = -1, xx = dp[x].p^x;
while (xx)
xx >>= 1, k++;
out(dp[x].p);
printf("%s\n", a[k].s);
}
int main()
{
scanf("%*d");
while (~scanf("%d", &n))
{
for (i = 0; i < n; i++)
scanf("%s%d%d", a[i].s, &a[i].d, &a[i].c);
memset(v, 0, sizeof(v));
for (i = 0; i < (1 << n) - 1; i++)
for (j = 0; j < n; j++)
{
jj = 1 << j;
if ((i & jj) == 0)
{
ii = i | jj;
dp[ii].x = dp[i].x + a[j].c;
yy = (dp[ii].x > a[j].d ? dp[ii].x - a[j].d : 0) + dp[i].y;  //yy是从当前i执行jj后总超过的时间
if (yy < dp[ii].y || !v[ii])
v[ii] = 1, dp[ii].y = yy, dp[ii].p = i;
}
}
printf("%d\n", dp[(1 << n) - 1].y);
out((1 << n) - 1);
}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值