Codeforces 363B Fence

本文介绍了一个编程问题的解决方法,即从给定的一组数值中找到连续k个数的最小和,通过滑动窗口技术实现高效求解。
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 jj + 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.

题意:n个数,寻找最小连续k个数的和

思路:sum保存k个数的和,扫描到一个数,就减去num[i-k] 即可保证连续k个和

#include <iostream>
#include <cstdio>

using namespace std;
int num[200000]; 
int main()
{
	int index; 
	int n,k; 
	int minn = 999999999 ; 
	
	cin>>n>>k; 
	long long sum=0; 
	int cnt=1 ; 
	
	num[0] =0 ; 
	for ( int i=1; i<=n;i++ ) {
		scanf("%d",&num[i]); 
		sum+=num[i]; 
		if ( cnt!=k ) ++cnt;
		else {
			sum -= num[i-k]; 
			if ( sum<minn ) {
				minn = sum; 
				index = i-k+1; 
			}
		}
	}
	cout<<index<<endl; 
	return 0;  
}



Codeforces的2000-2500分区间,以下是典型的**思维难度相对较低但极易WA**的题目清单,这些题目都满足: 1. **解法直接**(不需要复杂算法) 2. **实现易错**(边界条件/特殊判断多) 3. **数据陷阱**(有针对性构造的测试用例) --- ### 必练题目列表(附带WA原因分析) #### 1. [CF1555D - Say No to Palindromes](https://codeforces.com/problemset/problem/1555/D) (2000分) - **题意**:构造无长度≥2回文子串的字符串 - **易WA点**: - 只考虑"abc"循环,忽略"acbacb"等模式 - 未预处理所有可能的前缀方案 - **数据陷阱**: - Test 3常出现`n=5`需要多种模式组合 - **避坑代码**: ```cpp // 预处理6种可能模式 string patterns[6] = {"abc", "acb", "bac", "bca", "cab", "cba"}; ``` #### 2. [CF1593D2 - Half of Same](https://codeforces.com/problemset/problem/1593/D2) (2100分) - **题意**:找最大的k使至少半数元素同余 - **易WA点**: - 负数取模未标准化(如`-5%3`结果可能为-2) - 全相同元素时返回0而非INF - **关键测试**: - 含`[-1e9,1e9]`的极端数据 - 所有元素相同的case #### 3. [CF1469C - Building a Fence](https://codeforces.com/problemset/problem/1469/C) (2100分) - **题意**:栅栏高度严格交替 - **易WA点**: - 相邻栅栏高度差≥1的判断错误 - 未考虑首尾栅栏的特殊限制 - **反例构造**: ```python # 这个输入应该输出NO 1 3 2 1 1 1 ``` #### 4. [CF1481D - AB Graph](https://codeforces.com/problemset/problem/1481/D) (2000分) - **题意**:在AB图中构造回文路径 - **致命WA**: - `n=2`时直接判定不可行(实际可能可行) - 循环节长度计算错误 - **验证方法**: - 对`n=2``n=3`分别写特判 #### 5. [CF1374D - Zero Remainder Array](https://codeforces.com/problemset/problem/1374/D) (1900分,但数据加强后达2000+) - **题意**:通过+1操作使元素被k整除 - **易错细节**: - 相同余数需间隔k次操作 - 未处理`x≡0(mod k)`的情况 - **hack数据**: ```python 1 4 3 [0, 0, 0, 2] # 应输出5而非3 ``` --- ### 针对性训练建议 1. **数学类题目**: - 强制检查三步: ```python if a == 1: ... if b == 0: ... if all(x==arr[0]): ... ``` 2. **字符串类题目**: - 使用**暴力验证器**: ```python def is_valid(s): for i in range(len(s)-1): if s[i] == s[i+1]: return False return True ``` 3. **贪心类题目**: - 实施**交换论证法**: ```markdown 1. 假设存在更优解 2. 通过交换元素得到矛盾 3. 证明贪心的正确性 ``` --- ### 比赛实用技巧 1. **WA后检查顺序**: - 首先检查`n=1` - 然后检查全相同元素 - 最后验证最大/最小极值 2. **对拍数据生成**: ```python # 生成含负数的随机数据 import random n = random.randint(1,1e5) arr = [random.randint(-1e9,1e9) for _ in range(n)] ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值