Maximum Absurdity(dp)
2015-12-17 21:46
453 查看
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 test(s)
input
5 2
3 6 1 1 6
output
1 4
input
6 2
1 1 1 1 1 1
output
1 3
Note
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.
给一个长度为n的序列,找两个不相交的长度为k的子串,使他们的和最大,输出两个子串的起始位置。
处理不好就超时了= = ,之前超时的忘了,第二次写一下就想到正解了。
首先预处理出已这个位置为起始位置长度为k的和,
对于每个i,i表示后面那个长度为k的子串,那么对于它来说,最大的子串和就是前i个位置中最大的那一个,就是第一个子串的位置。
问题转化成快速找前i个中最大的子串,然后发现这个在遍历i的时候就可以快速处理,
i每次改变1,那么前i个中最大的子串其实只需要用前i - 1个中最大子串和新出现的子串比较即可。
具体理解可以画一画图示意一下。
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 test(s)
input
5 2
3 6 1 1 6
output
1 4
input
6 2
1 1 1 1 1 1
output
1 3
Note
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.
给一个长度为n的序列,找两个不相交的长度为k的子串,使他们的和最大,输出两个子串的起始位置。
处理不好就超时了= = ,之前超时的忘了,第二次写一下就想到正解了。
首先预处理出已这个位置为起始位置长度为k的和,
对于每个i,i表示后面那个长度为k的子串,那么对于它来说,最大的子串和就是前i个位置中最大的那一个,就是第一个子串的位置。
问题转化成快速找前i个中最大的子串,然后发现这个在遍历i的时候就可以快速处理,
i每次改变1,那么前i个中最大的子串其实只需要用前i - 1个中最大子串和新出现的子串比较即可。
具体理解可以画一画图示意一下。
#include<cstdio> #include<cstring> #include<iostream> #include<queue> #include<vector> #include<algorithm> #include<string> #include<cmath> #include<set> #include<map> #include<vector> using namespace std; typedef long long ll; const int inf = 0x3f3f3f3f; const int maxn = 1005; ll a[200005],dp[200005]; int main() { #ifdef LOCAL freopen("C:\\Users\\巍巍\\Desktop\\in.txt","r",stdin); //freopen("C:\\Users\\巍巍\\Desktop\\out.txt","w",stdout); #endif // LOCAL ll n,k; scanf("%lld%lld",&n,&k); for(ll i = 1;i <= n;i++) scanf("%lld",&a[i]); ll ans = 0; for(ll i = 1;i <= k;i++) ans = ans + a[i]; dp[1] = ans; for(ll i = 2;i <= n - k + 1;i++) { ans = ans + a[i + k - 1] - a[i - 1]; dp[i] = ans; } ans = dp[1] + dp[k + 1]; ll pos = 1,pos1,pos2; pos1 = 1;pos2 = k + 1; for(ll i = k + 2;i <= n - k + 1;i++) { if(dp[i - k] > dp[pos])pos = i - k; if(dp[i] + dp[pos] > ans) { ans = dp[i] + dp[pos]; pos1 = pos; pos2 = i; } } printf("%lld %lld\n",pos1,pos2); return 0; }
相关文章推荐
- 基于org.json的快速构建和解析json文本扩展工具
- 更改Eclipse中js文件的编辑方式,从而可以利用ctrl键快速定位方法
- 黑马程序员——网络编程之TCP
- 你应当如何学习C++(以及编程)
- javascript:理解DOM事件
- Duilib源码分析(六)整体流程
- React属性和状态对比
- AngularJS 重复HTML元素
- 数学之美番外篇:平凡而又神奇的贝叶斯方法
- 中断描述符表的初始化
- 最小公倍数
- IO(过滤器流)
- 编码问题
- 【转】Quartz.NET
- JS子父窗口互相取值赋值详解介绍
- IOS算法面试题(2)——任意阶乘的最后0的个数、超大数相加集总
- maven项目打包注意事项
- 数据结构实验之查找三:树的种类统计【OJ--3375】
- 二分查找算法
- Struts2执行流程