-
Notifications
You must be signed in to change notification settings - Fork 27
/
435.go
51 lines (46 loc) · 947 Bytes
/
435.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// UVa 435 - Block Voting
package main
import (
"fmt"
"os"
)
func solve(p, total int, votes []int) []int {
powerIndex := make([]int, p)
for i := 0; i < (1 << uint(p)); i++ {
var sum int
for j := range votes {
if i&(1<<uint(j)) != 0 {
sum += votes[j]
}
}
if sum <= total/2 {
for j := range votes {
if i&(1<<uint(j)) == 0 && sum+votes[j] > total/2 {
powerIndex[j]++
}
}
}
}
return powerIndex
}
func main() {
in, _ := os.Open("435.in")
defer in.Close()
out, _ := os.Create("435.out")
defer out.Close()
var kase, p int
for fmt.Fscanf(in, "%d", &kase); kase > 0; kase-- {
fmt.Fscanf(in, "%d", &p)
var total int
votes := make([]int, p)
for i := range votes {
fmt.Fscanf(in, "%d", &votes[i])
total += votes[i]
}
powerIndex := solve(p, total, votes)
for i, power := range powerIndex {
fmt.Fprintf(out, "party %d has power index %d\n", i+1, power)
}
fmt.Fprintln(out)
}
}