51Nod - 1050
题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1050
解题思路:
It's so beautiful !
循环数组的最大子段 [l, r] 只有两种可能:
1、0 < l <= r <= N,这一部分就是单纯地求数组的最大子段和;
2、0 < r < l <= N,这一部分可以这么想:既然这个循环子段最大,那么数组剩下的连续子段 [r+1,l-1] 应该就是最小子段,那么我们只要求出最小子段和,再用总和减去这个最小子段和,得到的肯定就是最大循环子段和。
AC代码:
#include <cstdio>
#include <algorithm> using namespace std;
typedef long long ll;
const int maxn=+;
ll A[maxn],dp[maxn];
int main(){
int N;
scanf("%d",&N);
bool zero=true;
for(int i=;i<=N;i++){
scanf("%lld",&A[i]);
if(A[i]>=) zero=false;
}
if(zero) printf("0\n");
else{
dp[]=A[];
ll ans=dp[];
for(int i=;i<=N;i++){
dp[i]=max(A[i],A[i]+dp[i-]);
if(dp[i]>ans) ans=dp[i];
}
printf("%lld\n",ans);
} return ;
}
51Nod - 1050的更多相关文章
- 51nod 1050 循环数组最大子段和
题目链接:51nod 1050 循环数组最大子段和 #include<stdio.h> #include<algorithm> using namespace std; ; l ...
- 51nod 1050 循环数组最大子段和 (dp)
http://www.51nod.com/onlineJudge/questionCode.html#problemId=1050¬iceId=13385 参考:http://blog. ...
- 51nod 1050 循环数组最大子段和 单调队列优化DP
题目链接: http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1050 这个呢,这个题之前 求一遍最大值 然后求一遍最小值 ...
- 51nod 1050 循环数组最大子段和【环形DP/最大子段和/正难则反】
1050 循环数组最大子段和 基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题 收藏 关注 N个整数组成的循环序列a[1],a[2],a[3],…,a[n],求该 ...
- 51Nod 1050 循环数组最大子段和 | DP
Input示例 6 -2 11 -4 13 -5 -2 Output示例 20 分析: 有两种可能,第一种为正常从[1 - n]序列中的最大子字段和:第二种为数组的total_sum - ([1-n] ...
- 51nod 1050【DP】
思路: 就是先正常的dp一下求一个最大连续子串,然后特殊情况就是sum-最小连续子串.. 比一比谁大谁小就好了 #include <stdio.h> #include <string ...
- 51nod 1050 循环数组最大子段和【动态规划】
N个整数组成的循环序列a[1],a[2],a[3],-,a[n],求该序列如a[i]+a[i+1]+-+a[j]的连续的子段和的最大值(循环序列是指n个数围成一个圈,因此需要考虑a[n-1],a[n] ...
- 基础dp 记录
51nod 1134 最长递增子序列 #include<iostream> #include<cstdio> #include<cstring> #include& ...
- 51nod 循环数组最大子段和
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1050 对于普通的数组,只要求一次最大子段和即可.但是这题是可以循环的,所 ...
随机推荐
- 你知道吗?iOS不少程序常传送装置信息给第三方
2019独角兽企业重金招聘Python工程师标准>>> 华盛顿邮报( The Washington Post)与隐私程序开发商Disconnect共同进行的研究揭露,许多iOS程序其 ...
- 《树莓派学习指南(基于Linux)》——本章小结
本节书摘来自异步社区<树莓派学习指南(基于Linux)>一书中的第二章的本章小结,作者[英]Peter Membrey ,[澳]David Hows ,译者 张志博,孙峻文,更多章节内容可 ...
- 【总结】Centos中,Kerberos安装
1.安装软件包 安装必须的工具 bison, make, binutils 下载压缩包至/usr/local目录下,并解压 [root@localhost local]# ls krb5-1.14.t ...
- 通过fiddler抓取IDEA的请求
2019独角兽企业重金招聘Python工程师标准>>> 因为fiddler默认是代理的8888端口,所以设置一下IDEA的请求使用本地的8888作为代理发出. 1."EDI ...
- Apache2.4 根目录修改
需要修改两个地方: 1.httpd.conf 中的 DocumentRoot 项 和 Directory 项 2.httpd-vhosts.conf 中的 DocumentRoot 项 网上找到的大部 ...
- 图论--割点--Tarjan模板
#include <iostream> #include <algorithm> #include <cstdio> #include <cstring> ...
- codeforce 266c Below the Diagonal 矩阵变换 (思维题)
C. Below the Diagonal You are given a square matrix consisting of n rows and n columns. We assume th ...
- P5057 【[CQOI2006]简单题】
洛谷P5057[CQOI2006]简单题 差分 树状数组基本操作不说了,主要想记录一下异或下的差分 a数组为每一位的真实值(假设\(a[0]=0\)),t为差分后的数组 则\(t[i]=a[i]\)^ ...
- 威联通(NAS)应用篇:自建OwnCloud网盘(百度网盘,拜拜~~~)
基础环境: 威联通一台 已安装好 ContainerStation 公网 IP(非必须) 自有公网域名 下载镜像文件 提醒:建议先把威联通的自带镜像源改为国内的阿里云镜像源,教程:https://ww ...
- H - Fire CodeForces - 864E 01背包
https://codeforces.com/problemset/problem/864/E 这个题目要把这个按照物品毁灭时间进行排序,如果时间短就要排在前面,这个是因为要保证之后的物品的拯救不会影 ...