結果

問題 No.1251 絶対に間違ってはいけない最小化問題
ユーザー pengin_2000pengin_2000
提出日時 2020-10-09 21:59:23
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 100 ms / 2,000 ms
コード長 1,958 bytes
コンパイル時間 1,003 ms
コンパイル使用メモリ 30,276 KB
実行使用メモリ 9,720 KB
最終ジャッジ日時 2023-09-27 16:55:57
合計ジャッジ時間 15,336 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
5,900 KB
testcase_03 AC 1 ms
4,384 KB
testcase_04 AC 2 ms
5,932 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 2 ms
5,972 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 1 ms
5,952 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 1 ms
5,896 KB
testcase_15 AC 2 ms
5,968 KB
testcase_16 AC 2 ms
5,892 KB
testcase_17 AC 1 ms
4,504 KB
testcase_18 AC 94 ms
9,412 KB
testcase_19 AC 91 ms
9,140 KB
testcase_20 AC 49 ms
6,880 KB
testcase_21 AC 7 ms
4,484 KB
testcase_22 AC 53 ms
7,152 KB
testcase_23 AC 6 ms
6,200 KB
testcase_24 AC 72 ms
8,160 KB
testcase_25 AC 51 ms
6,912 KB
testcase_26 AC 18 ms
5,144 KB
testcase_27 AC 6 ms
4,380 KB
testcase_28 AC 99 ms
9,624 KB
testcase_29 AC 98 ms
9,644 KB
testcase_30 AC 99 ms
9,536 KB
testcase_31 AC 98 ms
9,648 KB
testcase_32 AC 99 ms
9,632 KB
testcase_33 AC 99 ms
9,632 KB
testcase_34 AC 98 ms
9,620 KB
testcase_35 AC 98 ms
9,720 KB
testcase_36 AC 100 ms
9,720 KB
testcase_37 AC 98 ms
9,600 KB
testcase_38 AC 99 ms
9,604 KB
testcase_39 AC 100 ms
9,540 KB
testcase_40 AC 98 ms
9,660 KB
testcase_41 AC 98 ms
9,680 KB
testcase_42 AC 99 ms
9,632 KB
testcase_43 AC 1 ms
4,376 KB
testcase_44 AC 1 ms
6,016 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] = h[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 -= 2;
	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