DP问题杭电(hdu)1231最大连续子序列与4165Pills.doc_第1页
DP问题杭电(hdu)1231最大连续子序列与4165Pills.doc_第2页
DP问题杭电(hdu)1231最大连续子序列与4165Pills.doc_第3页
DP问题杭电(hdu)1231最大连续子序列与4165Pills.doc_第4页
DP问题杭电(hdu)1231最大连续子序列与4165Pills.doc_第5页
已阅读5页,还剩1页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1. 最大连续子序列2. PillsTime Limit: 2000/1000 MS (Java/Others)Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 11465Accepted Submission(s): 4825Problem 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 Input6-2 11 -4 13 -5 -210-10 1 2 3 4 -5 -23 3 7 -2165 -8 3 2 5 01103-1 -5 -23-1 0 -20Sample Output20 11 1310 1 410 3 510 10 100 -1 -20 0 0/showproblem.php?pid=1231以前这样的题还真心不太会,毕竟木有做过着样的题目。以前的题目都是暴利加暴利的。上午做了个关于求最大子序列的题目,对于以前落下的杭电的这个题目也不放过了。方法都一样的。Sum存序列的和。主要就是找起始位置了。这个问题 a b 就是起点和终点了。在最后面 的两个for 循环里面。注意 Output的要求就可以了。这里不多说了再加上今天做的另外做的一个DP 的题目。 Pills Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other)Total Submission(s) : 18 Accepted Submission(s) : 8Font: Times New Roman | Verdana | Georgia Font Size: Problem DescriptionAunt Lizzie takes half a pill of a certain medicine every day. She starts with a bottle that contains N pills.On the first day, she removes a random pill, breaks it in two halves, takes one half and puts the other half back into the bottle.On subsequent days, she removes a random piece (which can be either a whole pill or half a pill) from the bottle. If it is half a pill, she takes it. If it is a whole pill, she takes one half and puts the other half back into the bottle.In how many ways can she empty the bottle? We represent the sequence of pills removed from the bottle in the course of 2N days as a string, where the i-th character is W if a whole pill was chosen on the i-th day, and H if a half pill was chosen (0 = i 2N). How many different valid strings are there that empty the bottle? InputThe input will contain data for at most 1000 problem instances. For each problem instance there will be one line of input: a positive integer N 0 & j 0) 且 :a n 0 =a n-1 1 ;直接贴代码了:可以copy的:#include#include#include#define M 10500int main() int i,n,nuM,sumM,a,b,t,s; while(scanf(%d,&n)!=EOF) if(!n) break; sum0=0; a=1;b=n; for(s=0,i=1;i0) s=s+nui;sumi=s; else sumi=nui;s=0; for(b=n,t=-1,i=1;it) t=sumi;b=i; for(a=1,i=b;i0;i-) if(sumi=0) a=i; else break; if(sumb0) sumb=0; printf(%d %d %dn,sumb,nua,nub); return 0;#includetypedef _int64 element;int main() element a3131; int i,j; for(j=0;

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论