第四天

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&ltstdio.h&gt
#include&ltstdlib.h&gt
#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&ltstdio.h&gt
#include&ltstdlib.h&gt

int N;
int last;
int time;

int main(void)
{
scanf("%d", &N);
for (int i = 0; i 0)
time += n * 6;
else if (n

PTA A1007&A1008的更多相关文章

  1. 浙大PTA - - 堆中的路径

    题目链接:https://pta.patest.cn/pta/test/1342/exam/4/question/21731 本题即考察最小堆的基本操作: #include "iostrea ...

  2. 浙大PTA - - File Transfer

    题目链接:https://pta.patest.cn/pta/test/1342/exam/4/question/21732 #include "iostream" #includ ...

  3. 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 ...

  4. PTA中提交Java程序的一些套路

    201708新版改版说明 PTA与2017年8月已升级成新版,域名改为https://pintia.cn/,官方建议使用Firefox与Chrome浏览器. 旧版 PTA 用户首次在新版系统登录时,请 ...

  5. PTA分享码-Java

    主要用于Java语法练习,非竞赛类题目.   1. Java入门          959dbf0b7729daa61d379ec95fb8ddb0   2. Java基本语法   23bd8870e ...

  6. C语言第一次实验报告————PTA实验1.2.3内容

    一.PTA实验作业 题目1.温度转换 本题要求编写程序,计算华氏温度100°F对应的摄氏温度.计算公式:C=5×(F−32)/9,式中:C表示摄氏温度,F表示华氏温度,输出数据要求为整型. 1.实验代 ...

  7. PTA题---求两个有序序列中位数所体现的思想。

    ---恢复内容开始--- 近日,在做PTA题目时,遇到了一个这样的题,困扰了很久.题目如下:已知有两个等长的非降序序列S1, S2, 设计函数求S1与S2并集的中位数.有序序列A​0​​,A​1​​, ...

  8. 第十四,十五周PTA作业

    1.第十四周part1 7-3 #include<stdio.h> int main() { int n; scanf("%d",&n); int a[n]; ...

  9. 第七周PTA作业

    第一题: #include<stdio.h> int main() { ; ; ){ sum=sum+i; i++; } printf("sum = %d\n",sum ...

随机推荐

  1. 在linux中用同一个版本的R 同时安装 Seurat2 和 Seurat3

    在linux中用同一个版本的R 同时安装 Seurat 2 和 Seurat 3 Seurat  作为单细胞分析中的重量级R包,有多好用用,用过的人都知道.Seurat 分析流程基本涵盖了单细胞分析中 ...

  2. 使用文本框TextView/EditText的开源库清单

    在实际的开发中TextView和EditText是非常基本的控件.这两个控件的使用也是十分简单.而TextView/EditText的功能其实也是非常强大,例如简单的图文就可以使用TextView配合 ...

  3. Linux命令行中好用的快捷键

    Ctrl + L:清屏,与clear同效: Ctrl + D:终结当前程序,向终端传递EOF(End Of File): Ctrl + A:光标移至一行命令开头: Ctrl + E:光标移至一行命令结 ...

  4. React引入AntD按需加载报错

    背景:React使用create-react-app脚手架创建,然后yarn run eject暴露了配置之后修改less配置, 需求:实现antd组件按需加载与修改主题. 一开始是按照webpack ...

  5. 关于Picasso加载图片Callback不执行问题

    关于Picasso加载图片Callback不执行问题 问题背景 代码大致如下,Target或Callback的回调有时候不执行. https://github.com/square/picasso/i ...

  6. JAVA 异常汇总

    1  java.lang.ArithmeticException: / by zero 原因:当我们定义的被除数为整型时(short.int.long)会抛出此异常, 被除数为整型时不可为零.解决办法 ...

  7. 跨库数据迁移利器 —— Sqoop

    一.Sqoop 基本命令 1. 查看所有命令 # sqoop help 2. 查看某条命令的具体使用方法 # sqoop help 命令名 二.Sqoop 与 MySQL 1. 查询MySQL所有数据 ...

  8. 【CocosBuilder】学习笔记目录

    从2019年8月底开始学习CocosBuilder. ‎CocosBuilder 学习笔记(1) CCBReader 解析.ccbi文件流程 ‎CocosBuilder 学习笔记(2) .ccbi 文 ...

  9. Codeforces Round #504 E - Down or Right 交互题

    1023E 题意: 交互题.在一个有障碍地图中,问如何走才能从(1,1)走到(n,n),只能向右或者向左走.每次询问两个点,回复你这两个点能不能走通. 思路: 只用最多2*n-2次询问.从(1,1), ...

  10. cogs 313. [POI2001] 和平委员会(2-SAT

    http://cogs.pro:8080/cogs/problem/problem.php?pid=pyzQimjkj 题意:有n个集合,每个集合有俩元素,要从n个中各选一个放一堆,但是有的俩不能同时 ...