提出 #23007922


ソースコード 拡げる

#include <iostream>
#include <iomanip>
#include <vector>
#include <cmath>
#include <algorithm>
#include <set>
#include <utility>
#include <queue>
#include <map>
#include <assert.h>
#include <stack>
#include <string>
#include <ctime>
#include <chrono>
#include <random>
#define int long long int
using namespace std;

const int INF=1e16;

void solve()
{
	int n;
	cin>>n;
	vector<int> v(n+1);
	for (int j=1; j<=n; j++) cin>>v[j];
	if (n <= 2)
	{
		cout << 0 << '\n';
		return;
	}
	v[0]=-INF;
	
	sort(v.begin(), v.end());
	
	int res=v[n]-v[1];
	
	for (int j=1; j<=n/2; j++) //pref-suff len
	{
		int mx=-INF, mn=+INF;
		for (int i=1; i<=j; i++)
		{
			mx=max(mx, v[i]+v[n-i+1]);
			mn=min(mn, v[i]+v[n-i+1]);
		}
		for (int i=j+1; i<=(n+1)/2; i++)
		{
			mx=max(mx, max(v[i], v[n-i+1]));
			mn=min(mn, min(v[i], v[n-i+1]));
		}
		res=min(res, mx-mn);
	}

	for (int j = 2; j <= n; j += 2)
	{
		int mx = -INF, mn = +INF;
		for (int i = 1; i <= j / 2; i++)
		{
			mx = max(mx, v[i]+v[j-i+1]);
			mn = min(mn, v[i]+v[j-i+1]);
		}
		for (int i = j + 1; i <= n; i++)
		{
			mx = max(mx, v[i]);
			mn = min(mn, v[i]);
		}
		res = min(res, mx - mn);
	}
	vector<int> d(n + 1);
	for (int j = 1; j <= n; j++) d[j] = v[n - j + 1];
	v = d;
	for (int j = 2; j <= n; j += 2)
	{
		int mx = -INF, mn = +INF;
		for (int i = 1; i <= j / 2; i++)
		{
			mx = max(mx, v[i] + v[j - i + 1]);
			mn = min(mn, v[i] + v[j - i + 1]);
		}
		for (int i = j + 1; i <= n; i++)
		{
			mx = max(mx, v[i]);
			mn = min(mn, v[i]);
		}
		res = min(res, mx - mn);
	}
	cout << res << '\n';
	return;
}

signed main()
{
	ios::sync_with_stdio(0);
	cin.tie(NULL); cout.tie(NULL);
	int t;
	//cin >> t;
	t=1;
	while (t--)
	{
		solve();
	}
	return 0;
}

提出情報

提出日時
問題 D - 1 or 2
ユーザ mshiladityam
言語 C++ (GCC 9.2.1)
得点 700
コード長 1804 Byte
結果 AC
実行時間 40 ms
メモリ 3692 KiB

ジャッジ結果

セット名 Sample All
得点 / 配点 0 / 0 700 / 700
結果
AC × 3
AC × 55
セット名 テストケース
Sample sample_01.txt, sample_02.txt, sample_03.txt
All random_01.txt, random_02.txt, random_03.txt, random_04.txt, random_05.txt, random_06.txt, random_07.txt, random_08.txt, random_09.txt, random_10.txt, random_11.txt, random_12.txt, random_13.txt, random_14.txt, random_15.txt, random_16.txt, random_17.txt, random_18.txt, random_19.txt, random_20.txt, random_21.txt, random_22.txt, random_23.txt, random_24.txt, random_25.txt, random_26.txt, random_27.txt, random_28.txt, random_29.txt, random_30.txt, random_31.txt, random_32.txt, random_33.txt, random_34.txt, random_35.txt, random_36.txt, random_37.txt, random_38.txt, random_39.txt, random_40.txt, sample_01.txt, sample_02.txt, sample_03.txt, sign_01.txt, sign_02.txt, sign_03.txt, sign_04.txt, sign_05.txt, sign_06.txt, sign_07.txt, sign_08.txt, sign_09.txt, sign_10.txt, sign_11.txt, sign_12.txt
ケース名 結果 実行時間 メモリ
random_01.txt AC 38 ms 3604 KiB
random_02.txt AC 37 ms 3500 KiB
random_03.txt AC 34 ms 3632 KiB
random_04.txt AC 35 ms 3560 KiB
random_05.txt AC 33 ms 3560 KiB
random_06.txt AC 32 ms 3692 KiB
random_07.txt AC 34 ms 3604 KiB
random_08.txt AC 33 ms 3652 KiB
random_09.txt AC 35 ms 3652 KiB
random_10.txt AC 35 ms 3628 KiB
random_11.txt AC 22 ms 3568 KiB
random_12.txt AC 15 ms 3528 KiB
random_13.txt AC 11 ms 3644 KiB
random_14.txt AC 33 ms 3532 KiB
random_15.txt AC 19 ms 3584 KiB
random_16.txt AC 16 ms 3616 KiB
random_17.txt AC 31 ms 3676 KiB
random_18.txt AC 27 ms 3680 KiB
random_19.txt AC 21 ms 3540 KiB
random_20.txt AC 6 ms 3504 KiB
random_21.txt AC 22 ms 3508 KiB
random_22.txt AC 4 ms 3432 KiB
random_23.txt AC 15 ms 3592 KiB
random_24.txt AC 34 ms 3564 KiB
random_25.txt AC 21 ms 3544 KiB
random_26.txt AC 31 ms 3552 KiB
random_27.txt AC 24 ms 3656 KiB
random_28.txt AC 25 ms 3652 KiB
random_29.txt AC 3 ms 3600 KiB
random_30.txt AC 22 ms 3624 KiB
random_31.txt AC 3 ms 3552 KiB
random_32.txt AC 2 ms 3460 KiB
random_33.txt AC 2 ms 3532 KiB
random_34.txt AC 1 ms 3604 KiB
random_35.txt AC 2 ms 3532 KiB
random_36.txt AC 3 ms 3556 KiB
random_37.txt AC 4 ms 3528 KiB
random_38.txt AC 8 ms 3552 KiB
random_39.txt AC 3 ms 3488 KiB
random_40.txt AC 1 ms 3600 KiB
sample_01.txt AC 3 ms 3596 KiB
sample_02.txt AC 3 ms 3516 KiB
sample_03.txt AC 2 ms 3548 KiB
sign_01.txt AC 40 ms 3564 KiB
sign_02.txt AC 34 ms 3564 KiB
sign_03.txt AC 36 ms 3608 KiB
sign_04.txt AC 33 ms 3500 KiB
sign_05.txt AC 17 ms 3560 KiB
sign_06.txt AC 16 ms 3528 KiB
sign_07.txt AC 31 ms 3668 KiB
sign_08.txt AC 9 ms 3508 KiB
sign_09.txt AC 2 ms 3616 KiB
sign_10.txt AC 4 ms 3580 KiB
sign_11.txt AC 2 ms 3616 KiB
sign_12.txt AC 3 ms 3580 KiB