結果

問題 No.1715 Dinner 2
ユーザー 👑 ygussanyygussany
提出日時 2021-10-12 08:56:13
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 2,609 bytes
コンパイル時間 243 ms
コンパイル使用メモリ 33,496 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-24 11:33:05
合計ジャッジ時間 1,386 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 1 ms
4,348 KB
testcase_05 AC 1 ms
4,348 KB
testcase_06 AC 1 ms
4,348 KB
testcase_07 AC 1 ms
4,348 KB
testcase_08 AC 1 ms
4,348 KB
testcase_09 AC 1 ms
4,348 KB
testcase_10 AC 1 ms
4,348 KB
testcase_11 AC 1 ms
4,348 KB
testcase_12 AC 1 ms
4,348 KB
testcase_13 AC 1 ms
4,348 KB
testcase_14 AC 2 ms
4,348 KB
testcase_15 AC 1 ms
4,348 KB
testcase_16 AC 1 ms
4,348 KB
testcase_17 AC 2 ms
4,348 KB
testcase_18 AC 1 ms
4,348 KB
testcase_19 AC 1 ms
4,348 KB
testcase_20 AC 1 ms
4,348 KB
testcase_21 AC 1 ms
4,348 KB
testcase_22 AC 2 ms
4,348 KB
testcase_23 AC 1 ms
4,348 KB
testcase_24 AC 1 ms
4,348 KB
testcase_25 AC 1 ms
4,348 KB
testcase_26 AC 1 ms
4,348 KB
testcase_27 AC 2 ms
4,348 KB
testcase_28 AC 2 ms
4,348 KB
testcase_29 AC 2 ms
4,348 KB
testcase_30 AC 1 ms
4,348 KB
testcase_31 AC 1 ms
4,348 KB
testcase_32 AC 2 ms
4,348 KB
testcase_33 AC 2 ms
4,348 KB
testcase_34 AC 2 ms
4,348 KB
testcase_35 AC 1 ms
4,348 KB
testcase_36 AC 1 ms
4,348 KB
testcase_37 AC 1 ms
4,348 KB
testcase_38 AC 1 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// O(N * log(N) + D * log(N) * log(|max_ans|)) time solution

#include <stdio.h>

#define N_MAX 1000
#define D_MAX 1000
#define P_MAX 100000

typedef struct {
	int key, id;
} data;

void merge_sort(int n, data x[])
{
	static data y[N_MAX + 1];
	if (n <= 1) return;
	merge_sort(n / 2, &(x[0]));
	merge_sort((n + 1) / 2, &(x[n/2]));
	
	int i, p, q;
	for (i = 0, p = 0, q = n / 2; i < n; i++) {
		if (p >= n / 2) y[i] = x[q++];
		else if (q >= n) y[i] = x[p++];
		else y[i] = (x[p].key < x[q].key)? x[p++]: x[q++];
	}
	for (i = 0; i < n; i++) x[i] = y[i];
}

void chmax(long long* a, long long b)
{
	if (*a < b) *a = b;
}

int BS_floor(int N, int P[], int x)
{
	int l = 0, r = N, m;
	while (l < r) {
		m = (l + r + 1) / 2;
		if (P[m] <= x) l = m;
		else r = m - 1;
	}
	return l;
}

int main()
{
	int i, N, D, P[N_MAX + 1], Q[N_MAX + 1];
	data d[N_MAX + 1];
	scanf("%d %d", &N, &D);
	for (i = 0; i < N; i++) scanf("%d %d", &(d[i].key), &(d[i].id));
	merge_sort(N, d);
	for (i = 0; i < N; i++) {
		P[i+1] = d[i].key;
		Q[i+1] = d[i].id;
	}
	P[0] = 0;
	Q[0] = -P_MAX;
	
	int j, argmax[N_MAX + 1][3] = {};
	for (i = 1; i <= N; i++) {
		for (j = 0; j < 3; j++) argmax[i][j] = argmax[i-1][j];
		j = argmax[i-1][0];
		if (Q[i] - P[i] > Q[j] - P[j]) {
			argmax[i][2] = argmax[i][1];
			argmax[i][1] = argmax[i][0];
			argmax[i][0] = i;
			continue;
		}
		j = argmax[i-1][1];
		if (Q[i] - P[i] > Q[j] - P[j]) {
			argmax[i][2] = argmax[i][1];
			argmax[i][1] = i;
			continue;
		}
		j = argmax[i-1][2];
		if (Q[i] - P[i] > Q[j] - P[j]) argmax[i][2] = i;
	}

	long long l = 1, r = (long long)D * P[2], m, dp[D_MAX + 1][4], tmp;
	int h, k, kk;
	while (l < r) {
		m = (l + r) / 2;
		
		for (i = 0; i <= D; i++) for (j = 0; j < 4; j++) dp[i][j] = -1;
		for (i = 1, dp[0][3] = m; i <= D; i++) {
			for (j = 0; j < 4; j++) {
				if (dp[i-1][j] < 0) continue;
				else if (dp[i-1][j] > P[N]) k = N;
				else k = BS_floor(N, P, (int)dp[i-1][j]);
				
				for (h = 0; h < 3; h++) {
					if (h == j || P[argmax[k][h]] == 0) continue;
					tmp = dp[i-1][j] - P[argmax[k][h]]; 
					if (tmp < 0) continue;
					else tmp += Q[argmax[k][h]];
					
					if (tmp > P[N]) kk = N;
					else kk = BS_floor(N, P, (int)tmp);
					if (argmax[k][h] == argmax[kk][0]) chmax(&(dp[i][0]), tmp);
					else if (argmax[k][h] == argmax[kk][1]) chmax(&(dp[i][1]), tmp);
					else if (argmax[k][h] == argmax[kk][2]) chmax(&(dp[i][2]), tmp);
					else chmax(&(dp[i][3]), tmp);
				}
			}
			
			if (dp[i][0] + dp[i][1] + dp[i][2] + dp[i][3] == -4) break;
		}
		
		if (i > D) r = m;
		else l = m + 1;
	}
	printf("%d\n", -l);
	fflush(stdout);
	return 0;
}
0