动态规划 求两个没交集的区间的和的最大值的范围的左边界
来源:互联网 发布:js删除json数组 对象 编辑:程序博客网 时间:2024/06/08 10:00
Description
Reforms continue entering Berland. For example, during yesterday sitting the Berland Parliament approved as much as n laws (each law has been assigned a unique number from 1 to n). Today all these laws were put on the table of the President of Berland, G.W. Boosch, to be signed.
This time mr. Boosch plans to sign 2k laws. He decided to choose exactly two non-intersecting segments of integers from 1 to n of length k and sign all laws, whose numbers fall into these segments. More formally, mr. Boosch is going to choose two integers a, b(1 ≤ a ≤ b ≤ n - k + 1, b - a ≥ k) and sign all laws with numbers lying in the segments [a; a + k - 1] and [b; b + k - 1] (borders are included).
As mr. Boosch chooses the laws to sign, he of course considers the public opinion. Allberland Public Opinion Study Centre (APOSC) conducted opinion polls among the citizens, processed the results into a report and gave it to the president. The report contains the absurdity value for each law, in the public opinion. As mr. Boosch is a real patriot, he is keen on signing the laws with the maximum total absurdity. Help him.
Input
The first line contains two integers n and k (2 ≤ n ≤ 2·105, 0 < 2k ≤ n) — the number of laws accepted by the parliament and the length of one segment in the law list, correspondingly. The next line contains n integers x1, x2, ..., xn — the absurdity of each law (1 ≤ xi ≤ 109).
Output
Print two integers a, b — the beginning of segments that mr. Boosch should choose. That means that the president signs laws with numbers from segments [a; a + k - 1] and [b; b + k - 1]. If there are multiple solutions, print the one with the minimum number a. If there still are multiple solutions, print the one with the minimum b.
Sample Input
5 23 6 1 1 6
1 4
6 21 1 1 1 1 1
1 3
Hint
In the first sample mr. Boosch signs laws with numbers from segments [1;2] and [4;5]. The total absurdity of the signed laws equals 3 + 6 + 1 + 6 = 16.
In the second sample mr. Boosch signs laws with numbers from segments [1;2] and [3;4]. The total absurdity of the signed laws equals 1 + 1 + 1 + 1 = 4.
这道题一开始用暴力枚举肯定超时,就去找了下题解,原来是用动归做,
sum[i]储存送1到i的和,q[i]中储存的是从1到i中区间为k的的最大值以及区间和最大时其右边端点的位置
sum[i]-sum[i-k]代表从i-k+1到i的区间的和,接着只要判断这个是不是比之前大,是的话就将这个大的赋值进去q,否则将上一个的值赋给q
#include <iostream>#include <cstring>#include <cmath>#include <algorithm>long long sum[500001];struct pos{long long max_;int pos;}q[500001];using namespace std;int main(){memset(q,0,sizeof(q));int n,k,l,r;cin >> n >> k;sum[0]=0;long long mm=-1;for(int i=1; i <= n; ++i){cin >> sum[i];sum[i]+=sum[i-1];}for(int i=k; i <= n; ++i){if(sum[i]-sum[i-k]>q[i-1].max_){q[i].max_=sum[i]-sum[i-k];q[i].pos=i;}else{q[i].max_=q[i-1].max_;q[i].pos=q[i-1].pos;}if(sum[i]-sum[i-k]+q[i-k].max_>mm){mm=sum[i]-sum[i-k]+q[i-k].max_;l=q[i-k].pos-k+1;r=i-k+1;}}cout << l << " " << r << endl;}
- 动态规划 求两个没交集的区间的和的最大值的范围的左边界
- 求两个数组的交集
- 求两个数组的交集
- 求两个数组的交集
- 求两个数组的交集
- 求两个数组的交集
- 求两个数组的交集
- 求两个字符串的交集
- 求两个Collection的交集
- 求两个集合的交集
- 求两个数组的交集
- 求两个数组的交集
- 求两个数组的交集
- 求两个数组的交集
- 求两个数组的交集
- 求两个数组的交集
- 求两个集合的交集
- 求两个数组的交集
- Adobe Reader for Unix 安装(支持中文)
- html表格、表单元素、img标签、图像映射技术、html4引入flash
- 在继承中关于super的详解
- (一)四轴——PPM信号与接收
- ACM练习—6174问题
- 动态规划 求两个没交集的区间的和的最大值的范围的左边界
- 《Java程序设计》第二次作业:MySQL数据库及Java操作MySQL数据库
- 黑马程序员(八)网络编程
- 欢迎使用CSDN-markdown编辑器
- ACM练习—谁获得了最高奖学金
- MFC对话框风格和工程字符集的关系
- 循环链表&线性表(5.15)
- C语言学习第三天—控制流
- 网页中调起本地程序系列——简介篇