結果

問題 No.1767 BLUE to RED
ユーザー HimaHima
提出日時 2022-06-18 12:45:03
言語 Go
(1.22.1)
結果
AC  
実行時間 156 ms / 2,000 ms
コード長 2,291 bytes
コンパイル時間 16,547 ms
コンパイル使用メモリ 233,532 KB
実行使用メモリ 21,712 KB
最終ジャッジ日時 2024-04-18 07:56:36
合計ジャッジ時間 18,737 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,812 KB
testcase_02 AC 3 ms
6,816 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 2 ms
6,940 KB
testcase_09 AC 99 ms
18,704 KB
testcase_10 AC 105 ms
19,400 KB
testcase_11 AC 94 ms
18,128 KB
testcase_12 AC 86 ms
18,700 KB
testcase_13 AC 108 ms
19,404 KB
testcase_14 AC 144 ms
20,300 KB
testcase_15 AC 142 ms
20,304 KB
testcase_16 AC 145 ms
20,436 KB
testcase_17 AC 156 ms
20,300 KB
testcase_18 AC 152 ms
20,432 KB
testcase_19 AC 142 ms
20,304 KB
testcase_20 AC 143 ms
21,712 KB
testcase_21 AC 141 ms
20,300 KB
testcase_22 AC 138 ms
20,300 KB
testcase_23 AC 136 ms
20,300 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
	"strconv"
)

var sc = bufio.NewScanner(os.Stdin)
var out = bufio.NewWriter(os.Stdout)

func solve(n, m int, a, b []int) int {
	ca := make([]int, n)
	copy(ca, a)
	mn, mx := 0, int(1e9)+1
	ca = append([]int{mn}, ca...)
	ca = append(ca, mx)

	type section struct {
		l, r int
	}
	c := make(map[section][]int)

	for j := 0; j < m; j++ {
		idx := sort.Search(len(ca), func(i int) bool {
			return b[j] <= ca[i]
		})
		l, r := ca[idx-1], ca[idx]
		c[section{l, r}] = append(c[section{l, r}], b[j])
	}

	var ans int
	for k, v := range c {
		for len(v) > 0 {
			if k.l == mn {
				ans += k.r - v[len(v)-1]
				k.r = v[len(v)-1]
				v = v[:len(v)-1]
			} else if k.r == mx {
				ans += v[0] - k.l
				k.l = v[0]
				v = v[1:]
			} else {
				dl, dr := v[0]-k.l, k.r-v[len(v)-1]
				if dl <= dr {
					ans += v[0] - k.l
					k.l = v[0]
					v = v[1:]
				} else {
					ans += k.r - v[len(v)-1]
					k.r = v[len(v)-1]
					v = v[:len(v)-1]
				}
				//38 55 64 68 70 92 97
				// 38:1, 97:2, 92:5, 55:17, 64:9, 68:4, 70:2
				//
			}
		}
	}
	return ans
}

func main() {
	buf := make([]byte, 1024*1024)
	sc.Buffer(buf, bufio.MaxScanTokenSize)
	sc.Split(bufio.ScanWords)

	n, m := nextInt(), nextInt()
	a := nextIntSlice(n)
	b := nextIntSlice(m)
	ans := solve(n, m, a, b)
	PrintInt(ans)
}

func nextInt() int {
	sc.Scan()
	i, _ := strconv.Atoi(sc.Text())
	return i
}

func nextIntSlice(n int) []int {
	s := make([]int, n)
	for i := range s {
		s[i] = nextInt()
	}
	return s
}

func PrintInt(x int) {
	defer out.Flush()
	fmt.Fprintln(out, x)
}

func PrintFloat64(x float64) {
	defer out.Flush()
	fmt.Fprintln(out, x)
}

func PrintString(x string) {
	defer out.Flush()
	fmt.Fprintln(out, x)
}

func PrintHorizonaly(x []int) {
	defer out.Flush()
	fmt.Fprintf(out, "%d", x[0])
	for i := 1; i < len(x); i++ {
		fmt.Fprintf(out, " %d", x[i])
	}
	fmt.Fprintln(out)
}

func PrintVertically(x []int) {
	defer out.Flush()
	for _, v := range x {
		fmt.Fprintln(out, v)
	}
}

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

func Min(x, y int) int {
	if x < y {
		return x
	}
	return y
}

func Max(x, y int) int {
	if x < y {
		return y
	}
	return x
}

func Floor(x, y int) int {
	return x / y
}

func Ceil(x, y int) int {
	return (x + y - 1) / y
}
0