|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bufio" |
| 5 | + . "fmt" |
| 6 | + "os" |
| 7 | + "sort" |
| 8 | + "strconv" |
| 9 | +) |
| 10 | + |
| 11 | +// https://space.bilibili.com/206214 |
| 12 | +type interaction97 interface { |
| 13 | + readInitData() initData97 |
| 14 | + query(request97) response97 |
| 15 | + printAnswer(answer97) |
| 16 | +} |
| 17 | + |
| 18 | +type stdIO97 struct { |
| 19 | + in *bufio.Reader |
| 20 | + out *bufio.Writer |
| 21 | +} |
| 22 | + |
| 23 | +type ( |
| 24 | + initData97 struct{ n int } |
| 25 | + request97 struct{ q []int } |
| 26 | + response97 struct{ res string } |
| 27 | + answer97 struct{ ans string } |
| 28 | +) |
| 29 | + |
| 30 | +func (io stdIO97) readInitData() initData97 { |
| 31 | + in := io.in |
| 32 | + |
| 33 | + var n int |
| 34 | + Fscan(in, &n) |
| 35 | + |
| 36 | + return initData97{n} |
| 37 | +} |
| 38 | + |
| 39 | +func (io stdIO97) query(q request97) (resp response97) { |
| 40 | + in, out := io.in, io.out |
| 41 | + |
| 42 | + Fprint(out, "?") |
| 43 | + for _, v := range q.q { |
| 44 | + Fprint(out, " ", v) |
| 45 | + } |
| 46 | + Fprintln(out) |
| 47 | + |
| 48 | + out.Flush() |
| 49 | + |
| 50 | + Fscan(in, &resp.res) |
| 51 | + |
| 52 | + if resp.res == "0" { |
| 53 | + panic(-1) |
| 54 | + } |
| 55 | + return |
| 56 | +} |
| 57 | + |
| 58 | +func (io stdIO97) printAnswer(a answer97) { |
| 59 | + out := io.out |
| 60 | + |
| 61 | + Fprintln(out, "!", a.ans) |
| 62 | + |
| 63 | + out.Flush() |
| 64 | +} |
| 65 | + |
| 66 | +func doInteraction97(it interaction97) { |
| 67 | + dt := it.readInitData() |
| 68 | + n := dt.n |
| 69 | + |
| 70 | + getChar := func(i int) byte { |
| 71 | + return it.query(request97{[]int{1, i + 1}}).res[0] |
| 72 | + } |
| 73 | + getDiff := func(l, r int) int { |
| 74 | + v, _ := strconv.Atoi(it.query(request97{[]int{2, l + 1, r + 1}}).res) |
| 75 | + return v |
| 76 | + } |
| 77 | + |
| 78 | + ans := make([]byte, n) |
| 79 | + defer func() { it.printAnswer(answer97{string(ans)}) }() |
| 80 | + |
| 81 | + type pair struct { |
| 82 | + i int |
| 83 | + b byte |
| 84 | + } |
| 85 | + pos := []pair{} |
| 86 | + for i := n - 1; i >= 0; i-- { |
| 87 | + j := sort.Search(len(pos), func(j int) bool { return getDiff(i, pos[j].i) == j+1 }) |
| 88 | + if j < len(pos) { |
| 89 | + ans[i] = pos[j].b |
| 90 | + pos = append(pos[:j], pos[j+1:]...) |
| 91 | + } else { |
| 92 | + ans[i] = getChar(i) |
| 93 | + } |
| 94 | + pos = append([]pair{{i, ans[i]}}, pos...) |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +func run97() { |
| 99 | + in := bufio.NewReader(os.Stdin) |
| 100 | + out := bufio.NewWriter(os.Stdout) |
| 101 | + |
| 102 | + T := 1 |
| 103 | + for ; T > 0; T-- { |
| 104 | + doInteraction97(stdIO97{in, out}) |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +//func main() { run97() } |
0 commit comments