Fence

Fence

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.

Examples
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.

#include <iostream>
#include <cstring>
#include "algorithm"

using namespace std;

int xx[150001];
int main()
{
    int n,k;
    while(cin>>n>>k)
    {
        int a=0;
        int b=0;
        int c=0;
        for(int i =0; i<n; i++)
        {
            cin>>xx[i];
            a=a+xx[i];
            if(i==k-1)
            {
                b=a;
                a=a-xx[i-k+1];
                c=i-k+2;
            }
            if(i>k-1)
            {
                if(a<b)
                {
                    b=a;
                    c=i-k+2;
                }
                a=a-xx[i-k+1];
            }
        }
        cout<<c<<endl;
    }
    return 0;
}

错误范例(知道为什么错了吗)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define maxn 1111111

int a[maxn];
int sum[maxn];

int main()
{

    memset(sum,0,sizeof(sum));
    int n,k;
    scanf("%d%d",&n,&k);
    for(int i=1;i<=n;i++){
        scanf("%d",&a[i]);
    }
    for(int i=1;i<=n-k+1;i++){
        for(int j=i;j<=i+k-1;j++){
            sum[i]+=a[j];
        }
    }
    int min=sum[1];
    int count=1;
    for(int i=1;i<=n-k+1;i++){
        if(sum[i]<min){
            min=sum[i];
            count=i;
        }
    }
    printf("%d\n",count);
    return 0;
}

用C语言写的正确代码如下

#include <string.h>
#include <stdio.h>



int xx[150001];
int main()
{
    int n,k;
    scanf("%d%d",&n,&k);

    int a=0;
    int b=0;
    int c=0;
    for(int i =0; i<n; i++)
    {
       scanf("%d",&xx[i]);
        a=a+xx[i];
        if(i==k-1)
        {
            b=a;
            a=a-xx[i-k+1];
            c=i-k+2;
        }
        if(i>k-1)
        {
            if(a<b)
            {
                b=a;
                c=i-k+2;
            }
            a=a-xx[i-k+1];
        }
    }
    printf("%d\n",c);

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

veit sun

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值