結果

問題 No.1251 絶対に間違ってはいけない最小化問題
ユーザー pengin_2000pengin_2000
提出日時 2020-10-09 21:45:24
言語 C
(gcc 12.3.0)
結果
WA  
実行時間 -
コード長 1,955 bytes
コンパイル時間 692 ms
コンパイル使用メモリ 29,972 KB
実行使用メモリ 9,744 KB
最終ジャッジ日時 2023-09-27 16:07:36
合計ジャッジ時間 13,964 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,384 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 WA -
testcase_42 WA -
testcase_43 AC 1 ms
4,380 KB
testcase_44 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.c:61:15: 警告: conflicting types for built-in function ‘abs’; expected ‘int(int)’ [-Wbuiltin-declaration-mismatch]
   61 | long long int abs(long long int n)
      |               ^~~
main.c:2:1: 備考: ‘abs’ is declared in header ‘<stdlib.h>’
    1 | #include<stdio.h>
  +++ |+#include <stdlib.h>
    2 | long long int a[200005], b[200005];

ソースコード

diff #

#include<stdio.h>
long long int a[200005], b[200005];
long long int h[200005], l;
int comp_h(long long int x, long long int y)
{
	if (a[h[x]] > a[h[y]])
		return 1;
	else
		return -1;
}
void swap_h(long long int x, long long int y)
{
	long long int f = h[x];
	h[x] = h[y];
	h[y] = f;
	return;
}
void push(long long int ne)
{
	h[l] = ne;
	long long int p = l;
	l++;
	for (; p > 0; p = (p - 1) / 2)
		if (comp_h((p - 1) / 2, p) > 0)
			swap_h((p - 1) / 2, p);
	return;
}
long long int pop()
{
	l--;
	swap_h(0, l);
	long long int p = 0;
	for (;;)
	{
		if (2 * p + 2 < l)
		{
			if (comp_h(2 * p + 1, 2 * p + 2) > 0)
			{
				if (comp_h(p, 2 * p + 2) > 0)
					swap_h(p, 2 * p + 2);
				p = 2 * p + 2;
			}
			else
			{
				if (comp_h(p, 2 * p + 1) > 0)
					swap_h(p, 2 * p + 1);
				p = 2 * p + 1;
			}
		}
		else if (2 * p + 1 < l)
		{
			if (comp_h(p, 2 * p + 1) > 0)
				swap_h(p, 2 * p + 1);
			p = 2 * p + 1;
		}
		else
			break;
	}
	return h[l];
}
long long int abs(long long int n)
{
	if (n < 0)
		n *= -1;
	return n;
}
int main()
{
	long long int n;
	scanf("%lld", &n);
	long long int i, j;
	for (i = 0; i < n; i++)
		scanf("%lld", &a[i]);
	for (i = 0; i < n; i++)
		scanf("%lld", &b[i]);
	if (n == 1)
	{
		printf("%lld 0\n", a[0]);
		return 0;
	}
	long long int c[200005];
	l = 0;
	for (i = 0; i < n; i++)
		push(i);
	for (i = 0; i < n; i++)
		c[i] = pop();
	for (i = 0; i < n; i++)
		h[i] = a[i];
	for (i = 0; i < n; i++)
		a[i] = h[c[i]];
	for (i = 0; i < n; i++)
		h[i] = b[i];
	for (i = 0; i < n; i++)
		b[i] = b[c[i]];
	long long int sum_b[200005];
	sum_b[0] = 0;
	for (i = 0; i < n; i++)
		sum_b[i + 1] = sum_b[i] + b[i];
	long long int v1, v2;
	v1 = v2 = 0;
	j = 0;
	while (sum_b[j] * 2 <= sum_b[n])
		j++;
	j--;
	for (i = 0; i < n; i++)
		v1 += b[i] * abs(a[j] - a[i]);
	j++;
	for (i = 0; i < n; i++)
		v2 += b[i] * abs(a[j] - a[i]);
	if (v1 < v2)
		printf("%lld %lld\n", a[j - 1], v1);
	else
		printf("%lld %lld\n", a[j], v2);
	return 0;
}
0