HDU -> ACM -> Max Sum
程序员文章站
2024-03-20 09:24:04
...
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<iostream>
#include<algorithm>
#include<stdio.h>
using namespace std;
int l[1001];
int flag[1001][1001];//当把两个数组放到这里时不会出现栈溢出
struct Mice
{
int w;
int s;
int id;
};
Mice mices[1001];
int cmp(struct Mice a,struct Mice b)
{
if(a.w!=b.w) return a.w<b.w;
else return a.s>b.s;
}
int main()
{
//int l[1001];//存储最大递减序列的长度的值
//int flag[1001][1001];//存储递减序列下表的值
int i,size,j,k,max,t;
i=0;
while(scanf("%d %d",&mices[i].w,&mices[i].s)!=EOF)
{
mices[i].id=i+1;
i++;
}
size=i;
sort(mices,mices+size,cmp);
for(i=0;i<size;i++)
{
l[i]=1;
flag[i][0]=i;
}
for(i=1;i<size;i++)
{
for(j=i-1;j>=0;j--)
{
if(mices[i].w>mices[j].w && mices[i].s<mices[j].s && l[i]<l[j]+1)
{
max=l[j]+1;
l[i]=max;
for(k=0;k<max-1;k++)
flag[i][k]=flag[j][k];
flag[i][max-1]=i;
}
}
}
int index;
for(index=0,k=1;k<size;k++)
if(l[index]<l[k]) index=k;
cout<<l[index];
for(i=0;i<l[index];i++)
cout<<endl<<mices[flag[index][i]].id;
return 0;
}