CodeForces 363B Fence

Fence

Time Limit: 1000ms

Memory Limit: 262144KB

This problem will be judged on CodeForces. Original ID: 363B
64-bit integer IO format: %I64d      Java class name: (Any)

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.

Sample Input

Input

7 31 2 6 1 1 7 1

Output

3

Hint

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.

Source

Codeforces Round #211 (Div. 2)

解题:瞎搞

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int maxn = 200010;
 4 int n,k,sum[maxn];
 5 int main(){
 6     while(~scanf("%d %d",&n,&k)){
 7         int theMin = INT_MAX,idx = -1;
 8         for(int i = 1; i <= n; ++i){
 9             scanf("%d",sum+i);
10             sum[i] += sum[i-1];
11             if(i >= k && sum[i] - sum[i-k] < theMin){
12                 theMin = sum[i] - sum[i-k];
13                 idx = i - k + 1;
14             }
15         }
16         printf("%d\n",idx);
17     }
18     return 0;
19 }

时间: 2024-12-16 16:36:39

CodeForces 363B Fence的相关文章

codeforces 448CPainting Fence

题目:codeforces 448CPainting Fence 题意:n个1* a [ i ] 的木板,把他们立起来,变成每个木板宽为1长为 a [ i ] 的栅栏,现在要给栅栏刷漆,刷子宽1,每一刷子可以刷任意长,现在让你求最少需要多少刷子? 分析:题目看似没有头绪,仔细分析的话其实很简单 首先,我们假如每次都刷一个木板,即一竖行,那么需要n次刷完,可见这是一个ans的最大值.就是最差的情况下我这样刷最多为n刷. 其次:如果我们选择一横行的刷,而n个木板中最短的为min,那么我们可以花min

codeforces 232D Fence

John Doe has a crooked fence, consisting of n rectangular planks, lined up from the left to the right: the plank that goes i-th (1 ≤ i ≤ n)(from left to right) has width 1 and height hi. We will assume that the plank that goes i-th (1 ≤ i ≤ n) (from

Codeforces 659G Fence Divercity dp

Fence Divercity 我们设a[ i ] 为第 i 个围栏被切的最靠下的位置, 我们发现a[ i ] 的最大取值有一下信息: 如果从i - 1过来并在 i  结束a[ i ] = min(h[ i - 1], h[ i ]) 如果从i - 1过来并延续到i + 1,   a[ i ] = min(h[ i - 1 ], h[ i ], h[ i + 1 ]) 如果从 i 开始 i 结束 a[ i ] = h[ i ] 如果从 i 开始并延续到 i + 1,   a[ i ] = min

16.05.25-16.06.10 题集

继2016.05.24续: codeforces 651B. Beautiful Paintings-简单 http://codeforces.com/problemset/problem/651/B 大意:给出一个序列,求解其任意排列中满足ai?+?1?>?ai 的元素个数最大和. 分析:理想情况下,无重复元素的0从小到大的排列,满足条件的元素个数最多,是n-1. 非理想情况下还有重复元素,只要不断提取出重复的这些元素归到另一集合再这样讨论即可. #include <iostream>

Codeforces Round #256 (Div. 2) C. Painting Fence(分治贪心)

题目链接:http://codeforces.com/problemset/problem/448/C ---------------------------------------------------------------------------------------------------------------------------------------------------------- 欢迎光临天资小屋:http://user.qzone.qq.com/593830943

Codeforces 484E Sign on Fence(可持久化线段树+二分)

题目链接:Codeforces 484E Sign on Fence 题目大意:给定给一个序列,每个位置有一个值,表示高度,现在有若干查询,每次查询l,r,w,表示在区间l,r中, 连续最长长度大于w的最大高度为多少. 解题思路:可持久化线段树维护区间合并,前端时间碰到一题可持久化字典树,就去查了一下相关论文,大概知道了是 什么东西. 将高度按照从大到小的顺序排序,然后每次插入一个位置,线段树维护最长连续区间,因为插入是按照从大到小的顺 序,所以每次的线段树中的连续最大长度都是满足高度大于等于当

Codeforces Round #256 (Div. 2)——Painting Fence

题目连接 题意: n个木条,输入n个木条的高度,每个木条的宽度均为1.现在有长宽均为1的刷子,每次可以选择横着刷或者竖着刷,每次刷的时候不能离开木条,问将所有木条均涂色至少需要刷几次.(刷的时候可以经过已经被刷过的地方) n (1?≤?n?≤?5000),1?≤?ai(高度)?≤?109 分析: 分析一下横着和竖着的关系:假设现在已经有一个操作集合S是答案,那么集合中的操作顺序是可以改变的,即横和竖的顺序可以改变(因为可以经过已经涂色的木条),那么不妨先横着涂色.对于当前[l,r]的区间,答案不

Codeforces Round #256 (Div. 2/C)/Codeforces448C_Painting Fence(分治)

解题报告 给篱笆上色,要求步骤最少,篱笆怎么上色应该懂吧,,,刷子可以在横着和竖着刷,不能跳着刷,,, 如果是竖着刷,应当是篱笆的条数,横着刷的话,就是刷完最短木板的长度,再接着考虑没有刷的木板,,, 递归调用,,, #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #define inf 999999999999999 using namespace

Codeforces 484E Sign on Fence(是持久的段树+二分法)

题目链接:Codeforces 484E Sign on Fence 题目大意:给定给一个序列,每一个位置有一个值,表示高度,如今有若干查询,每次查询l,r,w,表示在区间l,r中, 连续最长长度大于w的最大高度为多少. 解题思路:可持久化线段树维护区间合并,前端时间碰到一题可持久化字典树,就去查了一下相关论文,大概知道了是 什么东西. 将高度依照从大到小的顺序排序,然后每次插入一个位置,线段树维护最长连续区间,由于插入是依照从大到小的顺 序,所以每次的线段树中的连续最大长度都是满足高度大于等于