Codeforces 363B Fence

来源:互联网 发布:appbook for mac 编辑:程序博客网 时间:2024/06/07 07:13
B. Fence
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

There is a fence in front of Polycarpus's home. The fence consists of n planks of the same width which go one after another from left to right. The height of the i-th plank is hi meters, distinct planks can have distinct heights.

Fence for n = 7 and h = [1, 2, 6, 1, 1, 7, 1]

Polycarpus has bought a posh piano and is thinking about how to get it into the house. In order to carry out his plan, he needs to take exactly k consecutive planks from the fence. Higher planks are harder to tear off the fence, so Polycarpus wants to find such k consecutive planks that the sum of their heights is minimal possible.

Write the program that finds the indexes of k consecutive planks with minimal total height. Pay attention, the fence is not around Polycarpus's home, it is in front of home (in other words, the fence isn't cyclic).

Input

The first line of the input contains integers n and k (1 ≤ n ≤ 1.5·105, 1 ≤ k ≤ n) — the number of planks in the fence and the width of the hole for the piano. The second line contains the sequence of integers h1, h2, ..., hn(1 ≤ hi ≤ 100), where hi is the height of the i-th plank of the fence.

Output

Print such integer j that the sum of the heights of planks jj + 1, ..., j + k - 1 is the minimum possible. If there are multiple such j's, print any of them.

Examples
input
7 31 2 6 1 1 7 1
output
3
Note

In the sample, your task is to find three consecutive planks with the minimum sum of heights. In the given case three planks with indexes 3, 4 and 5 have the required attribute, their total height is 8.

题意:n个数,寻找最小连续k个数的和

思路:sum保存k个数的和,扫描到一个数,就减去num[i-k] 即可保证连续k个和

#include <iostream>#include <cstdio>using namespace std;int num[200000]; int main(){int index; int n,k; int minn = 999999999 ; cin>>n>>k; long long sum=0; int cnt=1 ; num[0] =0 ; for ( int i=1; i<=n;i++ ) {scanf("%d",&num[i]); sum+=num[i]; if ( cnt!=k ) ++cnt;else {sum -= num[i-k]; if ( sum<minn ) {minn = sum; index = i-k+1; }}}cout<<index<<endl; return 0;  }



0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 苹果6dns被劫持怎么办 工地欠工资不给怎么办 买的狗得病了怎么办 剖腹产9天刀口痒怎么办 剖腹产6天刀口痒怎么办 剖腹产2年刀口痒怎么办 破腹产9天刀口痒怎么办 破腹产7天刀口痒怎么办 剖腹产8天伤口痒怎么办 刨腹产9天刀口痒怎么办 破腹产8天刀口痒怎么办 二胎刀口1.1mm薄怎么办 土豪养成记2闪退怎么办 爱上不该爱的人怎么办? 有妇之夫爱上我,怎么办 全身起疙瘩很痒怎么办 怀孕了肚子很痛怎么办 月经来肚子疼的厉害怎么办 孕妇9个月肚子疼怎么办 斗鱼身上长白点怎么办 8个月宝宝肚子疼怎么办 1岁半宝宝肚子痛怎么办 3岁宝宝肚子疼怎么办啊 吃的太辣肚子疼怎么办 2岁半宝宝肚子疼怎么办 2岁宝宝肚子痛哭怎么办 怀孕3个月拉肚子怎么办 一岁宝宝消化不良拉肚子怎么办 后背长好多痘痘怎么办 身上起疹子很痒怎么办 背上长好多痘痘怎么办 月经两三个月不来怎么办 来月经很少是褐色的怎么办 例假一天就没了怎么办 月经来一天就没了怎么办 假体隆胸8年怀孕怎么办 1岁宝宝长湿疹怎么办 2017卓达破产后怎么办 8岁儿童反复发烧怎么办 工商年报报错了怎么办 买房契税票丢了怎么办