結果

問題 No.1391 ±1 Abs Sum
ユーザー publfl
提出日時 2021-02-12 22:28:14
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 1,201 bytes
コンパイル時間 192 ms
コンパイル使用メモリ 32,896 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-07-19 23:02:25
合計ジャッジ時間 2,307 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 20 WA * 14
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>

int x[200010];
long long int sum[200010];
int a;
int L,R;
long long int func(int p, int s, int t)
{
	long long int ans = 0;
	if(p<s)
	{
		ans += sum[p];
		ans -= (sum[s-1]-sum[p]);
		ans += (sum[t]-sum[s-1]);
		ans -= (sum[a]-sum[t]);
		ans += (long long int)(-(p)+(s-1-p)-(t-s+1)+(a-t))*x[p];
	}
	else if(s<=p&&p<=t)
	{
		ans += sum[s-1];
		ans -= (sum[p]-sum[s-1]);
		ans += (sum[t]-sum[p]);
		ans -= (sum[a]-sum[t]);
		ans += (long long int)(-(s-1)+(p-s+1)-(t-p)+(a-t))*x[p];
	}
	else
	{
		ans += sum[s-1];
		ans -= (sum[t]-sum[s-1]);
		ans += (sum[p]-sum[t]);
		ans -= (sum[a]-sum[p]);
		ans += (long long int)(-(s-1)+(t-s+1)-(p-t)+(a-p))*x[p];
	}
	return ans;
}
int main()
{
	int b;
	scanf("%d%d",&a,&b);
	for(int i=1;i<=a;i++) scanf("%d",&x[i]);
	for(int i=1;i<=a;i++) sum[i] += (sum[i-1] + x[i]);
	long long int ans = 1;
	for(int i=1;i<=16;i++) ans*=10;
	int p = 1;
	b = a-b;
	for(int i=0;i<=b;i++)
	{
		L = i+1;
		R = a-(b-i);
		while(p+1<=a&&func(p+1,L,R)<func(p,L,R)) p++;
		while(p-1>=1&&func(p-1,L,R)<func(p,L,R)) p--;
		ans = ans<func(p,L,R)?ans:func(p,L,R);
		ans = ans<func(1,L,R)?ans:func(1,L,R);
		ans = ans<func(a,L,R)?ans:func(a,L,R);
	}
	printf("%lld\n",ans);
}
0