You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.
# include <bits/stdc++.h>
using namespace std ;
// 时间复杂度: 10000*10000=1e8, C++一秒可过
/**
测试用例I:
3 2
3 6 1
答案
3
测试用例II:
4 2
2 6 11 18
答案:
4
解释:
将2调到6, 需要4步。
也可以把2调到3, 1步。
把6降到3, 3步, 共4步。
测试用例III:
5 3
2 6 11 18 14
答案:
7
解释:
将11调到14, 需要3步
将18调到14, 需要4步, 共7步。
测试用例IV
5 3
1 2 7 8 12
答案: 5
解释:
最终结果是8,
7+1=8, 1步
8不需要动, 0步
12-8=4, 4步
共1+0+4=5步
*/
const int INF = 0x3f3f3f3f ;
const int N = 1010 ;
int a [ N ] ;
int n , k ;
int res = INF ;
int main ( ) {
cin > > n > > k ;
for ( int i = 0 ; i < n ; i + + ) cin > > a [ i ] ;
for ( int i = 0 ; i < = n - k ; i + + ) { // 枚举起点
vector < int > b ;
for ( int j = i ; j < = i + k - 1 ; j + + ) b . push_back ( a [ j ] ) ; // 将每个可用范围复制到数组b中
sort ( b . begin ( ) , b . end ( ) ) ; // 排序
int cnt = 0 ; // 变更次数
for ( int j = 0 ; j < b . size ( ) ; j + + ) cnt + = abs ( b [ b . size ( ) / 2 ] - b [ j ] ) ; // 枚举每个数字与中位数对比
res = min ( res , cnt ) ; // 记录最少变更次数
}
printf ( " %d \n " , res ) ;
return 0 ;
}