結果

問題 No.704 ゴミ拾い Medium
ユーザー 草苺奶昔草苺奶昔
提出日時 2023-02-05 22:29:46
言語 Go
(1.22.1)
結果
AC  
実行時間 603 ms / 1,500 ms
コード長 2,510 bytes
コンパイル時間 12,204 ms
コンパイル使用メモリ 207,324 KB
実行使用メモリ 18,612 KB
最終ジャッジ日時 2023-09-17 12:17:57
合計ジャッジ時間 26,795 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,364 KB
testcase_02 AC 1 ms
4,360 KB
testcase_03 AC 1 ms
4,384 KB
testcase_04 AC 1 ms
4,360 KB
testcase_05 AC 2 ms
4,364 KB
testcase_06 AC 1 ms
4,360 KB
testcase_07 AC 1 ms
4,360 KB
testcase_08 AC 1 ms
4,368 KB
testcase_09 AC 2 ms
4,360 KB
testcase_10 AC 1 ms
4,364 KB
testcase_11 AC 1 ms
4,364 KB
testcase_12 AC 1 ms
4,364 KB
testcase_13 AC 2 ms
4,360 KB
testcase_14 AC 3 ms
4,364 KB
testcase_15 AC 4 ms
4,360 KB
testcase_16 AC 3 ms
4,364 KB
testcase_17 AC 3 ms
4,360 KB
testcase_18 AC 3 ms
4,364 KB
testcase_19 AC 4 ms
4,364 KB
testcase_20 AC 3 ms
4,368 KB
testcase_21 AC 3 ms
4,364 KB
testcase_22 AC 3 ms
4,364 KB
testcase_23 AC 3 ms
4,364 KB
testcase_24 AC 600 ms
16,408 KB
testcase_25 AC 598 ms
16,404 KB
testcase_26 AC 595 ms
16,404 KB
testcase_27 AC 594 ms
16,404 KB
testcase_28 AC 594 ms
16,404 KB
testcase_29 AC 600 ms
16,408 KB
testcase_30 AC 594 ms
16,408 KB
testcase_31 AC 595 ms
16,408 KB
testcase_32 AC 602 ms
16,408 KB
testcase_33 AC 596 ms
16,404 KB
testcase_34 AC 538 ms
14,648 KB
testcase_35 AC 539 ms
14,644 KB
testcase_36 AC 540 ms
14,648 KB
testcase_37 AC 538 ms
14,648 KB
testcase_38 AC 538 ms
14,648 KB
testcase_39 AC 539 ms
14,644 KB
testcase_40 AC 539 ms
14,648 KB
testcase_41 AC 538 ms
14,648 KB
testcase_42 AC 536 ms
14,644 KB
testcase_43 AC 543 ms
14,644 KB
testcase_44 AC 1 ms
4,368 KB
testcase_45 AC 1 ms
4,364 KB
testcase_46 AC 584 ms
18,612 KB
testcase_47 AC 603 ms
16,452 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package main

import (
	"bufio"
	"fmt"
	"os"
)

// オフライン・オンライン変換:
// !如果offline问题存在复杂度O(M(n))的解,那么online问题存在复杂度O(M(n)logn)的解
// dp[j]=min(dp[i]+f(i,j)) (i<j)
// O(n^2)优化到O(nlogn^2)
// 例子:
// !dp[j]=min(dp[i]+(x[j]-x[i]-a)^2)
func offlineOnlineDp(n int, dist func(i, j int) int) int {
	dp := make([]int, n+1)
	used := make([]bool, n+1)
	used[n] = true

	update := func(k, val int) {
		if !used[k] {
			dp[k] = val
		}
		dp[k] = min(dp[k], val)
		used[k] = true
	}

	var dfs func(top, bottom, left, right int) // induce
	dfs = func(top, bottom, left, right int) {
		if top == bottom {
			return
		}
		mid := (top + bottom) / 2
		index := left
		res := dist(mid, index) + dp[index]
		for i := left; i <= right; i++ {
			tmp := dist(mid, i) + dp[i]
			if tmp < res {
				res = tmp
				index = i
			}
		}

		update(mid, res)
		dfs(top, mid, left, index)
		dfs(mid+1, bottom, index, right)
	}

	var solve func(left, right int)
	solve = func(left, right int) {
		if left+1 == right {
			update(left, dist(left, right)+dp[right])
			return
		}
		mid := (left + right) / 2
		solve(mid, right)
		dfs(left, mid, mid, right)
		solve(left, mid)
	}

	solve(0, n)
	return dp[0]
}

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

func main() {
	// 垃圾回收
	// https://yukicoder.me/problems/no/705
	// 公园里有n个垃圾
	// n个人准备去回收垃圾,
	// 每个人的起点在(startsi,0),沿x轴顺序递增排列
	// 每个垃圾的位置在(xi,yi),沿x轴顺序递增排列
	// !每个人只能回收他左边的垃圾
	// 每个人i回收垃圾j,j+1,...,i(0<=j<=i)的时间花费为
	// 1.欧几里得距离的平方2.曼哈顿距离3.欧几里得距离的三次方
	// !求回收所有垃圾的最短时间之和
	// dp[i]表示前i个人回收完前i个垃圾时的最短花费
	// dp[i]=min(dp[j]+abs((xi-xj))^2+yj^2) (0<=j<=i)
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()

	var n int
	fmt.Fscan(in, &n)
	starts, xs, ys := make([]int, n), make([]int, n), make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscan(in, &starts[i])
	}
	for i := 0; i < n; i++ {
		fmt.Fscan(in, &xs[i])
	}
	for i := 0; i < n; i++ {
		fmt.Fscan(in, &ys[i])
	}

	dist := func(i, j int) int {
		// 0<=i<j<=n
		a := abs(starts[j-1] - xs[i])
		b := abs(ys[i])
		return a + b
	}
	res := offlineOnlineDp(n, dist)
	fmt.Fprintln(out, res)
}

func abs(x int) int {
	if x < 0 {
		return -x
	}
	return x
}
0