結果

問題 No.29 パワーアップ
ユーザー yo-kondoyo-kondo
提出日時 2018-03-10 15:20:06
言語 Go
(1.22.1)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 1,601 bytes
コンパイル時間 16,600 ms
コンパイル使用メモリ 233,572 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-21 12:42:51
合計ジャッジ時間 14,818 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,248 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 1 ms
5,376 KB
testcase_09 AC 1 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 2 ms
5,376 KB
testcase_14 AC 2 ms
5,376 KB
testcase_15 AC 1 ms
5,376 KB
testcase_16 AC 2 ms
5,376 KB
testcase_17 AC 2 ms
5,376 KB
testcase_18 AC 2 ms
5,376 KB
testcase_19 AC 2 ms
5,376 KB
testcase_20 AC 2 ms
5,376 KB
testcase_21 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package main

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

// エントリポイント
func main() {
	in := bufio.NewScanner(os.Stdin)
	in.Scan()
	// 敵を倒す回数
	input1 := in.Text()
	// 敵を倒した時のもらえる3つのアイテムの番号
	input2 := make([]string, 0)
	for in.Scan() {
		input2 = append(input2, in.Text())
	}

	fmt.Println(powerUp(input1, input2))
}

// パワーアップする最大の回数を求める。
func powerUp(num string, items []string) string {
	_ = num

	intItems := make([]int, 0)
	// 2次元配列のstringを1次元配列のintに変換
	for _, v := range items {
		sp := strings.Split(v, " ")

		for _, v2 := range sp {
			i, _ := strconv.Atoi(v2)
			intItems = append(intItems, i)
		}
	}

	sort.Sort(sort.IntSlice(intItems))

	count := 0
	loop := true
	// 同じ値が2つあるか
	for loop {
		intItems, loop = removePair(intItems)
		if loop {
			count++
		}
	}

	// 値が4つあるか
	count += len(intItems) / 4

	return strconv.Itoa(count)
}

// 線形探索で同一の値を削除した配列を返す。事前にソートしておくこと。
// 同一の値があれば、同一の値を削除した配列と、trueを返す。
// 同一の値があれば、変更のない配列と、falseを返す。
func removePair(items []int) ([]int, bool) {
	rtnAry := make([]int, 0)
	for i := 0; i < len(items)-1; i++ {
		if items[i] == items[i+1] {
			for i2 := range items {
				if i2 == i || i2 == i + 1 {
					continue
				}
				rtnAry = append(rtnAry, items[i2])
			}
			return rtnAry, true
		}
	}
	return items, false
}
0