From 30170e3d98da3475a62265dc25bfde52dff3371e Mon Sep 17 00:00:00 2001 From: WangSenyuan Date: Wed, 12 Jun 2024 20:28:24 +0800 Subject: [PATCH] aaa --- .../set1/set19/set192/set1925/d/problem.md | 19 ++ .../set1/set19/set192/set1925/d/solution.go | 113 ++++++++ .../set1/set19/set197/set1970/d/solution.go | 140 ++++++++++ .../set19/set197/set1970/d/solution_test.go | 51 ++++ .../set1/set19/set197/set1970/d1/problem.md | 56 ---- .../set1/set19/set197/set1973/b/problem.md | 31 +++ .../set1/set19/set197/set1973/b/solution.go | 126 +++++++++ .../set19/set197/set1973/b/solution_test.go | 35 +++ .../set1/set19/set197/set1975/e/problem.md | 21 ++ .../set1/set19/set197/set1975/e/solution.go | 249 ++++++++++++++++++ .../set19/set197/set1975/e/solution_test.go | 90 +++++++ 11 files changed, 875 insertions(+), 56 deletions(-) create mode 100644 src/codeforces/set1/set19/set192/set1925/d/problem.md create mode 100644 src/codeforces/set1/set19/set192/set1925/d/solution.go create mode 100644 src/codeforces/set1/set19/set197/set1970/d/solution.go create mode 100644 src/codeforces/set1/set19/set197/set1970/d/solution_test.go delete mode 100644 src/codeforces/set1/set19/set197/set1970/d1/problem.md create mode 100644 src/codeforces/set1/set19/set197/set1973/b/problem.md create mode 100644 src/codeforces/set1/set19/set197/set1973/b/solution.go create mode 100644 src/codeforces/set1/set19/set197/set1973/b/solution_test.go create mode 100644 src/codeforces/set1/set19/set197/set1975/e/problem.md create mode 100644 src/codeforces/set1/set19/set197/set1975/e/solution.go create mode 100644 src/codeforces/set1/set19/set197/set1975/e/solution_test.go diff --git a/src/codeforces/set1/set19/set192/set1925/d/problem.md b/src/codeforces/set1/set19/set192/set1925/d/problem.md new file mode 100644 index 00000000..1cf61ef9 --- /dev/null +++ b/src/codeforces/set1/set19/set192/set1925/d/problem.md @@ -0,0 +1,19 @@ +Let's imagine the surface of Mars as an infinite coordinate plane. Initially, the rover Perseverance-2 and the helicopter Ingenuity-2 are located at the point with coordinates (0,0) +. A set of instructions 𝑠 + consisting of 𝑛 + instructions of the following types was specially developed for them: + +N: move one meter north (from point (𝑥,𝑦) + to (𝑥,𝑦+1) +); +S: move one meter south (from point (𝑥,𝑦) + to (𝑥,𝑦−1) +); +E: move one meter east (from point (𝑥,𝑦) + to (𝑥+1,𝑦) +); +W: move one meter west (from point (𝑥,𝑦) + to (𝑥−1,𝑦) +). +Each instruction must be executed either by the rover or by the helicopter. Moreover, each device must execute at least one instruction. Your task is to distribute the instructions in such a way that after executing all 𝑛 + instructions, the helicopter and the rover end up at the same point, or determine that this is impossible. \ No newline at end of file diff --git a/src/codeforces/set1/set19/set192/set1925/d/solution.go b/src/codeforces/set1/set19/set192/set1925/d/solution.go new file mode 100644 index 00000000..d8b6c7e0 --- /dev/null +++ b/src/codeforces/set1/set19/set192/set1925/d/solution.go @@ -0,0 +1,113 @@ +package main + +import ( + "bufio" + "bytes" + "fmt" + "os" +) + +func main() { + reader := bufio.NewReader(os.Stdin) + + tc := readNum(reader) + + var buf bytes.Buffer + + for tc > 0 { + tc-- + n := readNum(reader) + s := readString(reader)[:n] + res := solve(s) + buf.WriteString(res) + buf.WriteByte('\n') + } + + fmt.Print(buf.String()) +} + +func readString(reader *bufio.Reader) string { + s, _ := reader.ReadString('\n') + for i := 0; i < len(s); i++ { + if s[i] == '\n' || s[i] == '\r' { + return s[:i] + } + } + return s +} + +func readInt(bytes []byte, from int, val *int) int { + i := from + sign := 1 + if bytes[i] == '-' { + sign = -1 + i++ + } + tmp := 0 + for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' { + tmp = tmp*10 + int(bytes[i]-'0') + i++ + } + *val = tmp * sign + return i +} + +func readNum(reader *bufio.Reader) (a int) { + bs, _ := reader.ReadBytes('\n') + readInt(bs, 0, &a) + return +} + +func readTwoNums(reader *bufio.Reader) (a int, b int) { + res := readNNums(reader, 2) + a, b = res[0], res[1] + return +} + +func readThreeNums(reader *bufio.Reader) (a int, b int, c int) { + res := readNNums(reader, 3) + a, b, c = res[0], res[1], res[2] + return +} + +func readNNums(reader *bufio.Reader, n int) []int { + res := make([]int, n) + x := 0 + bs, _ := reader.ReadBytes('\n') + for i := 0; i < n; i++ { + for x < len(bs) && (bs[x] < '0' || bs[x] > '9') && bs[x] != '-' { + x++ + } + x = readInt(bs, x, &res[i]) + } + return res +} + +func solve(instructions string) string { + n := len(instructions) + var x, y int + for i := 0; i < n; i++ { + if instructions[i] == 'N' { + x++ + } else if instructions[i] == 'S' { + x-- + } else if instructions[i] == 'E' { + y++ + } else { + y-- + } + } + + if x%2 != 0 || y%2 != 0 { + return "NO" + } + if x == 0 && y == 0 && len(instructions) == 2 { + return "NO" + } + ans := make([]byte, n) + for i := 0; i < n; i++ { + ans[i] = 'R' + } + + return string(ans) +} diff --git a/src/codeforces/set1/set19/set197/set1970/d/solution.go b/src/codeforces/set1/set19/set197/set1970/d/solution.go new file mode 100644 index 00000000..8e4b052f --- /dev/null +++ b/src/codeforces/set1/set19/set197/set1970/d/solution.go @@ -0,0 +1,140 @@ +package main + +import ( + "bufio" + "bytes" + "fmt" + "os" +) + +func main() { + reader := bufio.NewReader(os.Stdin) + + n := readNum(reader) + + play := func(f func(int) []int) { + q := readNum(reader) + + for i := 0; i < q; i++ { + p := readNum(reader) + res := f(p) + fmt.Printf("%d %d\n", res[0], res[1]) + } + } + + solve(n, func(sets []string) { + var buf bytes.Buffer + for i := 0; i < n; i++ { + buf.WriteString(sets[i]) + buf.WriteByte('\n') + } + fmt.Print(buf.String()) + }, play) + +} +func readString(reader *bufio.Reader) string { + s, _ := reader.ReadString('\n') + for i := 0; i < len(s); i++ { + if s[i] == '\n' || s[i] == '\r' { + return s[:i] + } + } + return s +} + +func readInt(bytes []byte, from int, val *int) int { + i := from + sign := 1 + if bytes[i] == '-' { + sign = -1 + i++ + } + tmp := 0 + for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' { + tmp = tmp*10 + int(bytes[i]-'0') + i++ + } + *val = tmp * sign + return i +} + +func readNum(reader *bufio.Reader) (a int) { + bs, _ := reader.ReadBytes('\n') + readInt(bs, 0, &a) + return +} + +func readTwoNums(reader *bufio.Reader) (a int, b int) { + res := readNNums(reader, 2) + a, b = res[0], res[1] + return +} + +func readThreeNums(reader *bufio.Reader) (a int, b int, c int) { + res := readNNums(reader, 3) + a, b, c = res[0], res[1], res[2] + return +} + +func readNNums(reader *bufio.Reader, n int) []int { + res := make([]int, n) + x := 0 + bs, _ := reader.ReadBytes('\n') + for i := 0; i < n; i++ { + for x < len(bs) && (bs[x] < '0' || bs[x] > '9') && bs[x] != '-' { + x++ + } + x = readInt(bs, x, &res[i]) + } + return res +} + +var p = []int{1, 2, 6, 8, 12, 16, 20, 24, 30, 36, 40, 44, 48, 56, 60, 65, 72, 81, 88, 100, 107, 116, 131, 140, 144, 156, 160, 162, 171, 176, 192, 200, 210, 212, 221, 225, 242, 248, 254, 261, 276, 280, 292, 300, 309, 336, 351, 357, 362, 371, 378, 380, 396, 410, 417, 456, 465, 477, 501, 507, 522, 540, 546, 550, 552, 556, 597, 611, 651, 657, 672, 696, 707, 722, 732, 756, 760, 771, 786, 800, 852, 857, 861, 875, 877, 885, 920, 945, 950, 972, 976, 980, 996, 1002, 1027, 1037, 1041, 1060, 1080, 1092, 1124, 1130, 1152, 1173, 1185, 1200, 1212, 1227, 1236, 1260, 1271, 1275, 1277, 1281, 1285, 1338, 1344, 1365, 1380, 1386, 1390, 1407, 1431, 1437, 1452, 1485, 1501, 1520, 1530, 1536, 1542, 1561, 1565, 1610, 1617, 1635, 1653, 1704, 1712, 1722, 1731, 1736, 1821, 1850, 1872, 1887, 1900, 1905, 1911, 1917, 1932, 1940, 1948, 1981, 1992, 1997, 2016, 2025, 2037, 2096, 2100, 2104, 2110, 2120, 2135, 2178, 2205, 2216, 2220, 2227, 2241, 2332, 2337, 2346, 2370, 2380, 2400, 2432, 2445, 2452, 2467, 2472, 2488, 2516, 2520, 2530, 2554, 2578, 2592, 2600, 2632, 2656, 2688, 2706, 2710, 2720, 2730, 2740, 2795, 2802, 2811, 2824, 2841, 2856, 2892, 2901, 2915, 2955, 2961, 2976, 2984, 2997, 3021, 3060, 3082, 3096, 3098, 3104, 3112, 3117, 3140, 3165, 3173, 3186, 3192, 3201, 3216, 3252, 3257, 3306, 3317, 3321, 3340, 3360, 3370, 3385, 3412, 3462, 3480, 3492, 3516, 3531, 3550, 3577, 3591, 3600, 3612, 3621, 3642, 3675, 3700, 3717, 3732, 3737, 3822, 3851, 3861, 3880, 3896, 3930, 3941, 3945, 3956, 3961, 4005, 4020, 4026, 4032, 4050, 4103, 4125, 4131, 4137, 4176, 4196, 4224, 4240, 4251, 4257, 4280, 4347, 4356, 4371, 4396, 4404, 4424, 4440, 4491, 4531, 4545, 4560, 4567, 4585, 4602, 4620, 4650, 4720, 4722, 4785, 4797, 4820, 4875, 4901, 4917, 4929, 4944, 4972, 5005, 5007, 5037, 5076, 5085, 5096, 5100, 5112, 5145, 5226, 5232, 5292, 5296, 5307, 5340, 5365, 5384, 5397, 5411, 5435, 5440, 5445, 5456, 5491, 5501, 5505, 5517, 5571, 5580, 5636, 5645, 5667, 5690, 5700, 5748, 5799, 5826, 5852, 5860, 5865, 5877, 5922, 5937, 6000, 6007, 6036, 6072, 6085, 6120, 6124, 6132, 6156, 6196, 6216, 6315, 6330, 6357, 6372, 6425, 6427, 6443, 6450, 6457, 6471, 6481, 6501, 6580, 6597, 6657, 6700, 6716, 6736, 6780, 6796, 6812, 6840, 6861, 6882, 6897, 6915, 6917, 6947, 6960, 6981, 6996, 7010, 7032, 7056, 7076, 7080, 7130, 7140, 7147, 7165, 7182, 7212, 7228, 7242, 7261, 7321, 7331, 7350, 7380, 7382, 7392, 7413, 7491, 7497, 7506, 7515, 7521, 7527, 7536, 7557, 7576, 7587, 7605, 7641, 7690, 7692, 7700, 7722, 7752, 7816, 7821, 7832, 7856, 7890, 7917, 7932, 7947, 7976, 8037, 8052, 8060, 8091, 8100, 8145, 8156, 8196, 8220, 8236, 8246, 8284, 8313, 8330, 8336, 8346, 8352, 8385, 8421, 8432, 8460, 8484, 8514, 8541, 8565, 8576, 8592, 8652, 8676, 8712, 8725, 8752, 8781, 8842, 8892, 8960, 8964, 9012, 9027, 9032, 9036, 9057, 9066, 9090, 9132, 9146, 9170, 9180, 9186, 9192, 9225, 9234, 9240, 9276, 9284, 9321, 9357, 9402, 9412, 9435, 9440, 9464, 9472, 9501, 9570, 9585, 9625, 9660, 9675, 9696, 9720, 9741, 9747, 9765, 9780, 9816, 9849, 9857, 9880, 9885, 9900, 9906, 9937, 9972, 9984, 9990, 9996, 10000, 10005, 10010, 10040, 10067, 10101, 10117, 10161, 10192, 10200, 10252, 10272, 10290, 10332, 10336, 10340, 10356, 10416, 10452, 10531, 10557, 10565, 10605, 10650, 10665, 10685, 10707, 10747, 10760, 10836, 10851, 10860, 10909, 10956, 11001, 11056, 11066, 11112, 11125, 11137, 11160, 11172, 11180, 11211, 11272, 11277, 11300, 11316, 11330, 11377, 11400, 11421, 11427, 11442, 11472, 11481, 11487, 11496, 11547, 11556, 11586, 11596, 11620, 11676, 11697, 11704, 11720, 11767, 11805, 11811, 11832, 11865, 11937, 11991, 12041, 12072, 12084, 12105, 12114, 12128, 12156, 12162, 12195, 12200, 12225, 12240, 12261, 12300, 12345, 12400, 12420, 12432, 12456, 12471, 12485, 12492, 12512, 12525, 12537, 12546, 12552, 12572, 12600, 12645, 12651, 12656, 12685, 12720, 12756, 12761, 12807, 12827, 12840, 12882, 12891, 12897, 12960, 12981, 13002, 13041, 13056, 13092, 13101, 13125, 13140, 13145, 13181, 13220, 13227, 13257, 13308, 13344, 13356, 13370, 13380, 13387, 13416, 13420, 13436, 13517, 13521, 13612, 13680, 13713, 13748, 13827, 13875, 13920, 13932, 13940, 13962, 13996, 14050, 14080, 14085, 14106, 14112, 14141, 14147, 14157, 14186, 14205, 14224, 14226, 14241, 14256, 14260, 14322, 14345, 14372, 14385, 14399, 14421, 14436, 14466, 14504, 14526, 14561, 14592, 14596, 14616, 14620, 14682, 14690, 14697, 14721, 14736, 14820, 14865, 14886, 14890, 14896, 14900, 14905, 15012, 15041, 15067, 15072, 15080, 15096, 15138, 15186, 15226, 15240, 15267, 15276, 15296, 15330, 15360, 15372, 15396, 15444, 15472, 15627, 15642, 15677, 15681, 15732, 15752, 15756, 15764, 15785, 15796, 15800, 15827, 15844, 15852, 15857, 15864, 15936, 16002, 16010, 16032, 16051, 16060, 16116, 16137, 16160, 16177, 16220, 16227, 16320, 16332, 16362, 16367, 16380, 16397, 16411, 16416, 16485, 16492, 16503, 16545, 16572, 16580, 16592, 16626, 16634, 16647, 16656, 16660, 16732, 16802, 16841, 16856, 16920, 17020, 17025, 17040, 17052, 17076, 17160, 17252, 17256, 17280, 17292, 17320, 17325, 17347, 17376, 17385, 17391, 17397, 17437, 17461, 17481, 17517, 17562, 17607, 17682, 17696, 17725, 17732, 17805, 17817, 17857, 17880, 17901, 17941, 18012, 18020, 18085, 18090, 18096, 18132, 18180, 18216, 18252, 18272, 18285, 18300, 18312, 18345, 18396, 18404, 18408, 18440, 18476, 18480, 18488, 18501, 18552, 18585, 18595, 18636, 18690, 18741, 18752, 18792, 18837, 18915, 18932, 18957, 19035, 19057, 19077, 19100, 19205, 19210, 19236, 19266, 19300, 19320, 19377, 19440, 19476, 19482, 19516, 19540, 19581, 19605, 19656, 19747, 19770, 19800, 19812, 19840, 19856, 19860, 20010, 20076, 20121, 20151, 20181, 20196, 20202, 20281, 20322, 20336, 20352, 20475, 20485, 20532, 20541, 20605, 20610, 20625, 20640, 20661, 20727, 20764, 20787, 20850, 20856, 20871, 20917, 20946, 20972, 21000, 21029, 21056, 21060, 21091, 21177, 21188, 21192, 21210, 21225, 21276, 21312, 21324, 21380, 21465, 21476, 21490, 21505, 21561, 21576, 21597, 21660, 21672, 21732, 21765, 21770, 21781, 21816, 21856, 22002, 22092, 22107, 22297, 22341, 22392, 22396, 22500, 22512, 22572, 22611, 22620, 22640, 22717, 22737, 22747, 22785, 22810, 22842, 22936, 22941, 22962, 22971, 22991, 23005, 23025, 23037, 23052, 23061, 23157, 23162, 23172, 23184, 23226, 23236, 23284, 23296, 23313, 23332, 23370, 23377, 23426, 23430, 23485, 23556, 23565, 23601, 23622, 23640, 23653, 23661, 23676, 23740, 23751, 23760, 23766, 23772} + +func solve(n int, initilaizer func([]string), play func(func(int) []int)) { + sets := make([]string, n) + pref := "XO" + prev := 0 + for i := 0; i < n; i++ { + tmp := pref + for j := prev; j < p[i]; j++ { + tmp += "X" + } + sets[i] = tmp + pref = tmp + prev = p[i] + } + + calc := func(x, y int) int { + var ans int + + ans += max(x+1, y) + // 包含一个o + // 1/x, x/y-1 + ans += (1+1)*(x+2) + (x+2)*(y+1) - min(1+1, x+2)*min(x+2, y+1) + // // 左1右x+1,左x+1右y + // 包含两个o + ans += (1 + 1) * (y + 1) + + return ans + } + + ans := make(map[int]Pair) + for i := 0; i < n; i++ { + for j := 0; j < n; j++ { + ans[calc(p[i], p[j])] = Pair{i + 1, j + 1} + } + } + + initilaizer(sets) + + play(func(power int) []int { + res := ans[power] + return []int{res.first, res.second} + }) +} + +type Pair struct { + first int + second int +} diff --git a/src/codeforces/set1/set19/set197/set1970/d/solution_test.go b/src/codeforces/set1/set19/set197/set1970/d/solution_test.go new file mode 100644 index 00000000..c3a86d62 --- /dev/null +++ b/src/codeforces/set1/set19/set197/set1970/d/solution_test.go @@ -0,0 +1,51 @@ +package main + +import ( + "math/rand" + "testing" +) + +func runSample(t *testing.T, n int, q int) { + var sets []string + + interact := func(f func(int) []int) { + i := rand.Intn(n) + j := rand.Intn(n) + power := calc(sets[i] + sets[j]) + res := f(power) + + if len(res) == 0 || res[0] != i+1 || res[1] != j+1 { + t.Fatalf("Sample (%d, %d): %s, %d result %v, not correct", i, j, sets[i]+sets[j], power, res) + } + } + + solve(n, func(s []string) { + sets = s + }, interact) +} + +func calc(s string) int { + cache := make(map[string]int) + for i := 0; i < len(s); i++ { + for j := i; j < len(s); j++ { + cache[s[i:j+1]]++ + } + } + return len(cache) +} + +func TestSample1(t *testing.T) { + runSample(t, 2, 3) +} + +func TestSample2(t *testing.T) { + runSample(t, 3, 9) +} + +func TestSample3(t *testing.T) { + runSample(t, 10, 100) +} + +func TestSample4(t *testing.T) { + runSample(t, 3, 100) +} diff --git a/src/codeforces/set1/set19/set197/set1970/d1/problem.md b/src/codeforces/set1/set19/set197/set1970/d1/problem.md deleted file mode 100644 index 346ecc8e..00000000 --- a/src/codeforces/set1/set19/set197/set1970/d1/problem.md +++ /dev/null @@ -1,56 +0,0 @@ -Professor Vector is preparing to teach her Arithmancy class. She needs to prepare 𝑛 - distinct magic words for the class. Each magic word is a string consisting of characters X and O. A spell is a string created by concatenating two magic words together. The power of a spell is equal to the number of its different non-empty substrings. For example, the power of the spell XOXO is equal to 7, because it has 7 different substrings: X, O, XO, OX, XOX, OXO and XOXO. - -Each student will create their own spell by concatenating two magic words. Since the students are not very good at magic yet, they will choose each of the two words independently and uniformly at random from the 𝑛 - words provided by Professor Vector. It is therefore also possible that the two words a student chooses are the same. Each student will then compute the power of their spell, and tell it to Professor Vector. In order to check their work, and of course to impress the students, Professor Vector needs to find out which two magic words and in which order were concatenated by each student. - -Your program needs to perform the role of Professor Vector: first, create 𝑛 - distinct magic words, and then handle multiple requests where it is given the spell power and needs to determine the indices of the two magic words, in the correct order, that were used to create the corresponding spell. - -Interaction -This is an interactive problem. - -First, your program should read a single integer 𝑛 - (1≤𝑛≤3 -), the number of magic words to prepare. Then, it should print 𝑛 - magic words it has created, one per line. The magic words must be distinct, each magic word must have at least 1 and at most 30⋅𝑛 - characters, and each character must be either X or O. We will denote the 𝑖 --th magic word you printed as 𝑤𝑖 - (1≤𝑖≤𝑛 -). - -Then, your program should read a single integer 𝑞 - (1≤𝑞≤1000 -), the number of students in the class. Then, it should repeat the following process 𝑞 - times, one per student. - -For the 𝑗 --th student, it should first read a single integer 𝑝𝑗 -, the power of their spell. It is guaranteed that this number is computed by choosing two indices 𝑢𝑗 - and 𝑣𝑗 - independently and uniformly at random between 1 and 𝑛 - inclusive, concatenating 𝑤𝑢𝑗 - and 𝑤𝑣𝑗 -, and finding the number of different non-empty substrings of the resulting string. Then, your program must print the numbers 𝑢𝑗 - and 𝑣𝑗 -, in this order (1≤𝑢𝑗,𝑣𝑗≤𝑛 -). - -Note that it is not enough to find any two magic words that concatenate into a spell with the given power. You must find the exact words used by the student in the exact order. - -Remember to flush the output stream after printing all magic words and after printing 𝑢𝑗 - and 𝑣𝑗 - for each student. - - - ### ideas - 1. x, o, xx - 2. if n = 1, just x? xx => 2 - 3. if n = 2, x and o => xo => x, o, xo = 3, ox => o, x, ox = 3 不行 - 4. xx, o => xxxx = 4, xxo (x, xx, xo, xxo) 4, 也不行 - 5. xo, o => xoxo = 7, xoo (x, o, oo, xoo, xo) 5, oo (o oo) 2, oxo (o, ox, xo, oxo, x) 5 (wrong) - 6. 这个题目完全没想法啊 - 7. n = 2, XOXO X, 这个例子可以用来解决 n = 2, 但是规律是什么? - 8. XOXOX (x, xo, xox, oxo, xoxo, oxox, xoxox) - 9. xoxoxoxo (它包含上面的,且多了几个, 所以可以区分出来) - 10. xxoxo (x, xx, xo, ox, xox, oxo, xxox, xoxo, xxoxo) \ No newline at end of file diff --git a/src/codeforces/set1/set19/set197/set1973/b/problem.md b/src/codeforces/set1/set19/set197/set1973/b/problem.md new file mode 100644 index 00000000..5cd1dc87 --- /dev/null +++ b/src/codeforces/set1/set19/set197/set1973/b/problem.md @@ -0,0 +1,31 @@ +Today, Cat and Fox found an array 𝑎 + consisting of 𝑛 + non-negative integers. + +Define the loneliness of 𝑎 + as the smallest positive integer 𝑘 + (1≤𝑘≤𝑛 +) such that for any two positive integers 𝑖 + and 𝑗 + (1≤𝑖,𝑗≤𝑛−𝑘+1 +), the following holds: +𝑎𝑖|𝑎𝑖+1|…|𝑎𝑖+𝑘−1=𝑎𝑗|𝑎𝑗+1|…|𝑎𝑗+𝑘−1, +where 𝑥|𝑦 + denotes the bitwise OR of 𝑥 + and 𝑦 +. In other words, for every 𝑘 + consecutive elements, their bitwise OR should be the same. Note that the loneliness of 𝑎 + is well-defined, because for 𝑘=𝑛 + the condition is satisfied. + +Cat and Fox want to know how lonely the array 𝑎 + is. Help them calculate the loneliness of the found array. + +### ideas +1. 如果 k = x, 成立, k = x + 1 成立吗? +2. 似乎是成立的 +3. 考虑第一个和第二个序列, +4. a[1] | ... | a[x] = a[2] | ... | a[x+1] +5. a[2] | ... | a[x+1] = a[3] | ... | a[x+2] +6. 上下两边相与,左右还是相等的 +7. 所以可以二分 \ No newline at end of file diff --git a/src/codeforces/set1/set19/set197/set1973/b/solution.go b/src/codeforces/set1/set19/set197/set1973/b/solution.go new file mode 100644 index 00000000..139ca2ea --- /dev/null +++ b/src/codeforces/set1/set19/set197/set1973/b/solution.go @@ -0,0 +1,126 @@ +package main + +import ( + "bufio" + "bytes" + "fmt" + "os" + "sort" +) + +func main() { + reader := bufio.NewReader(os.Stdin) + + tc := readNum(reader) + + var buf bytes.Buffer + + for tc > 0 { + tc-- + n := readNum(reader) + a := readNNums(reader, n) + res := solve(a) + buf.WriteString(fmt.Sprintf("%d\n", res)) + } + + fmt.Print(buf.String()) +} +func readString(reader *bufio.Reader) string { + s, _ := reader.ReadString('\n') + for i := 0; i < len(s); i++ { + if s[i] == '\n' || s[i] == '\r' { + return s[:i] + } + } + return s +} + +func readInt(bytes []byte, from int, val *int) int { + i := from + sign := 1 + if bytes[i] == '-' { + sign = -1 + i++ + } + tmp := 0 + for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' { + tmp = tmp*10 + int(bytes[i]-'0') + i++ + } + *val = tmp * sign + return i +} + +func readNum(reader *bufio.Reader) (a int) { + bs, _ := reader.ReadBytes('\n') + readInt(bs, 0, &a) + return +} + +func readTwoNums(reader *bufio.Reader) (a int, b int) { + res := readNNums(reader, 2) + a, b = res[0], res[1] + return +} + +func readThreeNums(reader *bufio.Reader) (a int, b int, c int) { + res := readNNums(reader, 3) + a, b, c = res[0], res[1], res[2] + return +} + +func readNNums(reader *bufio.Reader, n int) []int { + res := make([]int, n) + x := 0 + bs, _ := reader.ReadBytes('\n') + for i := 0; i < n; i++ { + for x < len(bs) && (bs[x] < '0' || bs[x] > '9') && bs[x] != '-' { + x++ + } + x = readInt(bs, x, &res[i]) + } + return res +} + +const D = 20 + +func solve(nums []int) int { + n := len(nums) + + cnt := make([][]int, D) + for i := 0; i < D; i++ { + cnt[i] = make([]int, n+1) + for j := 0; j < n; j++ { + cnt[i][j+1] = cnt[i][j] + (nums[j]>>i)&1 + } + } + + get := func(l int, r int) int { + var res int + for i := 0; i < D; i++ { + if cnt[i][r] > cnt[i][l] { + res |= 1 << i + } + } + return res + } + + check := func(k int) bool { + if k == 0 { + return false + } + + val := get(0, k) + + for i := 1; i+k <= n; i++ { + tmp := get(i, i+k) + if tmp != val { + return false + } + } + + return true + } + + return sort.Search(n, check) +} diff --git a/src/codeforces/set1/set19/set197/set1973/b/solution_test.go b/src/codeforces/set1/set19/set197/set1973/b/solution_test.go new file mode 100644 index 00000000..9bc26927 --- /dev/null +++ b/src/codeforces/set1/set19/set197/set1973/b/solution_test.go @@ -0,0 +1,35 @@ +package main + +import "testing" + +func runSample(t *testing.T, nums []int, expect int) { + res := solve(nums) + + if res != expect { + t.Fatalf("Sample expect %d, but got %d", expect, res) + } +} + +func TestSample1(t *testing.T) { + nums := []int{0} + expect := 1 + runSample(t, nums, expect) +} + +func TestSample2(t *testing.T) { + nums := []int{2, 2, 2} + expect := 1 + runSample(t, nums, expect) +} + +func TestSample3(t *testing.T) { + nums := []int{1, 0, 2} + expect := 3 + runSample(t, nums, expect) +} + +func TestSample4(t *testing.T) { + nums := []int{3, 0, 1, 4, 2} + expect := 4 + runSample(t, nums, expect) +} diff --git a/src/codeforces/set1/set19/set197/set1975/e/problem.md b/src/codeforces/set1/set19/set197/set1975/e/problem.md new file mode 100644 index 00000000..c4169adc --- /dev/null +++ b/src/codeforces/set1/set19/set197/set1975/e/problem.md @@ -0,0 +1,21 @@ +You are given a tree of 𝑛 + vertices numbered from 1 + to 𝑛 +. Initially, all vertices are colored white or black. + +You are asked to perform 𝑞 + queries: + +"u" — toggle the color of vertex 𝑢 + (if it was white, change it to black and vice versa). +After each query, you should answer whether all the black vertices form a chain. That is, there exist two black vertices such that the simple path between them passes through all the black vertices and only the black vertices. Specifically, if there is only one black vertex, they form a chain. If there are no black vertices, they do not form a chain. + +### ideas +1. 考虑假设存在这样一个chain,其中的一个端点的depth肯定是最大的 +2. 另外一个端点,就不好说了 +3. 然后,那个depth最小的点,肯定也在这个path上 +4. 考虑那个 heavy-light 分解,所形成的线段树,如果存在这样一个path,那么从那个最低点u开始,但那个最高点p,它们必然是连续的几段 +5. 然后,在确认的过程中,先标记成white,然后再找到最低的点,继续处理一次 +6. 最后再恢复 +7. range update + range query +8. \ No newline at end of file diff --git a/src/codeforces/set1/set19/set197/set1975/e/solution.go b/src/codeforces/set1/set19/set197/set1975/e/solution.go new file mode 100644 index 00000000..7e19344c --- /dev/null +++ b/src/codeforces/set1/set19/set197/set1975/e/solution.go @@ -0,0 +1,249 @@ +package main + +import ( + "bufio" + "bytes" + "fmt" + "os" +) + +func main() { + reader := bufio.NewReader(os.Stdin) + + tc := readNum(reader) + + var buf bytes.Buffer + + for tc > 0 { + tc-- + n, m := readTwoNums(reader) + c := readNNums(reader, n) + edges := make([][]int, n-1) + for i := 0; i < n-1; i++ { + edges[i] = readNNums(reader, 2) + } + queries := make([]int, m) + for i := 0; i < m; i++ { + queries[i] = readNum(reader) + } + res := solve(n, c, edges, queries) + + for _, x := range res { + if x { + buf.WriteString("Yes\n") + } else { + buf.WriteString("No\n") + } + } + } + + fmt.Print(buf.String()) +} +func readString(reader *bufio.Reader) string { + s, _ := reader.ReadString('\n') + for i := 0; i < len(s); i++ { + if s[i] == '\n' || s[i] == '\r' { + return s[:i] + } + } + return s +} + +func readInt(bytes []byte, from int, val *int) int { + i := from + sign := 1 + if bytes[i] == '-' { + sign = -1 + i++ + } + tmp := 0 + for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' { + tmp = tmp*10 + int(bytes[i]-'0') + i++ + } + *val = tmp * sign + return i +} + +func readNum(reader *bufio.Reader) (a int) { + bs, _ := reader.ReadBytes('\n') + readInt(bs, 0, &a) + return +} + +func readTwoNums(reader *bufio.Reader) (a int, b int) { + res := readNNums(reader, 2) + a, b = res[0], res[1] + return +} + +func readThreeNums(reader *bufio.Reader) (a int, b int, c int) { + res := readNNums(reader, 3) + a, b, c = res[0], res[1], res[2] + return +} + +func readNNums(reader *bufio.Reader, n int) []int { + res := make([]int, n) + x := 0 + bs, _ := reader.ReadBytes('\n') + for i := 0; i < n; i++ { + for x < len(bs) && (bs[x] < '0' || bs[x] > '9') && bs[x] != '-' { + x++ + } + x = readInt(bs, x, &res[i]) + } + return res +} + +type Pair struct { + first int + second int +} + +func solve(n int, color []int, edges [][]int, queries []int) []bool { + g := NewGraph(n+1, 2*n) + + for _, edge := range edges { + u, v := edge[0], edge[1] + g.AddEdge(u, v) + g.AddEdge(v, u) + } + + c := make([]int, n+1) + copy(c[1:], color) + color = c + + var faw, sum_two, sum_more, black_cnt, xor_two int + + fa := make([]int, n+1) + black_child_cnt := make([]int, n+1) + + var dfs func(p int, u int) + dfs = func(p int, u int) { + fa[u] = p + if color[u] == 1 { + black_cnt++ + } + for i := g.nodes[u]; i > 0; i = g.next[i] { + v := g.to[i] + if p != v { + dfs(u, v) + if color[v] == 1 { + black_child_cnt[u]++ + } + } + } + + if color[p] == 0 && color[u] == 1 { + faw++ + } + if color[u] == 1 { + if black_child_cnt[u] == 2 { + sum_two++ + xor_two ^= u + } else if black_child_cnt[u] > 2 { + sum_more++ + } + } + } + + dfs(0, 1) + + flip := func(x int) { + color[x] ^= 1 + + var d int + + if color[x] == 0 { + d = -1 + } else { + d = 1 + } + black_cnt += d + + if color[fa[x]] == 0 { + faw += d + } + + if black_child_cnt[x] == 2 { + sum_two += d + xor_two ^= x + } + if black_child_cnt[x] > 2 { + sum_more += d + } + faw -= d * black_child_cnt[x] + + if color[x] == 1 { + if color[fa[x]] == 1 && black_child_cnt[fa[x]] == 2 { + sum_two-- + sum_more++ + xor_two ^= fa[x] + } + black_child_cnt[fa[x]]++ + + if color[fa[x]] == 1 && black_child_cnt[fa[x]] == 2 { + sum_two++ + xor_two ^= fa[x] + } + } else { + if color[fa[x]] == 1 && black_child_cnt[fa[x]] == 2 { + sum_two-- + xor_two ^= fa[x] + } + black_child_cnt[fa[x]]-- + if color[fa[x]] == 1 && black_child_cnt[fa[x]] == 2 { + sum_two++ + sum_more-- + xor_two ^= fa[x] + } + } + } + + check := func() bool { + if black_cnt == 0 { + return false + } + if sum_more > 0 || sum_two > 1 { + return false + } + if faw > 1 { + return false + } + if sum_two == 1 && color[fa[xor_two]] == 1 { + return false + } + return true + } + + ans := make([]bool, len(queries)) + + for i, u := range queries { + flip(u) + ans[i] = check() + } + + return ans +} + +type Graph struct { + nodes []int + next []int + to []int + cur int +} + +func NewGraph(n int, e int) *Graph { + nodes := make([]int, n) + next := make([]int, e) + to := make([]int, e) + return &Graph{nodes, next, to, 0} +} + +func (g *Graph) AddEdge(u, v int) { + g.cur++ + g.next[g.cur] = g.nodes[u] + g.nodes[u] = g.cur + g.to[g.cur] = v +} diff --git a/src/codeforces/set1/set19/set197/set1975/e/solution_test.go b/src/codeforces/set1/set19/set197/set1975/e/solution_test.go new file mode 100644 index 00000000..12cda56e --- /dev/null +++ b/src/codeforces/set1/set19/set197/set1975/e/solution_test.go @@ -0,0 +1,90 @@ +package main + +import ( + "reflect" + "testing" +) + +func runSample(t *testing.T, n int, c []int, edges [][]int, queries []int, expect []bool) { + res := solve(n, c, edges, queries) + + if !reflect.DeepEqual(res, expect) { + t.Fatalf("Sample expect %v, but got %v", expect, res) + } +} + +func TestSample1(t *testing.T) { + n := 2 + c := []int{1, 0} + edges := [][]int{ + {1, 2}, + } + queries := []int{1} + expect := []bool{false} + runSample(t, n, c, edges, queries, expect) +} + +func TestSample2(t *testing.T) { + n := 5 + c := []int{1, 0, 0, 0, 0} + edges := [][]int{ + {1, 2}, + {1, 3}, + {1, 5}, + {3, 4}, + } + queries := []int{4, 3, 2, 5} + expect := []bool{false, true, true, false} + runSample(t, n, c, edges, queries, expect) +} + +func TestSample3(t *testing.T) { + n := 5 + c := []int{1, 1, 1, 1, 1} + edges := [][]int{ + {3, 5}, + {2, 5}, + {3, 4}, + {1, 5}, + } + queries := []int{1, 1, 1} + expect := []bool{true, false, true} + runSample(t, n, c, edges, queries, expect) +} + +func TestSample4(t *testing.T) { + n := 4 + c := []int{0, 0, 0, 0} + edges := [][]int{ + {1, 2}, + {2, 3}, + {1, 4}, + } + queries := []int{1, 2, 3, 2} + expect := []bool{true, true, true, false} + runSample(t, n, c, edges, queries, expect) +} + +func TestSample5(t *testing.T) { + n := 1 + c := []int{0} + + queries := []int{1} + expect := []bool{true} + runSample(t, n, c, nil, queries, expect) +} + +func TestSample6(t *testing.T) { + n := 5 + c := []int{0, 0, 0, 0, 0} + edges := [][]int{ + {1, 2}, + {2, 3}, + {3, 4}, + {4, 5}, + } + + queries := []int{1, 2, 3, 4, 5, 2, 3, 4, 5} + expect := []bool{true, true, true, true, true, false, false, false, true} + runSample(t, n, c, edges, queries, expect) +}