結果

問題 No.154 市バス
ユーザー aru aruaru aru
提出日時 2020-08-24 21:57:31
言語 Go
(1.21.3)
結果
AC  
実行時間 25 ms / 2,000 ms
コード長 1,884 bytes
コンパイル時間 10,831 ms
コンパイル使用メモリ 210,520 KB
実行使用メモリ 5,572 KB
最終ジャッジ日時 2023-08-06 07:50:24
合計ジャッジ時間 11,682 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 24 ms
5,572 KB
testcase_01 AC 25 ms
5,568 KB
testcase_02 AC 23 ms
5,572 KB
testcase_03 AC 17 ms
5,568 KB
testcase_04 AC 23 ms
5,568 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 14 ms
5,572 KB
testcase_08 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package main

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

func out(x ...interface{}) {
	fmt.Println(x...)
}

var sc = bufio.NewScanner(os.Stdin)

func getInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

func getInts(N int) []int {
	ret := make([]int, N)
	for i := 0; i < N; i++ {
		ret[i] = getInt()
	}
	return ret
}

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

// min, max, asub, absなど基本関数
func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

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

func asub(a, b int) int {
	if a > b {
		return a - b
	}
	return b - a
}

func abs(a int) int {
	if a >= 0 {
		return a
	}
	return -a
}

func lowerBound(a []int, x int) int {
	idx := sort.Search(len(a), func(i int) bool {
		return a[i] >= x
	})
	return idx
}

func upperBound(a []int, x int) int {
	idx := sort.Search(len(a), func(i int) bool {
		return a[i] > x
	})
	return idx
}

func solve(s []byte) {
	w, g, r := 0, 0, 0
	wst := 0
	for i := 0; i < len(s); i++ {
		switch s[i] {
		case 'R':
			r++
			if wst == 1 {
				wst = 0
			}
		case 'G':
			g++
			if wst == 2 {
				wst = 1
			}
		case 'W':
			w++
			wst = 2
		}
	}
	if w >= g && g == r {
	} else {
		out("impossible")
		return
	}
	if wst != 0 {
		out("impossible")
		return
	}
	for i, j := 0, 0; i < len(s); i++ {
		if s[i] == 'W' {
			if j >= r {
				s[i] = 'x'
			}
			j++
		}
	}
	r, g, w = 0, 0, 0
	for i := 0; i < len(s); i++ {
		switch s[i] {
		case 'W':
			w++
		case 'G':
			g++
		case 'R':
			r++
		}
		if w >= g && g >= r && w >= r {
			continue
		}

		out("impossible")
		return
	}
	if w == g && g == r {
		out("possible")
		return
	}
	out("impossible")
}

func main() {
	sc.Split(bufio.ScanWords)
	sc.Buffer([]byte{}, 1000000)
	T := getInt()
	for i := 0; i < T; i++ {
		s := getString()
		solve([]byte(s))
	}
}
0