题面在这里!

 

    显然直接枚举左端点(右端点)就OK啦。

 

#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include<cmath>
#define ll long long
using namespace std;
const int N=1e5+5;
 
int n,a[N],ans=1e9,k;
 
inline int Get(int x,int y){
	if(x>=0) return y;
	if(y<=0) return -x;
	return y-x+min(y,-x);
}
 
int main(){
    scanf("%d%d",&n,&k);
    for(int i=1;i<=n;i++) scanf("%d",a+i);
    
    sort(a+1,a+n+1);
    
    for(int i=k;i<=n;i++) ans=min(ans,Get(a[i-k+1],a[i]));
    
    printf("%d\n",ans);
    return 0;
}

  

相关文章:

  • 2021-11-23
  • 2022-01-21
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-08-27
  • 2022-01-26
  • 2022-12-23
猜你喜欢
  • 2021-06-27
  • 2021-07-26
  • 2022-12-23
  • 2021-06-04
  • 2021-11-07
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案