网易云数据结构- Maximum Subsequence Sum
题目

思路
显然是最大子列和的进化版,那就先思考下经典的最大子列和。这也是道思维题,啥算法也没用到,全是思维技巧,真心不知道考试遇到这种题该怎么办了。
存放答案的一个类,我把它看成一个袋子,需要什么都可以“.”出来
class ans
{
static int maxsub = 0;
static int firstElement = 0;
static int lastElement = 0;
static int lastElementIndex = 0;
}
经典的最大子列和只是更新了maxsub,现在可以同时更新firstElement和lastElement,最后输出即可。
这时更新一下
if (tempsum > ans.maxsub)
{
ans.lastElement = arr[i];
ans.firstElement = firstElement;
ans.maxsub = tempsum;
}
以及
这时更新一下
if (tempsum < 0)
{
if (i + 1 < arr.length)
firstElement = arr[i + 1];
tempsum = 0;
}
对于为什么可以这样,我相信仔细思考一定能看懂。一定要先思考,再看答案,这样才能有共鸣。
代码
package 网易云数据结构;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
import java.security.acl.LastOwnerException;
import java.util.Scanner;
public class Main
{
public static void main(String[] args) throws IOException
{
StreamTokenizer in = new StreamTokenizer(new BufferedReader(
new InputStreamReader(System.in)));
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
in.nextToken();
boolean isAllNegative = true;
// 读入k个数据
int k = (int) in.nval;
int arr[] = new int[k + 1];
for (int i = 1; i <= k; i++)
{
in.nextToken();
arr[i] = (int) in.nval;
if (arr[i] >= 0)
{
isAllNegative = false;
}
}
// 处理数据
int tempsum = 0;
int firstElement = arr[1];
for (int i = 1; i < arr.length; i++)
{
tempsum += arr[i];
if (tempsum > ans.maxsub)
{
ans.lastElement = arr[i];
ans.firstElement = firstElement;
ans.maxsub = tempsum;
}
if (tempsum < 0)
{
if (i + 1 < arr.length)
firstElement = arr[i + 1];
tempsum = 0;
}
}
// 输出
if (isAllNegative)
{
System.out.println(0 + " " + arr[1] + " " + arr[arr.length - 1]);
} else
System.out.println(ans.maxsub + " " + ans.firstElement + " "
+ ans.lastElement);
}
}
class ans
{
static int maxsub = 0;
static int firstElement = 0;
static int lastElement = 0;
static int lastElementIndex = 0;
}
网易云数据结构- Maximum Subsequence Sum的更多相关文章
- 中国大学MOOC-陈越、何钦铭-数据结构-2015秋 01-复杂度2 Maximum Subsequence Sum (25分)
01-复杂度2 Maximum Subsequence Sum (25分) Given a sequence of K integers { N1,N2, ..., NK }. ...
- PAT Maximum Subsequence Sum[最大子序列和,简单dp]
1007 Maximum Subsequence Sum (25)(25 分) Given a sequence of K integers { N~1~, N~2~, ..., N~K~ }. A ...
- Algorithm for Maximum Subsequence Sum z
MSS(Array[],N)//Where N is the number of elements in array { sum=; //current sum max-sum=;//Maximum ...
- PAT1007:Maximum Subsequence Sum
1007. Maximum Subsequence Sum (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...
- PTA (Advanced Level) 1007 Maximum Subsequence Sum
Maximum Subsequence Sum Given a sequence of K integers { N1, N2, ..., NK }. A continuous su ...
- 【DP-最大子串和】PAT1007. Maximum Subsequence Sum
1007. Maximum Subsequence Sum (25) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...
- PAT甲 1007. Maximum Subsequence Sum (25) 2016-09-09 22:56 41人阅读 评论(0) 收藏
1007. Maximum Subsequence Sum (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...
- PAT 甲级 1007 Maximum Subsequence Sum (25)(25 分)(0不是负数,水题)
1007 Maximum Subsequence Sum (25)(25 分) Given a sequence of K integers { N~1~, N~2~, ..., N~K~ }. A ...
- PAT 1007 Maximum Subsequence Sum(最长子段和)
1007. Maximum Subsequence Sum (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...
随机推荐
- CoutDownLatch 多线程同步辅助类
CountDownLatch,一个同步辅助类,在完成一组正在其他线程中执行的操作之前,它允许一个或多个线程一直等待. 主要方法 public CountDownLatch(int count); pu ...
- C++服务器设计(六):设备连接的生命周期管理
生命周期介绍 每一个服务器系统的新连接从建立开始时,均会经历多个阶段.比如连接的建立,登录的验证,退出前的资源释放等.同时在具体的消息处理中,还会遇到不可识别的消息事件,或者消息处理时出现数据错误等. ...
- codeforces 337C Quiz(贪心)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Quiz Manao is taking part in a quiz. The ...
- (原)ubuntu16在torch中使用caffe训练好的模型
转载请注明出处: http://www.cnblogs.com/darkknightzh/p/5783006.html 之前使用的是torch,由于其他人在caffe上面预训练了inception模型 ...
- 从汇编看c++中的placement operator new
placement operator new是重载的operator new运算符,它允许我们将对象放到一个指定的内存中.下面来看c++源码: class X { private: int _x; p ...
- Js之Location对象
Window对象的location属性引用的是Location对象,它表示该窗口中当前显示的文档的URL,并定义了方法来使窗口载入新的文档.Document对象的location属性也引用到Locat ...
- AngularJs directive 'transclude' option 详解
transclude好像不是一个英语单词,有道词典里没有,百度翻译的意思是嵌入. transclude在angularjs的自定义的derective中是比较常见的一个东西,所有有必要要了解它. 我们 ...
- [原]C++关于运算符重载的程序报错error…
错误信息如下: 1>t2.obj : error LNK2019: 无法解析的外部符号 "public: __thiscall Date::Date(void)" (??0D ...
- Python学习笔记一,输入输出
输出:用print()在括号中加上字符串,就可以向屏幕上输出指定的文字.比如输出'hello, world',用代码实现如下>>>print('hello,world') 也可以是多 ...
- collections——高性能容器数据类型
由于最近对机器学习算法感兴趣,一直知道python有一个包collections封装了一些比dict,list之类高级点的类,所以抽空研究下,为接下来的工作准备. 主要参考是https://docs. ...