B. Fence
来源:互联网 发布:c语言ide是什么意思 编辑:程序博客网 时间:2024/06/05 16:21
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.
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 kconsecutive 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).
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.
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.
7 31 2 6 1 1 7 1
3
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.
解题说明:此题要求一列数中连续k个数之和最小的情况,用循环判断即可
#include<stdio.h>int main(){int i,j,n,m,k;int h[1000000];int ind =0;int sum=0;int min =0;scanf("%d%d",&n,&k);for(i=0;i<n;i++){scanf("%d",&h[i]);}for(i=0;i<k;i++){sum += h[i];}min=sum;for(i=k;i<n;i++){sum =sum+h[i]-h[i-k];if(sum<min){min=sum;ind = i-k+1;}}printf("%d\n",ind+1);return 0;}
- B. Fence
- B. Fence
- codeforces #145 B Fence
- CodeForces - 363B Fence
- CodeForces 363B - Fence
- Codeforces 363B Fence
- B. Fence----前缀和
- codeforces 363B Fence
- codeforces 240B - Fence DP
- Codeforces 363B Fence-暴力
- codeforces 363B round211 div2 B. Fence
- codeforces 363B round211 div2 B. Fence
- CF#202DIV2:B. Color the Fence
- 349B - Color the Fence (贪心)
- Codeforces #211 (Div. 2) B. Fence
- CF 349B - Color the Fence
- CodeForces 349B - Color the Fence
- cf 349 B. Color the Fence(贪心)
- Varnish简单介绍
- MFC 消息类型 (1)
- ios 避免弹出键盘挡住屏幕
- 黑马程序员---面试题破解之二_银行业务调度系统
- Storm集群安装
- B. Fence
- Linux时间子系统之六:高精度定时器(HRTIMER)的原理和实现
- linux内核中读写文件
- C++必知必会之(8)指向指针的指针
- 算法习题66:颠倒栈
- Maven按照目标环境生成指定可执行的jar
- Syntax error: word unexpected (expecting ")")错误的解决方法
- 仿网易新闻效果源码分析
- linux 查看文件系统