結果
問題 | No.771 しおり |
ユーザー |
|
提出日時 | 2019-06-27 21:43:30 |
言語 | Go (1.23.4) |
結果 |
AC
|
実行時間 | 485 ms / 2,000 ms |
コード長 | 7,499 bytes |
コンパイル時間 | 14,381 ms |
コンパイル使用メモリ | 221,408 KB |
実行使用メモリ | 40,156 KB |
最終ジャッジ日時 | 2024-07-22 07:14:19 |
合計ジャッジ時間 | 21,069 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 43 |
ソースコード
package mainimport ("bufio""errors""fmt""io""os""strconv")/*********** I/O ***********/var (// ReadString returns a WORD string.ReadString func() stringstdout *bufio.Writer)func init() {ReadString = newReadString(os.Stdin)stdout = bufio.NewWriter(os.Stdout)}func newReadString(ior io.Reader) func() string {r := bufio.NewScanner(ior)r.Buffer(make([]byte, 1024), int(1e+11))// Split sets the split function for the Scanner. The default split function is ScanLines.// Split panics if it is called after scanning has started.r.Split(bufio.ScanWords)return func() string {if !r.Scan() {panic("Scan failed")}return r.Text()}}// ReadInt returns an integer.func ReadInt() int {return int(readInt64())}func readInt64() int64 {i, err := strconv.ParseInt(ReadString(), 0, 64)if err != nil {panic(err.Error())}return i}// ReadIntSlice returns an integer slice that has n integers.func ReadIntSlice(n int) []int {b := make([]int, n)for i := 0; i < n; i++ {b[i] = ReadInt()}return b}// ReadFloat64 returns an float64.func ReadFloat64() float64 {return float64(readFloat64())}func readFloat64() float64 {f, err := strconv.ParseFloat(ReadString(), 64)if err != nil {panic(err.Error())}return f}// ReadFloatSlice returns an float64 slice that has n float64.func ReadFloat64Slice(n int) []float64 {b := make([]float64, n)for i := 0; i < n; i++ {b[i] = ReadFloat64()}return b}// ReadRuneSlice returns a rune slice.func ReadRuneSlice() []rune {return []rune(ReadString())}/*********** Debugging ***********/// ZeroPaddingRuneSlice returns binary expressions of integer n with zero padding.// For debugging use.func ZeroPaddingRuneSlice(n, digitsNum int) []rune {sn := fmt.Sprintf("%b", n)residualLength := digitsNum - len(sn)if residualLength <= 0 {return []rune(sn)}zeros := make([]rune, residualLength)for i := 0; i < len(zeros); i++ {zeros[i] = '0'}res := []rune{}res = append(res, zeros...)res = append(res, []rune(sn)...)return res}/*********** DP sub-functions ***********/// ChMin accepts a pointer of integer and a target value.// If target value is SMALLER than the first argument,// then the first argument will be updated by the second argument.func ChMin(updatedValue *int, target int) bool {if *updatedValue > target {*updatedValue = targetreturn true}return false}// ChMax accepts a pointer of integer and a target value.// If target value is LARGER than the first argument,// then the first argument will be updated by the second argument.func ChMax(updatedValue *int, target int) bool {if *updatedValue < target {*updatedValue = targetreturn true}return false}// NthBit returns nth bit value of an argument.// n starts from 0.func NthBit(num, nth int) int {return num >> uint(nth) & 1}/*********** Arithmetic ***********/// Max returns the max integer among input set.// This function needs at least 1 argument (no argument causes panic).func Max(integers ...int) int {m := integers[0]for i, integer := range integers {if i == 0 {continue}if m < integer {m = integer}}return m}// Min returns the min integer among input set.// This function needs at least 1 argument (no argument causes panic).func Min(integers ...int) int {m := integers[0]for i, integer := range integers {if i == 0 {continue}if m > integer {m = integer}}return m}// DigitSum returns digit sum of a decimal number.// DigitSum only accept a positive integer.func DigitSum(n int) int {if n < 0 {return -1}res := 0for n > 0 {res += n % 10n /= 10}return res}// Sum returns multiple integers sum.func Sum(integers ...int) int {s := 0for _, i := range integers {s += i}return s}// CeilInt returns the minimum integer larger than or equal to float(a/b).func CeilInt(a, b int) int {res := a / bif a%b > 0 {res++}return res}// FloorInt returns the maximum integer smaller than or equal to float(a/b)func FloorInt(a, b int) int {res := a / breturn res}// PowInt is integer version of math.Pow// PowInt calculate a power by Binary Power (二分累乗法(O(log e))).func PowInt(a, e int) int {if a < 0 || e < 0 {panic(errors.New("[argument error]: PowInt does not accept negative integers"))}if e == 0 {return 1}if e%2 == 0 {halfE := e / 2half := PowInt(a, halfE)return half * half}return a * PowInt(a, e-1)}// AbsInt is integer version of math.Absfunc AbsInt(a int) int {if a < 0 {return -a}return a}// Gcd returns the Greatest Common Divisor of two natural numbers.// Gcd only accepts two natural numbers (a, b >= 1).// 0 or negative number causes panic.// Gcd uses the Euclidean Algorithm.func Gcd(a, b int) int {if a <= 0 || b <= 0 {panic(errors.New("[argument error]: Gcd only accepts two NATURAL numbers"))}if a < b {a, b = b, a}// Euclidean Algorithmfor b > 0 {div := a % ba, b = b, div}return a}// Lcm returns the Least Common Multiple of two natural numbers.// Lcd only accepts two natural numbers (a, b >= 1).// 0 or negative number causes panic.// Lcd uses the Euclidean Algorithm indirectly.func Lcm(a, b int) int {if a <= 0 || b <= 0 {panic(errors.New("[argument error]: Gcd only accepts two NATURAL numbers"))}// a = a'*gcd, b = b'*gcd, a*b = a'*b'*gcd^2// a' and b' are relatively prime numbers// gcd consists of prime numbers, that are included in a and bgcd := Gcd(a, b)// not (a * b / gcd), because of reducing a probability of overflowreturn (a / gcd) * b}// Strtoi is a wrapper of `strconv.Atoi()`.// If `strconv.Atoi()` returns an error, Strtoi calls panic.func Strtoi(s string) int {if i, err := strconv.Atoi(s); err != nil {panic(errors.New("[argument error]: Strtoi only accepts integer string"))} else {return i}}// PrintIntsLine returns integers string delimited by a space.func PrintIntsLine(A ...int) string {res := []rune{}for i := 0; i < len(A); i++ {str := strconv.Itoa(A[i])res = append(res, []rune(str)...)if i != len(A)-1 {res = append(res, ' ')}}return string(res)}/********** I/O usage **********///str := ReadString()//i := ReadInt()//X := ReadIntSlice(n)//S := ReadRuneSlice()/*******************************************************************/const MOD = 1000000000 + 7const ALPHABET_NUM = 26var n intvar A, B []intvar dp [1 << 18][18]intfunc main() {n = ReadInt()A, B = make([]int, n), make([]int, n)for i := 0; i < n; i++ {A[i], B[i] = ReadInt(), ReadInt()}if n == 1 {fmt.Println(0)return}for S := 0; S < (1 << uint(n)); S++ {for j := 0; j < n; j++ {dp[S][j] = 1 << 60}}for S := 0; S < (1 << uint(n)); S++ {for j := 0; j < n; j++ {for k := 0; k < n; k++ {if S == 0 {dp[S|1<<uint(j)][j] = B[j] - A[j]continue}if NthBit(S, j) == 0 && NthBit(S, k) == 1 {ChMin(&dp[S|1<<uint(j)][j], Max(dp[S][k], B[k]-A[k]+A[j]))}}}}ans := 1 << 60for j := 0; j < n; j++ {ChMin(&ans, dp[1<<uint(n)-1][j])}fmt.Println(ans)// for S := 0; S < (1 << uint(n)); S++ {// fmt.Println(dp[S][:n])// }}// MODはとったか?// 遷移だけじゃなくて最後の最後でちゃんと取れよ?/*******************************************************************/