Codeforces Problem 332B
来源:互联网 发布:社会网络分析大数据 编辑:程序博客网 时间:2024/05/22 14:35
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.
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).
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.
5 23 6 1 1 6
1 4
6 21 1 1 1 1 1
1 3
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的区间和,以及在该点之后最大的区间和及其位置,再进行遍历
#include<iostream>#include<string.h>#include<stdio.h>using namespace std;#define LL long longconst int maxn = 2 * 1e5 +100;LL n,k,a[maxn],sum[maxn],mx[maxn],pos[maxn],i;int main(){ scanf("%lld%lld",&n,&k); for(i=1;i<=n;i++){ scanf("%lld",&a[i]); sum[i] = sum[i-1] + a[i]; if(i>=k) sum[i] -= a[i-k]; } for(i=n;i>=k;i--){ if(sum[i]>=mx[i+1]){ mx[i] = sum[i]; pos[i] = i; } else{ mx[i] = mx[i+1]; pos[i] = pos[i+1]; } } LL ans = 0,pos1 = 0,pos2 = 0; for(i=k;i<=n;i++){ LL sm = sum[i] + mx[i+k]; if(ans<sm){ ans = sm; pos1 = i-k+1; pos2 = pos[i+k]-k+1; } } printf("%lld %lld\n",pos1,pos2); return 0;}
- Codeforces Problem 332B
- Codeforces Problem 332B: Backward Linear Scan
- Problem - 216B - Codeforces
- Problem - 218B - Codeforces
- codeforces problem/408/B
- Codeforces Problem - 50B
- Problem - 376B - Codeforces
- problem - 616B -codeforces
- Codeforces Problem 333B
- codeforces-Problem 193B - Xor
- Codeforces Problem 499B. Lecture
- codeforces 278B New Problem
- Codeforces Problem - 617B Chocolate
- Problem--158B--Codeforces--Taxi
- codeforces 782/problem/B [二分]
- codeforces/contest/797/problem/B
- codeforces/contest/798/problem/B
- B. Secret Combination codeforces-problem-496B
- HDFS数据不均衡解决方案:基于剩余空间大小的均衡策略
- 从奇虎360离职去创业型公司当CTO是一种怎样的体验?
- 设计模式之组合模式
- Java线程总结
- 快速排序
- Codeforces Problem 332B
- 使用cookie实现跨域系统单点登录
- CI持续集成系统环境--Gitlab+Gerrit+Jenkins完整对接
- 值传递String
- CTF小练
- java_归并排序
- 笑出腹肌的程序猿搞笑趣图
- 开源许可证教程
- Hive之——自定义函数