結果

問題 No.59 鉄道の旅
ユーザー bal4ubal4u
提出日時 2019-06-01 22:33:13
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 12 ms / 5,000 ms
コード長 1,128 bytes
コンパイル時間 186 ms
コンパイル使用メモリ 30,876 KB
実行使用メモリ 9,588 KB
最終ジャッジ日時 2023-10-17 22:51:36
合計ジャッジ時間 1,099 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 1 ms
4,348 KB
testcase_03 AC 1 ms
4,348 KB
testcase_04 AC 12 ms
9,588 KB
testcase_05 AC 2 ms
5,936 KB
testcase_06 AC 2 ms
5,916 KB
testcase_07 AC 2 ms
5,920 KB
testcase_08 AC 4 ms
9,580 KB
testcase_09 AC 5 ms
9,456 KB
testcase_10 AC 7 ms
9,548 KB
testcase_11 AC 2 ms
4,348 KB
testcase_12 AC 4 ms
4,348 KB
testcase_13 AC 6 ms
5,872 KB
testcase_14 AC 7 ms
5,872 KB
testcase_15 AC 1 ms
4,348 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.c: In function 'in':
main.c:7:14: warning: implicit declaration of function 'getchar_unlocked' [-Wimplicit-function-declaration]
    7 | #define gc() getchar_unlocked()
      |              ^~~~~~~~~~~~~~~~
main.c:14:24: note: in expansion of macro 'gc'
   14 |         int n = 0, c = gc();
      |                        ^~

ソースコード

diff #

// yukicoder: 59 鉄道の旅
// 2019.6.1 bal4u

#include <stdio.h>

#if 1
#define gc() getchar_unlocked()
#else
#define gc() getchar()
#endif

int in()   // 整数の入力(負数対応)
{
	int n = 0, c = gc();
	if (c == '-') {	c = gc();
		do n = 10*n + (c & 0xf), c = gc(); while (c >= '0');
		return -n;
	}
	do n = 10*n + (c & 0xf), c = gc(); while (c >= '0');
	return n;
}

#define MAXVAL 1000005
int bit[MAXVAL+5], sz, p;
void init(int maxVal) {
	sz = maxVal;
	p = 1; while (p < sz) p <<= 1;
}

void add(int i, int x) {
	i++; while (i <= sz)
		bit[i] += x, i += i & -i;
}

void insert(int val) { add(val, 1); }
void erase(int val) { add(val, -1); }

int nth_element(int n) {
	int a = 0, q = p; n--;
	while (q >>= 1)
		if (a+q <= sz && bit[a+q] <= n)
			a += q, n -= bit[a];
	return a;
}

int f[MAXVAL];

int main()
{
	int N, K, w, ans;
	
	N = in(), K = in(), ans = 0;
	init(MAXVAL);
	while (N--) {
		w = in();
		if (w > 0) {
			if (ans < K || nth_element(K) > MAXVAL-w)
				ans++, f[w]++, insert(MAXVAL-w);
		} else {
			w = -w;
			if (f[w] > 0) f[w]--, ans--, erase(MAXVAL-w);
		}
	}
	printf("%d\n", ans);
	return 0;
}
0