結果

問題 No.545 ママの大事な二人の子供
ユーザー bal4ubal4u
提出日時 2019-04-25 09:42:14
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 24 ms / 2,000 ms
コード長 1,977 bytes
コンパイル時間 194 ms
コンパイル使用メモリ 31,232 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-28 22:08:38
合計ジャッジ時間 1,370 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 1 ms
5,376 KB
testcase_07 AC 1 ms
5,376 KB
testcase_08 AC 1 ms
5,376 KB
testcase_09 AC 1 ms
5,376 KB
testcase_10 AC 1 ms
5,376 KB
testcase_11 AC 1 ms
5,376 KB
testcase_12 AC 1 ms
5,376 KB
testcase_13 AC 1 ms
5,376 KB
testcase_14 AC 1 ms
5,376 KB
testcase_15 AC 1 ms
5,376 KB
testcase_16 AC 1 ms
5,376 KB
testcase_17 AC 1 ms
5,376 KB
testcase_18 AC 1 ms
5,376 KB
testcase_19 AC 2 ms
5,376 KB
testcase_20 AC 3 ms
5,376 KB
testcase_21 AC 1 ms
5,376 KB
testcase_22 AC 12 ms
5,376 KB
testcase_23 AC 24 ms
5,376 KB
testcase_24 AC 5 ms
5,376 KB
testcase_25 AC 21 ms
5,376 KB
testcase_26 AC 14 ms
5,376 KB
testcase_27 AC 19 ms
5,376 KB
testcase_28 AC 24 ms
5,376 KB
testcase_29 AC 20 ms
5,376 KB
testcase_30 AC 22 ms
5,376 KB
testcase_31 AC 20 ms
5,376 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.c: In function 'in':
main.c:8:14: warning: implicit declaration of function 'getchar_unlocked' [-Wimplicit-function-declaration]
    8 | #define gc() getchar_unlocked()
      |              ^~~~~~~~~~~~~~~~
main.c:16:34: note: in expansion of macro 'gc'
   16 |         long long n = 0; int c = gc();
      |                                  ^~

ソースコード

diff #

// yukicoder: No.545 ママの大事な二人の子供
// 2019.4.25 bal4u

#include <stdio.h>
#include <stdlib.h>

#if 1
#define gc() getchar_unlocked()
#define pc(c) putchar_unlocked(c)
#else
#define gc() getchar()
#define pc(c) putchar(c)
#endif
long long in()    // 非負整数の入力
{
	long long n = 0; int c = gc();
	do n = 10 * n + (c & 0xf), c = gc(); while (c >= '0');
	return n;
}

long long a[35], b[35]; int N;
long long s[65600]; int sz;

void precalc(long long *a, long long *b, long long *s, int n)
{
	int i, j;
	long long _s = 0;
	sz = 1 << n;
	for (i = 0; i < sz; i++) {
		_s = 0;
		for (j = 0; j < n; j++) {
			if ((i >> j) & 1) _s += a[j];
			else _s -= b[j];
		}
		s[i] = _s;
	}
}

int cmp(const void *a, const void *b)
{
	if (*(long long *)a < *(long long *)b) return -1;
	return *(long long *)a >= *(long long *)b;
}

int bsch(long long x)
{
	int m, l = 0, r = sz;

    while (l+1 < r) {
        m = (l+r) >> 1;
        if (s[m] < x) l = m; else r = m;
    }
	return r;
}

#define ABS(x) ((x)>=0?(x):-(x))

int main()
{
	int i, k, N, n, lim;
	long long ans;

	N = (int)in(), n = N/2;
	for (i = 0; i < N; i++) a[i] = in(), b[i] = in();
	if (n > 0) {
		precalc(a, b, s, n);
		qsort(s, sz, sizeof(long long), cmp);
		k = N - n, lim = 1 << k, ans = 0x7ffffffffffffffLL;
		for (i = 0; i < lim; i++) {
			long long t, d = 0;
			int m, j;
			for (j = 0; j < k; j++) {
				if ((i >> j) & 1) d += a[j+n];
				else d -= b[j+n];
			}
			m = bsch(-d);
			if (m >= 0 && m < sz) {
				t = ABS(s[m]+d);
				if (t == 0) { ans = 0; break; }
				if (t < ans) ans = t;
			}
			if (m > 0 && (t = ABS(s[m-1]+d)) < ans) ans = t;
			if (m+1 < sz && (t = ABS(s[m+1]+d)) < ans) ans = t;
		}
	} else {
		lim = 1 << N, ans = 0x7ffffffffffffffLL;
		for (i = 0; i < lim; i++) {
			long long t, d = 0;
			int j;
			for (j = 0; j < N; j++) {
				if ((i >> j) & 1) d += a[j+n];
				else d -= b[j+n];
			}
			if ((t = ABS(d)) < ans) ans = t;
		}
	}
	printf("%lld\n", ans);
	return 0;
}
0