結果

問題 No.1374 Absolute Game
ユーザー vjudge1
提出日時 2024-10-19 01:40:33
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 25 ms / 2,000 ms
コード長 1,514 bytes
コンパイル時間 2,052 ms
コンパイル使用メモリ 173,636 KB
実行使用メモリ 6,820 KB
最終ジャッジ日時 2024-10-19 01:40:37
合計ジャッジ時間 3,568 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 26
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
using ll = long long;
//using LL = __int128
#define pi pair<ll, ll>
#define fi first
#define se second
#define N 111111
#define inf 0x3f3f3f3f3f3f3f3f
ll n, a[N], vis[11], res[5];
void upd(ll &x, int t, ll v){
	if(t)
		x = max(x, v);
	else
		x = min(x, v);
}
bool check(){
	for(int i = 2 ; i <= n ; i++)
		if(a[i] * a[i - 1] <= 0)
			return false;
	return true;
}
int main(){
	scanf("%lld", &n);
	for(int i = 1 ; i <= n ; i++)
		scanf("%lld", a + i);
	if(check()){
		for(int i = 1 ; i <= n ; i++)
			a[i] = abs(a[i]);
		sort(a + 1, a + 1 + n, greater<ll>());
		ll ans[2] = {0};
		for(int i = 1 ; i <= n ; i++)
			ans[i & 1] += a[i];
		printf("%lld", ans[1] - ans[0]);
		return 0;
	}
	sort(a + 1, a + 1 + n);
	ll ans[2] = {0};
	for(int i = 1 ; i <= n ; i++)
		ans[i & 1] += a[i];
	res[1] = abs(ans[1]) - abs(ans[0]);
	memset(ans, 0, sizeof ans);
	for(int i = n ; i >= 1 ; i--)
		ans[i & 1] += a[i];
	res[2] = abs(ans[n & 1]) - abs(ans[(n ^ 1) & 1]);
	memset(ans, 0, sizeof ans);
	for(int i = n / 2 ; i >= 1 ; i--)
		ans[1] += a[i];
	for(int i = n / 2 + 1 ; i <= n ; i++)
		ans[0] += a[i];
	res[3] = abs(ans[0]) - abs(ans[1]);
	memset(ans, 0, sizeof ans);
	for(int i = n - n / 2 ; i >= 1 ; i--)
		ans[0] += a[i];
	for(int i = n - n / 2 + 1 ; i <= n ; i++)
		ans[1] += a[i];
	res[4] = abs(ans[0]) - abs(ans[1]);
	cerr << res[1] << " " << res[2] << " " << res[3] << " " << res[4] << "\n";
	printf("%lld", max(min(res[2], res[3]), min(res[1], res[4])));
	return 0;
}
/*

*/
0