結果

問題 No.334 門松ゲーム
ユーザー yukirinyukirin
提出日時 2016-01-26 13:44:06
言語 Go
(1.22.1)
結果
AC  
実行時間 50 ms / 2,000 ms
コード長 2,605 bytes
コンパイル時間 10,658 ms
コンパイル使用メモリ 231,656 KB
実行使用メモリ 20,736 KB
最終ジャッジ日時 2024-04-19 04:29:30
合計ジャッジ時間 11,427 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 12 ms
5,760 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 5 ms
5,376 KB
testcase_08 AC 4 ms
5,376 KB
testcase_09 AC 3 ms
5,376 KB
testcase_10 AC 11 ms
5,376 KB
testcase_11 AC 3 ms
5,376 KB
testcase_12 AC 49 ms
18,816 KB
testcase_13 AC 5 ms
5,376 KB
testcase_14 AC 12 ms
5,888 KB
testcase_15 AC 50 ms
20,736 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package main

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

type collection struct {
	index []int
	val   []int
	cols  []*collection
}

var sc = bufio.NewScanner(os.Stdin)
var rdr = bufio.NewReaderSize(os.Stdin, 1000000)

func main() {
	sc.Split(bufio.ScanWords)
	n := nextInt()
	var nums []int

	for i := 0; i < n; i++ {
		nums = append(nums, nextInt())
	}
	c := &collection{
		val: nums,
	}
	build(c)

	var f, s, t []*collection

	for _, v := range c.cols {
		if len(v.cols) == 0 {
			f = append(f, v)
			break
		}
	}
	if len(nums) < 9 {
		if f == nil {
			fmt.Println(-1)
			return
		}
		fmt.Println(trim(f[0].index))
		return
	}

	for _, v := range c.cols {
		all := true
		for _, c := range v.cols {
			if len(c.cols) == 0 {
				all = false
				break
			}
		}
		if all {
			s = append(s, v)
		}
	}
	if len(nums) < 12 {
		if s == nil {
			fmt.Println(-1)
			return
		}
		fmt.Println(trim(s[0].index))
		return
	}

	for _, v := range s {
		all := true
		for _, c := range v.cols {
			sub := false
			for _, r := range c.cols {
				if len(r.cols) == 0 {
					sub = true
					break
				}
			}
			if !sub {
				all = false
				break
			}

		}
		if all {
			t = append(t, v)
		}
	}
	if t == nil {
		fmt.Println(-1)
		return
	}
	fmt.Println(trim(t[0].index))
}

func trim(a []int) string {
	s := fmt.Sprintf("%v", a)
	return s[1 : len(s)-1]
}
func build(c *collection) {
	for i := 0; i < len(c.val)-2; i++ {
		for j := i + 1; j < len(c.val)-1; j++ {
			for k := j + 1; k < len(c.val); k++ {
				if !check(c.val[i], c.val[j], c.val[k]) {
					continue
				}
				col := &collection{
					index: []int{i, j, k},
				}
				col.val = append(col.val, c.val[:i]...)
				col.val = append(col.val, c.val[i+1:j]...)
				col.val = append(col.val, c.val[j+1:k]...)
				col.val = append(col.val, c.val[k+1:]...)
				c.cols = append(c.cols, col)
				build(col)
			}
		}
	}
}

func check(a, b, c int) bool {
	if (b < a && a < c) || (c < a && a < b) {
		return true
	}
	if (a < c && c < b) || (b < c && c < a) {
		return true
	}
	return false
}

func nextLine() string {
	sc.Scan()
	return sc.Text()
}

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

func nextInt64() int64 {
	i, _ := strconv.ParseInt(nextLine(), 10, 64)
	return i
}

func nextUint64() uint64 {
	i, _ := strconv.ParseUint(nextLine(), 10, 64)
	return i
}

func nextFloat() float64 {
	f, _ := strconv.ParseFloat(nextLine(), 64)
	return f
}

func readLine() string {
	buf := make([]byte, 0, 1000000)
	for {
		l, p, e := rdr.ReadLine()
		if e != nil {
			panic(e)
		}
		buf = append(buf, l...)
		if !p {
			break
		}
	}
	return string(buf)
}
0