2394: 【入门】【P1614】爱与愁的心痛

内存限制:128 MB 时间限制:1.000 S
评测方式:文本比较 命题人:
提交:39 解决:16

题目描述

最近有 lns="http://www.w3.org/1998/Math/MathML"> 个不爽的事,每句话都有一个正整数刺痛值(心理承受力极差)。爱与愁大神想知道连续 lns="http://www.w3.org/1998/Math/MathML"> 个刺痛值的和的最小值是多少,但是由于业务繁忙,爱与愁大神只好请你编个程序告诉他。

输入

第一行有两个用空格隔开的整数,分别代表 lns="http://www.w3.org/1998/Math/MathML"> 和 lns="http://www.w3.org/1998/Math/MathML">

第 lns="http://www.w3.org/1998/Math/MathML">2 到第 lns="http://www.w3.org/1998/Math/MathML">(+1) 行,每行一个整数,第 lns="http://www.w3.org/1998/Math/MathML">(+1) 行的整数 lns="http://www.w3.org/1998/Math/MathML"> 代表第 lns="http://www.w3.org/1998/Math/MathML"> 件事的刺痛值 lns="http://www.w3.org/1998/Math/MathML">

输出

输出一行一个整数,表示连续 lns="http://www.w3.org/1998/Math/MathML"> 个刺痛值的和的最小值是多少。

样例输入 复制

8 3
1
4
7
3
1
2
4
3

样例输出 复制

6

提示

数据规模与约定

  • 对于 lns="http://www.w3.org/1998/Math/MathML">30% 的数据,保证 lns="http://www.w3.org/1998/Math/MathML">20
  • 对于 lns="http://www.w3.org/1998/Math/MathML">60% 的数据,保证 lns="http://www.w3.org/1998/Math/MathML">100
  • 对于 lns="http://www.w3.org/1998/Math/MathML">90% 的数据,保证 lns="http://www.w3.org/1998/Math/MathML">103
  • 对于 lns="http://www.w3.org/1998/Math/MathML">100% 的数据,保证 lns="http://www.w3.org/1998/Math/MathML">03×103lns="http://www.w3.org/1998/Math/MathML">1100


#include<bits/stdc++.h>
using namespace std;
int a[10000];
int main(){
	int n,m,ans=300000; 
	/*也可以写成:
	ans=INT_MAX 最大的int
	ans=0x3f3f3f3f 十进制是1061109567,是10^9级别的,所以它可以作为无穷大使用又不致出现数据大于无穷大的情形。
	*/
	cin>>n>>m;
	for (int i=1;i<=n;i++){
		cin>>a[i];
	}
	for (int i=1;i<=n-m+1;i++) {
		int s=0;
		for (int j=0;j<m;j++){
			s+=a[i+j];
		}
		if (s<ans) ans=s;  //或者写成 ans=min(s,ans);
	}
	cout<<ans;
	return 0;
}