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).
给定一个连续的数字序列S,S 2,S 3,S 4。年代x,…Sn(1 x n 1,000,000,-32768S x 32767)。我们定义一个函数和(i,j)=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),你的任务是找到m对i和j,它们组成和(i 1 j1)+sum(i 2 j 2)+sum(我3,j 3)+。+和(i m,j m)最大值(我是x,y,j,x,j,x是不允许的)。
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. ^_^
但是我很懒,我不想写一个特殊的评判模块,所以你不需要输出m对I和j,只输出最大的和(x,j 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.
每个测试用例从两个整数m和n开始,后面是n个整数,S,2,S。3。S 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
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.
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.
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
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
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.
2 3 Computer 3 3 English 20 1 Math 3 2 3 Computer 3 3 English 6 3 Math 6 3
2 Computer Math English 3 Computer English Math
#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);
}
}