結果

問題 No.2548 Problem Selection
ユーザー millfi_
提出日時 2023-11-25 13:29:07
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 50 ms / 2,000 ms
コード長 1,136 bytes
コンパイル時間 2,291 ms
コンパイル使用メモリ 172,240 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-09-26 10:27:07
合計ジャッジ時間 3,437 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 25
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

#define overload4(_1, _2, _3, _4, name, ...) name
#define rep1(n) for(int i = 0; i < (int)(n); ++i)
#define rep2(i, n) for(int i = 0; i < (int)(n); ++i)
#define rep3(i, a, b) for(int i = (a); i < (int)(b); ++i)
#define rep4(i, a, b, c) for(int i = (a); i < (int)(b); i += (c))
#define rep(...) overload4(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__)
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using u16 = uint16_t;
const ll INF = 1 << 30;
int main(){
    size_t N{}; size_t M{};
	cin >> N >> M;
	vector<ll> problems(N);
	rep(i,N)cin >> problems.at(i);
	sort(problems.begin(), problems.end());
	ull cnt = 0;
	rep(i,M-1){
		cnt += abs(problems.at(i) - problems.at(i+1)) * abs(problems.at(i) - problems.at(i+1));
	}
	ull S = cnt;
	rep(i,1,N-M+1){
		size_t r = M+i-1;
		size_t l = i;
		//cout << "l,r = " << l << " " << r << endl;
		cnt -= abs(problems.at(l) - problems.at(l-1)) * abs(problems.at(l) - problems.at(l-1));
		cnt += abs(problems.at(r) - problems.at(r-1)) * abs(problems.at(r) - problems.at(r-1));
		S = min(S, cnt);
	}
	cout << S << endl;
}
0