【51nod - 1050】循环数组最大子段和(dp)

题干:

N个整数组成的循环序列a[1],a[2],a[3],…,a[n],求该序列如a[i]+a[i+1]+…+a[j]的连续的子段和的最大值(循环序列是指n个数围成一个圈,因此需要考虑a[n-1],a[n],a[1],a[2]这样的序列)。当所给的整数均为负数时和为0。

例如:-2,11,-4,13,-5,-2,和最大的子段为:11,-4,13。和为20。

 收起

输入

第1行:整数序列的长度N(2 <= N <= 50000)
第2 - N+1行:N个整数 (-10^9 <= S[i] <= 10^9)

输出

输出循环数组的最大子段和。

输入样例

6
-2
11
-4
13
-5
-2

输出样例

20

解题报告:

    模板了。

AC代码:

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int INF = 0x3f3f3f3f;
const LL mod = 1e9 + 7;
const int N = 200005;
int a[N];
LL pre[N];
int main() {int n;scanf("%d", &n);for (int i = 1; i <= n; i++) {scanf("%d", &a[i]);a[n + i] = a[i];}for (int i = 1; i <= 2 * n; i++) {pre[i] = pre[i - 1] + a[i];}deque<int> q;q.push_back(0);LL ans = a[1];for (int i = 1; i <= 2 * n; i++) {if (!q.empty() && q.front() < i - n) {q.pop_front();}if(pre[i] - pre[q.front()] > ans) {ans = pre[i] - pre[q.front()];}else {while (!q.empty() && pre[q.back()] >= pre[i]) {q.pop_back();}}//ans = max(ans, pre[i] - pre[q.front()]);q.push_back(i);}printf("%lld\n", ans);return 0;
}

或者:

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int INF = 0x3f3f3f3f;
const LL mod = 1e9 + 7;
const int N = 200005;
int a[N];
LL pre[N];
int main() {int n;scanf("%d", &n);for (int i = 1; i <= n; i++) {scanf("%d", &a[i]);a[n + i] = a[i];}for (int i = 1; i <= 2 * n; i++) {pre[i] = pre[i - 1] + a[i];}deque<int> q;q.push_back(0);LL ans = a[1];for (int i = 1; i <= 2 * n; i++) {if (!q.empty() && q.front() < i - n) {q.pop_front();}ans = max(ans, pre[i] - pre[q.front()]);while (!q.empty() && pre[q.back()] >= pre[i]) {q.pop_back();}q.push_back(i);}printf("%lld\n", ans);return 0;
}

另一个做法:

https://blog.csdn.net/weixin_41544329/article/details/85076111

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/440779.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Java行业情景分析_Java 设计模式情景分析——单例模式

单例模式可以说是应用最广的模式之一&#xff0c;在应用单例模式时&#xff0c;单例对象的类必须保证只有一个实例存在&#xff0c;而且可以自行实例化并向整个系统提供这个实例。一般在不能自由构造对象的情况下&#xff0c;就会使用单例设计模式&#xff0c;例如创建一个对象…

笔试题 - 汽车加油问题

题干&#xff1a; 第一行输入n&#xff0c;k&#xff0c;代表共有k个加油站&#xff0c;加满油后可以跑n公里。 第二行k1个数&#xff0c;其中前k个数代表加油站的位置&#xff0c;第k1个数代表终点的位置。 一辆车从坐标零点加满油后出发&#xff0c;问最少加几次油&#xff0…

php实现播放直播_PHP直播技术分享(一:实现直播)

推流服务器采用的是自搭的推流服务器 , 自己玩玩 做外包项目还是去搞七牛云/阿里这样的服务器吧,开始bb-----1:技术栈image.jpeg2:开发中业务(1)主播申请时创建个秘钥 , 这个秘钥随时字符串即可到时候根据字符串找到拉流的直播位置存数据库包括推流地址image.png3:配置nginx-rt…

笔试题 - 阶乘问题

题干&#xff1a; 【题意】 给你一个数组n&#xff08;1<n<1e5&#xff09; 让你输出有多少数的阶乘后恰好有n个0&#xff0c;并依次输出。 【类型】 二分or暴力 【分析】 肯定满足&#xff0c;数字越大&#xff0c;其后的0的个数也就越多。 于是我们可以二分出最小的l&…

php获取location,php获取header[‘location’]信息常见问题

15/01/31本文关键字: 302, header, location//初始化url信息$host “#8221;;$url$host.”l/rzTf7ap2viw/&iid222004556&resourceId0_04_05_99/v.swf”;//按照字段获取header响应信息$headers get_headers($url, TRUE);//获取这个土豆的302跳转地址$u302 $headers[“Lo…

【计蒜客 - 蓝桥训练】蒜厂年会(单调队列优化dp,循环数列的最大子段和)

题干&#xff1a; 在蒜厂年会上有一个抽奖&#xff0c;在一个环形的桌子上&#xff0c;有 nn 个纸团&#xff0c;每个纸团上写一个数字&#xff0c;表示你可以获得多少蒜币。但是这个游戏比较坑&#xff0c;里面竟然有负数&#xff0c;表示你要支付多少蒜币。因为这些数字都是…

php对联广告,html左右对联代码 cms网站对联广告html代码

实现网页左右两侧居中的对联广告代码你只记得自己有好多朋友却从没想过小编只有你一人。无标题文档 #left{ width:200px;height:450px; position:absolute; left:0px; background:red; border:1px #000 solid} #right{width:200px;height:450px; position:absolute; right:0px;…

php 分页 url重写 分页问题,解决千古难题,wordpress分页URL问题,wordpress给分页加链接...

原本的wordpress文章如果分页会成如下的结构&#xff1a;http://www.xyqzmt.cn/1.html/2通常固定链接都是这样的结构&#xff0c;设为/%postname%.html或者/%post_id%.html 以前我一直无法解决如上的问题&#xff0c;最后放弃挣扎&#xff0c;如果遇到很长的文章全都放在一个页…

【POJ - 2378】Tree Cutting(树形dp,树的重心变形)

题干&#xff1a; After Farmer John realized that Bessie had installed a "tree-shaped" network among his N (1 < N < 10,000) barns at an incredible cost, he sued Bessie to mitigate his losses. Bessie, feeling vindictive, decided to sabotage…

php 5.5.38 ldap安装,centos5.5系统下面,lnmp环境下面php加载ldap扩展

对于LDAP的安装得先安装检查下面是不是已经安装&#xff0c;如果没有安装之&#xff1a;检查&#xff1a;yum list openldapyum list openldap-devel安装 &#xff1a;yum install openldapyum install openldap-devel一定要执行这条命令&#xff1a;cp -frp /usr/lib64/liblda…

【牛客 - 181F】子序列(容斥,组合数,费马小定理)

题干&#xff1a; 题目描述 给出一个长度为n的序列&#xff0c;你需要计算出所有长度为k的子序列中&#xff0c;除最大最小数之外所有数的乘积相乘的结果 输入描述: 第一行一个整数T&#xff0c;表示数据组数。 对于每组数据&#xff0c;第一行两个整数N&#xff0c;k&#…

php石头剪刀布五局三胜,求程序,下面是剪刀石头布的程序,谁能帮忙改成能选择三局两胜,五局三胜……的?能成功运行的加分...

// 只需要修改main() //#define AllCount 5 // 输入你要选择的赛制&#xff0c;5为五局三胜&#xff0c;3为三局两胜 //#define WinCount AllCount / 2 1 void main() { Play play; int a5; int failure0,win0,tie0; unsigned long begin; int WinCount; int AllCount; printf…

【CodeForces - 1102C 】Doors Breaking and Repairing (思维,简单博弈)

题干&#xff1a; You are policeman and you are playing a game with Slavik. The game is turn-based and each turn consists of two phases. During the first phase you make your move and during the second phase Slavik makes his move. There are nn doors, the i…

php如何提高程序运行效率,如何提高PHP的运行效率 | 萧小寒

摘要在网站开发的过程中&#xff0c;如果我们不太注意代码的规范和编写技巧&#xff0c;很容易导致代码运行效率低下的问题。这个问题应该是绝大多数程序员身上的通病。如何编写出高效的代码&#xff0c;成为很多初中级程序员向高级程序员转变的一道无法逾越的坎。今天让我们一…

【CodeForces - 485C】Bits (二进制相关,数学,贪心)

题干&#xff1a; Lets denote as the number of bits set (1 bits) in the binary representation of the non-negative integer x. You are given multiple queries consisting of pairs of integers l and r. For each query, find the x, such that l ≤ x ≤ r, an…

php工程导致系统蓝屏,经常蓝屏是什么原因

经常蓝屏的原因&#xff1a;1、错误安装或更新显卡驱动后导致电脑蓝屏&#xff1b;2、超频过度是导致蓝屏&#xff1b;3、安装的软件存在不兼容&#xff1b;4、电脑内部硬件温度过高&#xff1b;5、内存条接触不良或内存损坏。错误更新显卡驱动错误安装或更新显卡驱动后导致电脑…

【CodeForces - 485B】Valuable Resources (贪心,水题,几何相关)

题干&#xff1a; Many computer strategy games require building cities, recruiting army, conquering tribes, collecting resources. Sometimes it leads to interesting problems. Lets suppose that your task is to build a square city. The world map uses the Car…

php测试插入,php – 使用Symfony测试数据库插入

今天是个好日子,在过去的几天里,我一直在研究测试驱动开发,并决定我也需要学习它.虽然我无法弄清楚如何准确地做到这一点.我的项目依赖于Symfony2.1.6框架和Doctrine,所以我有一些需要填充的数据库表.Book (1,n) – (0,n) Genre现在,如果我想插入一个类型记录,我首先需要编写一…

【CodeForces - 485A】Factory (水题,抽屉原理,tricks)

题干&#xff1a; One industrial factory is reforming working plan. The director suggested to set a mythical detail production norm. If at the beginning of the day there were x details in the factory storage, then by the end of the day the factory has to p…

matlab调用哈希表,ros与matlab联动使用

ros与matlab联动使用联动使用教程1 ubuntu18.04或16.04下安装matlab R2017b参考链接2 ubuntu下基于matlab启动rosmatlab中常用命令如下&#xff1a;查看robotics system toolbox工具箱是否安装成功help robotics.rosrosinit ——表示启动ROS&#xff0c;相当于roscorerosshutdo…