Skip to content

Instantly share code, notes, and snippets.

@Amar1729
Created April 8, 2018 02:41
Show Gist options
  • Save Amar1729/3f2bf6be81e2df0f99a3541f70471ee8 to your computer and use it in GitHub Desktop.
Save Amar1729/3f2bf6be81e2df0f99a3541f70471ee8 to your computer and use it in GitHub Desktop.
package main
import "fmt"
import "bufio"
import "sort"
import "os"
import "strings"
import "strconv"
func convert_str(inp string) []int {
var output []int
var curr int
curr = 1
for _, elem := range(inp) {
if elem == 'C' {
curr = curr * 2
} else {
output = append(output, curr)
}
}
return output
}
func calc(N int, inp string) int {
var result int
var str []int
str = convert_str(inp)
// temporary integer
var tmp int
// sum to keep track of
var sum int
if len(str) > N {
result = -1
} else {
// get current total
for i := 0; i < len(str); i++ {
sum = sum + str[i]
}
for sum > N {
// 'decrement' last boi and sum
tmp = str[len(str)-1] / 2
sum = sum - str[len(str)-1]/2
// resort :/
str[len(str)-1] = tmp
sort.Ints(str)
result = result + 1
}
}
return result
}
func main() {
reader := bufio.NewReader(os.Stdin)
var T int
fmt.Scanf("%d", &T)
for i := 0; i < T; i++ {
line, _, _ := reader.ReadLine()
s := strings.Split(string(line), " ")
N, _ := strconv.Atoi(s[0])
inp := s[1]
tmp := calc(N, inp)
fmt.Print("Case #", i+1)
fmt.Print(": ")
if tmp == -1 {
fmt.Println("IMPOSSIBLE")
} else {
fmt.Println(tmp)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment