• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
HaibaraAi
博客园    首页    新随笔    联系   管理    订阅  订阅

Codeforce Round #211 Div2 B

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 j, j + 1, ..., j + k - 1 is the minimum possible. If there are multiple such j's, print any of them.

Sample test(s)
Input
7 3 1 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.

 1 #include<cstdio>
 2 #include <cstring>
 3 using namespace std;
 4 const int INF = 1 << 30;
 5 int a[200005], s[200005];
 6 
 7 int main()
 8 {
 9     int i, j, k, n, t, sum, l, r,x;
10         scanf("%d%d", &n,&x);
11         memset(s, 0, sizeof s);
12         for (i = 1; i <= n; i++)
13         {
14             scanf("%d", &a[i]);
15             s[i] =s[i-1]+ a[i];
16         }
17         int ans = INF;
18         k = 1;
19         for (int i = 1; i + x - 1 <= n; i++){
20             if (ans > s[i + x - 1] - s[i - 1]){ans = s[i + x - 1] - s[i - 1]; k = i;}
21         }
22         printf("%d\n", k);
23     return 0;
24 }
View Code
posted @ 2013-11-11 18:35  HaibaraAi  阅读(177)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3