PTA A1007&A1008
第四天
A1007 Maximum Subsequence Sum (25 分)
题目内容
Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to be { Ni, Ni+1, ..., Nj
} where 1≤i≤j≤K. The Maximum Subsequence is the continuous subsequence which has the largest sum of its elements. For example, given sequence { -2, 11, -4, 13, -5, -2 }, its maximum subsequence is { 11, -4, 13 } with the largest sum being 20.
Now you are supposed to find the largest sum, together with the first and the last numbers of the maximum subsequence.
Input Specification:
Each input file contains one test case. Each case occupies two lines. The first line contains a positive integer K (≤10000). The second line contains K numbers, separated by a space.
Output Specification:
For each test case, output in one line the largest sum, together with the first and the last numbers of the maximum subsequence. The numbers must be separated by one space, but there must be no extra space at the end of a line. In case that the maximum subsequence is not unique, output the one with the smallest indices i and j (as shown by the sample case). If all the K numbers are negative, then its maximum sum is defined to be 0, and you are supposed to output the first and the last numbers of the whole sequence.
Sample Input:
10
-10 1 2 3 4 -5 -23 3 7 -21
Sample Output:
10 1 4
单词
continuous
英 /kən'tɪnjʊəs/ 美 /kən'tɪnjʊəs/
adj. 连续的,持续的;继续的;连绵不断的
indices
英 /'ɪndɪsiːz/ 美 /'ɪndɪsiz/
n. 指数;目录(index的复数)
题目分析
最大子列和问题,在MOOC数据结构课程中听姥姥说过一遍,自己也写过,再写的时候发现有点忘了,于是翻了以前的代码。。。。不忍直视啊=-=,复习了一遍姥姥的视频,自己重写了一遍,据说解决方法是动态规划,并没有深入了解。
具体代码
#include<stdio.h>
#include<stdlib.h>
#define MAXSIZE 10000
int N;
int a[MAXSIZE];
int begin, maxbegin, maxend;
int maxsum = -1, sum;int main(void)
{
scanf("%d", &N);
for (int i = 0; i maxsum)
{
maxsum = sum;
maxbegin = begin;
maxend = i;
}
if (sum参考博客
【C/C++】Maximum Subsequence Sum/最大子列和问题
A1008 Elevator (20 分)
题目内容
The highest building in our city has only one elevator. A request list is made up with N positive numbers. The numbers denote at which floors the elevator will stop, in specified order. It costs 6 seconds to move the elevator up one floor, and 4 seconds to move down one floor. The elevator will stay for 5 seconds at each stop.
For a given request list, you are to compute the total time spent to fulfill the requests on the list. The elevator is on the 0th floor at the beginning and does not have to return to the ground floor when the requests are fulfilled.Input Specification:
Each input file contains one test case. Each case contains a positive integer N, followed by N positive numbers. All the numbers in the input are less than 100.
Output Specification:
For each test case, print the total time on a single line.
Sample Input:
3 2 3 1
Sample Output:
41
单词
request
英 /rɪ'kwest/ 美 /rɪ'kwɛst/
n. 请求;需要
vt. 要求,请求denote
英 /dɪ'nəʊt/ 美 /dɪ'not/
vt. 表示,指示specified
英 /ˈspesɪfaɪd/ 美 /ˈspɛsɪfaɪd/
v. 指定;详细说明(specify的过去分词)
adj. 规定的;详细说明的fulfill
英 /ful'fil/ 美 /ful'fil/
vt. 履行;实现;满足;使结束(等于fulfil)题目分析
没什么好说的,小学生加减法而已。
具体代码
#include<stdio.h>
#include<stdlib.h>int N;
int last;
int time;int main(void)
{
scanf("%d", &N);
for (int i = 0; i 0)
time += n * 6;
else if (nPTA A1007&A1008的更多相关文章
- 浙大PTA - - 堆中的路径
题目链接:https://pta.patest.cn/pta/test/1342/exam/4/question/21731 本题即考察最小堆的基本操作: #include "iostrea ...
- 浙大PTA - - File Transfer
题目链接:https://pta.patest.cn/pta/test/1342/exam/4/question/21732 #include "iostream" #includ ...
- ERROR<53761> - Plugins - conn=-1 op=-1 msgId=-1 - Connection Bind through PTA failed (91). Retrying...
LDAP6.3在DSCC控制台启动实例完成,但是操作状态显示“意外错误”,查看日志如下: 04/May/2016:21:10:39 +0800] - Sun-Java(tm)-System-Direc ...
- PTA中提交Java程序的一些套路
201708新版改版说明 PTA与2017年8月已升级成新版,域名改为https://pintia.cn/,官方建议使用Firefox与Chrome浏览器. 旧版 PTA 用户首次在新版系统登录时,请 ...
- PTA分享码-Java
主要用于Java语法练习,非竞赛类题目. 1. Java入门 959dbf0b7729daa61d379ec95fb8ddb0 2. Java基本语法 23bd8870e ...
- C语言第一次实验报告————PTA实验1.2.3内容
一.PTA实验作业 题目1.温度转换 本题要求编写程序,计算华氏温度100°F对应的摄氏温度.计算公式:C=5×(F−32)/9,式中:C表示摄氏温度,F表示华氏温度,输出数据要求为整型. 1.实验代 ...
- PTA题---求两个有序序列中位数所体现的思想。
---恢复内容开始--- 近日,在做PTA题目时,遇到了一个这样的题,困扰了很久.题目如下:已知有两个等长的非降序序列S1, S2, 设计函数求S1与S2并集的中位数.有序序列A0,A1, ...
- 第十四,十五周PTA作业
1.第十四周part1 7-3 #include<stdio.h> int main() { int n; scanf("%d",&n); int a[n]; ...
- 第七周PTA作业
第一题: #include<stdio.h> int main() { ; ; ){ sum=sum+i; i++; } printf("sum = %d\n",sum ...
随机推荐
- mysql根据逗号将一行数据拆分成多行数据
mysql根据逗号将一行数据拆分成多行数据 原始数据 处理结果展示 DDL CREATE TABLE `company` ( `id` ) DEFAULT NULL, `name` ) DEFAULT ...
- Nacos(一):Nacos介绍
前言 6月份阿里开源的Nacos出了1.0.1版本,从去年7月份第一个release版本到现在一直在默默关注 官方的版本规划为:Nacos从0.8.0开始支持生产可用,1.0版本可大规模生产可用,2. ...
- Scrapy框架解读
1. Scrapy组件a. 主体部分i. 引擎(Scrapy):处理整个系统的数据流处理,触发事务(框架核心)ii. 调度器(Scheduler):1) 用来接受引擎发过来的请求, 压入队列中, 并在 ...
- N个tomcat之间实现Session共享(写的不错,转来的)
以下文章写的比较不错,转来的. tomcat的session共享设置如此简单为什么很少人去用.这个我说的重点. 1.自身的session如果服务器不在同一个网段会有session失效(本人使用的是阿里 ...
- sea.js的同步魔法
前些时间也是想写点关于CMD模块规范的文字,以便帮助自己理解.今天看到一篇知乎回答,算是给了我一点启发. 同步写法却不阻塞? 先上一个sea.js很经典的模块写法: // 定义一个模块 define( ...
- javaio字节流复制文件夹
public class Copy1 { public static void main(String[] args) throws IOException { File src=new File(& ...
- 算法与数据结构基础 - 贪心(Greedy)
贪心基础 贪心(Greedy)常用于解决最优问题,以期通过某种策略获得一系列局部最优解.从而求得整体最优解. 贪心从局部最优角度考虑,只适用于具备无后效性的问题,即某个状态以前的过程不影响以后的状态. ...
- Unity的UGUI在SetParent后修改UI的localposition问题
正常情况下,UGUI设置UI的localposition可以直接赋值 UIxxx.rectTransform.localPosition = ] / 2f, , ); 运行后在Unity的Inspec ...
- 【RabbitMQ】如何进行消息可靠投递【下篇】
说明 上一篇文章里,我们了解了如何保证消息被可靠投递到RabbitMQ的交换机中,但还有一些不完美的地方,试想一下,如果向RabbitMQ服务器发送一条消息,服务器确实也接收到了这条消息,于是给你返回 ...
- Elasticsearch和solr之我为什么选择solr
老大:这个项目需要用到搜索引擎,小李你去学习一下. 小李:喳! 小李:以前用过的搜索引擎是solr4.7,那已经是两年前使用的了不知道现在有没有更好的解决方案了呢? 小李打开了google,百度,bi ...