結果

問題 No.165 四角で囲え!
ユーザー bal4ubal4u
提出日時 2019-06-21 17:27:41
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 185 ms / 5,000 ms
コード長 2,035 bytes
コンパイル時間 1,444 ms
コンパイル使用メモリ 30,824 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-26 18:55:37
合計ジャッジ時間 2,920 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 65 ms
4,376 KB
testcase_01 AC 0 ms
4,380 KB
testcase_02 AC 0 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 0 ms
4,380 KB
testcase_05 AC 93 ms
4,376 KB
testcase_06 AC 22 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 0 ms
4,380 KB
testcase_09 AC 1 ms
4,384 KB
testcase_10 AC 0 ms
4,376 KB
testcase_11 AC 40 ms
4,380 KB
testcase_12 AC 16 ms
4,380 KB
testcase_13 AC 19 ms
4,380 KB
testcase_14 AC 13 ms
4,380 KB
testcase_15 AC 11 ms
4,380 KB
testcase_16 AC 125 ms
4,380 KB
testcase_17 AC 92 ms
4,376 KB
testcase_18 AC 185 ms
4,380 KB
testcase_19 AC 107 ms
4,380 KB
testcase_20 AC 90 ms
4,376 KB
testcase_21 AC 3 ms
4,376 KB
testcase_22 AC 3 ms
4,380 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.c: 関数 ‘in’ 内:
main.c:8:14: 警告: 関数 ‘getchar_unlocked’ の暗黙的な宣言です [-Wimplicit-function-declaration]
    8 | #define gc() getchar_unlocked()
      |              ^~~~~~~~~~~~~~~~
main.c:15:24: 備考: in expansion of macro ‘gc’
   15 |         int n = 0, c = gc();
      |                        ^~

ソースコード

diff #

// yukicoder: No.165 四角で囲え!
// 2019.6.21 bal4u

#include <stdio.h>
#include <stdlib.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;
}

typedef struct { int x, y, p; } PP;
PP t[405];
int x[405], xs;
int y[405], ys;
int map[405][405], no[405][405];
int sum[405][405], cnt[405][405];
int N, B;
int cmp(const void *a, const void *b) { return *(int *)a - *(int *)b; }

int uniq(int *a, int n) {
	int i, j;
	
	for (i = 0, j = 1; j < n; j++) {
		while (j < n && a[j] == a[i]) j++;
		if (j < n) if (++i != j) a[i] = a[j];
	}
	return i+1;
}

int bsch(int *a, int x, int r) {
	int m, l = 0;

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

int main()
{
	int i, a, r, c, rr, cc, ans;

	N = in(), B = in();
	for (i = 0; i < N; i++) {
		t[i].x = x[i] = in(), t[i].y = y[i] = in(), t[i].p = in();
	}
	qsort(x, N, sizeof(int), cmp); xs = uniq(x, N);
	qsort(y, N, sizeof(int), cmp); ys = uniq(y, N);

	for (i = 0; i < N; i++) {
		c = bsch(x, t[i].x, xs);
		r = bsch(y, t[i].y, ys);
		map[r][c] = t[i].p, no[r][c] = 1;
	}

	for (r = 0; r < ys; r++) for (c = 0; c < xs; c++) {
        sum[r+1][c+1] = sum[r][c+1] + sum[r+1][c] - sum[r][c] + map[r][c];
		cnt[r+1][c+1] = cnt[r][c+1] + cnt[r+1][c] - cnt[r][c] + no[r][c];
	}

	ans = 0;
    for (r = 0; r < ys; r++) for (rr = 1; rr <= ys; rr++) {
		c = 0, cc = 1; while (cc <= xs) {
			while (cc <= xs && sum[rr][cc] - sum[r][cc] - sum[rr][c] + sum[r][c] <= B) {
				a = cnt[rr][cc] - cnt[r][cc] - cnt[rr][c] + cnt[r][c];
				if (a > ans) {
					ans = a;
					if (ans == N) goto done;
				}
				cc++;
			}
			while (c < cc && sum[rr][cc] - sum[r][cc] - sum[rr][c] + sum[r][c] > B) c++;
		}
    }
done:
	printf("%d\n", ans);
	return 0;
}
0