結果

問題 No.2100 [Cherry Alpha C] Two-way Steps
ユーザー pengin_2000pengin_2000
提出日時 2022-10-14 23:26:31
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 174 ms / 2,000 ms
コード長 3,069 bytes
コンパイル時間 1,220 ms
コンパイル使用メモリ 30,192 KB
実行使用メモリ 13,596 KB
最終ジャッジ日時 2023-09-08 23:51:07
合計ジャッジ時間 7,838 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
9,992 KB
testcase_01 AC 2 ms
9,984 KB
testcase_02 AC 2 ms
10,104 KB
testcase_03 AC 2 ms
10,004 KB
testcase_04 AC 132 ms
12,716 KB
testcase_05 AC 107 ms
12,188 KB
testcase_06 AC 16 ms
10,484 KB
testcase_07 AC 21 ms
10,520 KB
testcase_08 AC 124 ms
12,688 KB
testcase_09 AC 108 ms
12,088 KB
testcase_10 AC 70 ms
11,308 KB
testcase_11 AC 92 ms
11,896 KB
testcase_12 AC 72 ms
11,352 KB
testcase_13 AC 66 ms
11,728 KB
testcase_14 AC 71 ms
11,336 KB
testcase_15 AC 74 ms
11,624 KB
testcase_16 AC 79 ms
11,392 KB
testcase_17 AC 39 ms
11,568 KB
testcase_18 AC 69 ms
11,732 KB
testcase_19 AC 152 ms
13,144 KB
testcase_20 AC 30 ms
10,908 KB
testcase_21 AC 70 ms
11,268 KB
testcase_22 AC 76 ms
11,352 KB
testcase_23 AC 115 ms
12,328 KB
testcase_24 AC 171 ms
13,468 KB
testcase_25 AC 172 ms
13,496 KB
testcase_26 AC 170 ms
13,492 KB
testcase_27 AC 169 ms
13,576 KB
testcase_28 AC 171 ms
13,524 KB
testcase_29 AC 170 ms
13,532 KB
testcase_30 AC 168 ms
13,508 KB
testcase_31 AC 171 ms
13,596 KB
testcase_32 AC 169 ms
13,580 KB
testcase_33 AC 174 ms
13,516 KB
testcase_34 AC 116 ms
12,204 KB
testcase_35 AC 111 ms
12,036 KB
testcase_36 AC 111 ms
11,940 KB
testcase_37 AC 106 ms
12,088 KB
testcase_38 AC 114 ms
12,176 KB
testcase_39 AC 99 ms
12,028 KB
testcase_40 AC 100 ms
11,992 KB
testcase_41 AC 99 ms
11,976 KB
testcase_42 AC 98 ms
11,980 KB
testcase_43 AC 96 ms
11,904 KB
testcase_44 AC 76 ms
12,692 KB
testcase_45 AC 77 ms
12,740 KB
testcase_46 AC 77 ms
12,792 KB
testcase_47 AC 77 ms
12,732 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<stdio.h>
long long int Max(long long int a, long long int b)
{
	if (a > b)
		return a;
	else
		return b;
}
long long int x[300005], y[300005];
long long int heap[300005], l;
int comp_h(long long int a, long long int b)
{
	if (x[heap[a]] > x[heap[b]])
		return 1;
	else
		return -1;
}
void swap_h(long long int a, long long int b)
{
	long long int f = heap[a];
	heap[a] = heap[b];
	heap[b] = f;
	return;
}
void push(long long int ne)
{
	heap[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 heap[l];
}
long long int n, m;
long long int h[100005];
long long int c[300005];
long long int dp[100005][2];
long long int f1()
{
	long long int i;
	for (i = 0; i < n; i++)
		dp[i][0] = dp[i][1] = -1;
	dp[0][0] = 0;
	long long int min, mid, max;
	for (i = 0; i < n; i++)
	{
		min = -1;
		max = m;
		while (max - min > 1)
		{
			mid = (max + min) / 2;
			if (x[c[mid]] < i)
				min = mid;
			else
				max = mid;
		}
		for (; x[c[max]] == i; max++)
		{
			if (y[c[max]] < i)
				continue;
			if (h[y[c[max]]] < h[i])
			{
				if (dp[i][0] >= 0)
					dp[y[c[max]]][0] = Max(dp[y[c[max]]][0], dp[i][0]);
				if (dp[i][1] >= 0)
					dp[y[c[max]]][0] = Max(dp[y[c[max]]][0], dp[i][1]);
			}
			else
			{
				if (dp[i][0] >= 0)
					dp[y[c[max]]][1] = Max(dp[y[c[max]]][1], dp[i][0] + h[y[c[max]]] - h[i]);
			}
		}
	}
	return Max(dp[n - 1][0], dp[n - 1][1]);
}
long long int f2()
{
	long long int i;
	for (i = 0; i < n; i++)
		dp[i][0] = dp[i][1] = -1;
	dp[n - 1][0] = 0;
	long long int min, mid, max;
	for (i = n - 1; i >= 0; i--)
	{
		min = -1;
		max = m;
		while (max - min > 1)
		{
			mid = (max + min) / 2;
			if (x[c[mid]] < i)
				min = mid;
			else
				max = mid;
		}
		for (; x[c[max]] == i; max++)
		{
			if (y[c[max]] > i)
				continue;
			if (h[y[c[max]]] < h[i])
			{
				if (dp[i][0] >= 0)
					dp[y[c[max]]][0] = Max(dp[y[c[max]]][0], dp[i][0]);
				if (dp[i][1] >= 0)
					dp[y[c[max]]][0] = Max(dp[y[c[max]]][0], dp[i][1]);
			}
			else
			{
				if (dp[i][0] >= 0)
					dp[y[c[max]]][1] = Max(dp[y[c[max]]][1], dp[i][0] + h[y[c[max]]] - h[i]);
			}
		}
	}
	return Max(dp[0][0], dp[0][1]);
}
int main()
{
	scanf("%lld %lld", &n, &m);
	long long int i;
	for (i = 0; i < n; i++)
		scanf("%lld", &h[i]);
	for (i = 0; i < m; i++)
	{
		scanf("%lld %lld", &x[i], &y[i]);
		x[i]--;
		y[i]--;
		x[i + m] = y[i];
		y[i + m] = x[i];
	}
	l = 0;
	m *= 2;
	for (i = 0; i < m; i++)
		push(i);
	for (i = 0; i < m; i++)
		c[i] = pop();
	c[m] = m;
	x[m] = -1;
	printf("%lld\n%lld\n", f1(), f2());
	return 0;
}
0