当前位置: 首页 > news >正文

一、基础数据结构——2.队列——3.双端队列和单调队列2

参考资料:《算法竞赛》,罗勇军 郭卫斌 著
本博客作为阅读本书的学习笔记,仅供交流学习。
建议关注 罗勇军老师博客

3. 单调队列与最大子序和问题

不限制子序列长度问题——贪心法或动态规划

HDOJ 1003 MAX SUM

Max Sum Time Limit: 2000/1000 MS (Java/Others)
Memory Limit: 65536/32768 K (Java/Others)

Problem Description
Given a sequence a[1],a[2],a[3]…a[n], your job is to calculate the max sum of a sub-sequence. For example, given (6,-1,5,4,-7), the max sum in this sequence is 6 + (-1) + 5 + 4 = 14.

Input

The first line of the input contains an integer T(1<=T<=20) which means the number of test cases. Then T lines follow, each line starts with a number N(1<=N<=100000), then N integers followed(all the integers are between -1000 and 1000).

Output
For each test case, you should output two lines. The first line is “Case #:”, # means the number of the test case. The second line contains three integers, the Max Sum in the sequence, the start position of the sub-sequence, the end position of the sub-sequence. If there are more than one result, output the first one. Output a blank line between two cases.

Sample Input
2
5 6 -1 5 4 -7
7 0 6 -1 1 -6 7 -5

Sample Output
Case 1: 14 1 4
Case 2: 7 1 6

Author
Ignatius.L

  1. 贪心法
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x7fffffff;int main() {int t; cin>>t;//测试用例个数for (int i = 1; i <= t; ++i) {int n; cin>>n;int maxsum = -INF;//最大子序和,初始设为一个最小的int负数int start = 1, end=1, p=1; //起点,终点,扫描位置int sum=0;for (int j = 1; j <= n; ++j) {int a; cin>>a; sum+=a;//读入一个元素,累加if (sum>maxsum){maxsum=sum;start=p;end=j;}if (sum<0){//扫描到j时,若前面的最大子序和是负数,从下一个重新开始求和sum=0;p=j+1;}}printf("Case %d:\n",i);printf(" %d %d %d\n",maxsum,start,end);if (i!=t) cout<<endl;}return 0;
}
  1. 动态规划
#include <bits/stdc++.h>
using namespace std;
int dp[100005];//dp[i]:以第i个数为结尾的最大值
int main(){int t; cin>>t;//测试用例个数for (int k = 1; k <= t; ++k) {int n; cin>>n;for (int i = 1; i <= n; ++i) cin>>dp[i];//用dp[]存储数据a[]int start=1, end=1, p=1;//起点,终点,扫描位置int maxsum = dp[1];for (int i = 2; i <= n; ++i) {if (dp[i-1]+dp[i]>=dp[i])//转移方程dp[i]=max(dp[i-1]+a[i],a[i])dp[i]=dp[i-1]+dp[i];//dp[i-1]+a[i]比a[i]大else p=i;//a[i]更大,则dp[i]=a[i]if (dp[i]>maxsum){//dp[i]更大maxsum=dp[i];start=p;end=i;//p开始,i结尾}}printf("Case %d:\n",k);printf(" %d %d %d\n",maxsum,start,end);if (k!=t) cout<<endl;}return 0;
}

限制子序列长度问题——单调队列

#include <bits/stdc++.h>
using namespace std;
deque<int> dq;
int s[100005];
int main(){int n,m;scanf("%d %d",&n,&m);for (int i = 1; i < n; ++i) scanf("%lld",&s[i]);for (int i = 1; i < n; ++i) s[i]=s[i-1]+s[i];//计算前缀和int ans = -1e8;dq.push_back(0);for (int i = 1; i <= n; ++i) {while(!dq.empty()&&dq.front()<i-m) dq.pop_front();//队头超过m范围:删头if (dq.empty()) ans = max(ans,s[i]);else ans= max(ans,s[i]-s[dq.front()]);//队头就是最小的s[k]while(!dq.empty()&&s[dq.back()]>=s[i]) dq.pop_back();//队尾大于s[i]:去尾dq.push_back(i);}printf("%d\n",ans);return 0;
}
http://www.lryc.cn/news/298757.html

相关文章:

  • Stable Diffusion 模型下载:Samaritan 3d Cartoon(撒玛利亚人 3d 卡通)
  • 【软件工程导论】实验二——编制数据字典(数字化校园系统案例分析)
  • 耳机壳UV树脂制作私模定制耳塞适合什么样的人使用呢?
  • 第三百一十回
  • 海量数据处理商用短链接生成器平台 - 4
  • 基于CNN+LSTM深度学习网络的时间序列预测matlab仿真
  • 如何控制系统安全 或 控制流氓软件
  • 【Docker】Docker Container(容器)
  • Amazon CodeWhisperer 免费 AI 代码生成助手体验分享
  • Spring Cloud Gateway 网关路由
  • 【Spring学习】Spring Data Redis:RedisTemplate、Repository、Cache注解
  • C语言:内存函数
  • Go+:一种简单而强大的编程语言
  • 【开源】SpringBoot框架开发数字化社区网格管理系统
  • Lua可变参数函数
  • Nginx实战:3-日志按天分割
  • springmvc中的数据提交方式
  • unity2017 遇到visual studio 2017(社区版) 30日试用期到了
  • Netty应用(六) 之 异步 Channel
  • STM32CubeMx+MATLAB Simulink串口输出实验,UART/USART串口测试实验
  • 【51单片机】串口通信实验(包括波特率如何计算)
  • Kafka零拷贝技术与传统数据复制次数比较
  • npm ERR! network This is a problem related to network connectivity.
  • 【SQL高频基础题】619.只出现一次的最大数字
  • STM32F1 - GPIO外设
  • 新增同步管理、操作日志模块,支持公共链接分享,DataEase开源数据可视化分析平台v2.3.0发布
  • 跟着pink老师前端入门教程-day19
  • ChatGPT学习第一周
  • 爬爬爬——今天是浏览器窗口切换和给所选人打钩(自动化)
  • Netty应用(五) 之 Netty引入 EventLoop