From 4af8a8974e59f0becc60b7b97e7ec270d6a8f5e2 Mon Sep 17 00:00:00 2001
From: Index2022 <2033541709@qq.com>
Date: Mon, 23 Dec 2024 17:41:36 +0800
Subject: [PATCH] =?UTF-8?q?=09modified:=20=20=20.vscode/settings.json=20?=
 =?UTF-8?q?=09new=20file:=20=20=20bytedance/=E5=B0=8FC=E7=9A=84=E7=B1=BB?=
 =?UTF-8?q?=E4=BA=8C=E8=BF=9B=E5=88=B6=E6=8B=BC=E5=9B=BE.py=20=09new=20fil?=
 =?UTF-8?q?e:=20=20=20bytedance/=E6=89=93=E7=82=B9=E8=AE=A1=E6=97=B6?=
 =?UTF-8?q?=E5=99=A8=E7=9A=84=E5=8C=BA=E9=97=B4=E5=90=88=E5=B9=B6.py=20=09?=
 =?UTF-8?q?new=20file:=20=20=20bytedance/=E6=9C=80=E5=A4=A7UCC=E5=AD=90?=
 =?UTF-8?q?=E4=B8=B2=E8=AE=A1=E7=AE=97.py=20=09modified:=20=20=20bytedance?=
 =?UTF-8?q?/=E6=9C=80=E5=A4=A7=E8=BF=9E=E7=BB=AD=E5=AD=90=E6=95=B0?=
 =?UTF-8?q?=E7=BB=84=E5=92=8C=E9=97=AE=E9=A2=98.py=20=09modified:=20=20=20?=
 =?UTF-8?q?test.cpp=20=09modified:=20=20=20test.py=20=09modified:=20=20=20?=
 =?UTF-8?q?test2.cpp?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .vscode/settings.json                         |   3 +-
 ...33\345\210\266\346\213\274\345\233\276.py" |  25 ++++
 ...72\351\227\264\345\220\210\345\271\266.py" |  21 +++
 ...20\344\270\262\350\256\241\347\256\227.py" |  62 ++++++++
 ...04\345\222\214\351\227\256\351\242\230.py" |   2 +-
 test.cpp                                      | 134 ++++++++++++++----
 test.py                                       |   1 +
 test2.cpp                                     |  83 ++++++++---
 8 files changed, 276 insertions(+), 55 deletions(-)
 create mode 100644 "bytedance/\345\260\217C\347\232\204\347\261\273\344\272\214\350\277\233\345\210\266\346\213\274\345\233\276.py"
 create mode 100644 "bytedance/\346\211\223\347\202\271\350\256\241\346\227\266\345\231\250\347\232\204\345\214\272\351\227\264\345\220\210\345\271\266.py"
 create mode 100644 "bytedance/\346\234\200\345\244\247UCC\345\255\220\344\270\262\350\256\241\347\256\227.py"

diff --git a/.vscode/settings.json b/.vscode/settings.json
index df7410e..10ea886 100644
--- a/.vscode/settings.json
+++ b/.vscode/settings.json
@@ -97,5 +97,6 @@
         "typeinfo": "cpp",
         "valarray": "cpp",
         "variant": "cpp"
-    }
+    },
+    "python.analysis.typeCheckingMode": "off"
 }
\ No newline at end of file
diff --git "a/bytedance/\345\260\217C\347\232\204\347\261\273\344\272\214\350\277\233\345\210\266\346\213\274\345\233\276.py" "b/bytedance/\345\260\217C\347\232\204\347\261\273\344\272\214\350\277\233\345\210\266\346\213\274\345\233\276.py"
new file mode 100644
index 0000000..1d6cbcb
--- /dev/null
+++ "b/bytedance/\345\260\217C\347\232\204\347\261\273\344\272\214\350\277\233\345\210\266\346\213\274\345\233\276.py"
@@ -0,0 +1,25 @@
+def solution(n: str) -> int:
+    def check(s: str) -> bool:
+        """
+        # 判断s中是否含有除了0和1之外的字符
+        for i in s:
+            if i != '0' and i != '1':
+                return False
+        return True
+        """
+        return all(c in "01" for c in s)
+
+    ans = 1
+    while not check(n):
+        tosub = "".join("1" if c > "1" else c for c in n)
+        n = str(int(n) - int(tosub))
+        ans += 1
+    return ans
+
+
+if __name__ == "__main__":
+    print(solution("10101") == 1)
+    print(solution("212") == 2)
+    print(solution("1000000") == 1)
+    print(solution("123456789") == 9)
+    print(solution("9876543210") == 9)
diff --git "a/bytedance/\346\211\223\347\202\271\350\256\241\346\227\266\345\231\250\347\232\204\345\214\272\351\227\264\345\220\210\345\271\266.py" "b/bytedance/\346\211\223\347\202\271\350\256\241\346\227\266\345\231\250\347\232\204\345\214\272\351\227\264\345\220\210\345\271\266.py"
new file mode 100644
index 0000000..8d7ae64
--- /dev/null
+++ "b/bytedance/\346\211\223\347\202\271\350\256\241\346\227\266\345\231\250\347\232\204\345\214\272\351\227\264\345\220\210\345\271\266.py"
@@ -0,0 +1,21 @@
+def solution(inputArray):
+    inputArray.sort()  # list的比较大小方法就是从第一个元素开始比较,如果第一个元素相等,就比较第二个元素,以此类推
+    ans = 0
+    st, ed = inputArray[0]
+    for begin, end in inputArray[1:]:
+        if begin > ed:
+            ans += ed - st + 1
+            st, ed = begin, end
+        else:
+            ed = max(ed, end)
+    ans += ed - st + 1
+    return ans
+
+
+if __name__ == "__main__":
+    #  You can add more test cases here
+    testArray1 = [[1, 4], [7, 10], [3, 5]]
+    testArray2 = [[1, 2], [6, 10], [11, 15]]
+
+    print(solution(testArray1) == 9)
+    print(solution(testArray2) == 12)
diff --git "a/bytedance/\346\234\200\345\244\247UCC\345\255\220\344\270\262\350\256\241\347\256\227.py" "b/bytedance/\346\234\200\345\244\247UCC\345\255\220\344\270\262\350\256\241\347\256\227.py"
new file mode 100644
index 0000000..93b471d
--- /dev/null
+++ "b/bytedance/\346\234\200\345\244\247UCC\345\255\220\344\270\262\350\256\241\347\256\227.py"
@@ -0,0 +1,62 @@
+"""
+题解:https://bytedance.larkoffice.com/docx/WGGidwQk3o5h3XxjWGEcHwDGnZb
+
+经验:题目中给了多个字符串的操作,我们需要先考虑化简这些操作。考虑哪些操作可以被其他操作替代,哪些操作对结果没有影响。这道题的结论是插入是最优的操作,删除是完全不操作的,替换操作完全可以由插入操作代替。
+
+已知只执行插入操作,那如何操作可以得到最多的UCC子串?
+前提:字符不能重复利用
+1. 优先记录已经有的UCC子串的个数
+2. 优先记录UC和CC的个数,因为它们只需要插入一次就可以得到UCC子串
+3. 剩下的字符,需要插入两次才能得到UCC子串
+4. 如果对现有的字符进行插入操作后次数还有剩余,那么剩余的次数可以用来直接插入UCC
+"""
+
+
+def solution(m: int, s: str) -> int:
+    res, n = 0, 0
+    i = 0
+    length = len(s)
+    while i < length:
+        if (
+            s[i : i + 3] == "UCC"
+        ):  # 这里的判断条件可以更简洁,不用每次都判断i是否越界 因为如果i+3越界的话,s[i:i+3]得到的是s[i:],不会报错,也不会等于'UCC'
+            res += 1
+            i += 3
+        elif s[i : i + 2] == "UC" or s[i : i + 2] == "CC":
+            n += 1
+            i += 2
+        else:
+            i += 1
+
+    # 优化的写法,更简洁,但是不容易理解
+    ans = res + min(n, m)
+    m = max(0, m - n)
+    remaining = length - res * 3 - n * 2
+    ans += min(remaining, m // 2)
+    m = max(0, m - remaining * 2)  # m -= min(m, remaining * 2) 也可以
+    ans += m // 3
+    """
+    ans = res
+    if n >= m:
+        ans += m
+    else:
+        ans += n
+        m -= n
+        twice = len(s) - res * 3 - n * 2
+        if m >= twice * 2:
+            ans += twice
+            m -= twice * 2
+            ans += m // 3
+        else:
+            ans += m // 2
+    """
+    return ans
+
+
+if __name__ == "__main__":
+    print(solution(m=3, s="UCUUCCCCC") == 3)
+    print(solution(m=6, s="U") == 2)
+    print(solution(m=2, s="UCCUUU") == 2)
+
+
+# UC U UCC CC C
diff --git "a/bytedance/\346\234\200\345\244\247\350\277\236\347\273\255\345\255\220\346\225\260\347\273\204\345\222\214\351\227\256\351\242\230.py" "b/bytedance/\346\234\200\345\244\247\350\277\236\347\273\255\345\255\220\346\225\260\347\273\204\345\222\214\351\227\256\351\242\230.py"
index 81c477d..1cda817 100644
--- "a/bytedance/\346\234\200\345\244\247\350\277\236\347\273\255\345\255\220\346\225\260\347\273\204\345\222\214\351\227\256\351\242\230.py"
+++ "b/bytedance/\346\234\200\345\244\247\350\277\236\347\273\255\345\255\220\346\225\260\347\273\204\345\222\214\351\227\256\351\242\230.py"
@@ -3,7 +3,7 @@
 朴素的思路:枚举替换的位置,然后计算替换后的最大子数组和
 对于每个位置i,计算包含a[i]的最大子数组和的方法是先计算前面的元素的贡献和后面的元素的贡献,然后再加上max(a[i], x)。而计算贡献可以用前缀和来优化。
 
-这道题与最大子数组和的区别在于,最大子数组和是不允许替换元素的,而这道题是允许替换元素的。对于前者,我们可以枚举每个位置i,然后计算以a[i]结尾的最大子数组和,最后取最大值。但是在这道题中,如果采用同样方法,我们就无法考虑替换后的a[i]对后面的数的贡献。因此我们枚举的是包含a[i]的最大子数组和,而不是以a[i]结尾的最大子数组和。
+这道题与最大连续子数组和模板题的区别在于,模板题是不允许替换元素的,而这道题是允许替换元素的。对于前者,我们可以枚举每个位置i,然后计算以a[i]结尾的最大连续子数组和,最后取最大值。但是在这道题中,如果采用同样方法,我们就无法考虑替换后的a[i]对后面的数的贡献。因此我们枚举的是包含a[i]的最大子数组和,而不是以a[i]结尾的最大子数组和。
 '''
 def solution(n: int, x: int, a: list) -> int:
     # 将a数组中的最小值替换为x
diff --git a/test.cpp b/test.cpp
index 51b31f9..3cab272 100644
--- a/test.cpp
+++ b/test.cpp
@@ -1,44 +1,118 @@
-#include <bits/stdc++.h>
+#include <algorithm>
+#include <iostream>
+#include <vector>
 using namespace std;
-inline int read()
+
+const int N = 110;
+int a[N], b[N];
+int n;
+
+bool compare(const vector<int> &arr1, const vector<int> &arr2)
+{
+    for (int i = 0; i < arr1.size(); i++)
+        if (arr1[i] != arr2[i])
+            return false;
+    return true;
+}
+
+void insertionStep(vector<int> &arr, int step) // 这是第step+1步
 {
-    int x = 0, f = 1;
-    char ch = getchar();
-    while (!isdigit(ch))
+    for (int i = 0; i < step; i++)
     {
-        f = ch != '-';
-        ch = getchar();
+        if (arr[i] <= arr[step])
+            continue;
+        else
+        {
+            int temp = arr[step];
+            for (int j = step - 1; j >= i; j--)
+                arr[j + 1] = arr[j];
+            arr[i] = temp;
+            break;
+        }
     }
-    while (isdigit(ch))
+}
+
+void HeapAdjust(vector<int> &arr, int start, int fina)
+{
+    int temp = arr[start];
+    for (int j = 2 * start + 1; j <= fina; j = j * 2 + 1)
     {
-        x = (x << 1) + (x << 3) + (ch ^ 48);
-        ch = getchar();
+        if (j < fina && arr[j] < arr[j + 1])
+            j++;
+        if (temp >= arr[j])
+            break;
+        arr[start] = arr[j];
+        start = j;
     }
-    return f ? x : -x;
+    arr[start] = temp;
 }
-int n;
-bitset<110> a[110];
+
+void Heap(vector<int> &arr, int step) // 这是第step+1步
+{
+    swap(arr[0], arr[arr.size() - step]);
+    HeapAdjust(arr, 0, arr.size() - step - 1);
+}
+
+
+
 int main()
 {
-    n = read();
+    
+    cin >> n;
+    vector<int> initial(n), middle(n);
+    vector<int> temp(n);
+
+    for (int i = 0; i < n; i++)
+    {
+        cin >> initial[i];
+        temp[i] = initial[i];
+    }
+    for (int i = 0; i < n; i++)
+        cin >> middle[i];
+
+    // for (int i = 0; i < n; i++)
+    // {
+    //     insertionStep(initial, i);
+    //     if (compare(initial, middle))
+    //     {
+    //         cout << "Insertion Sort" << endl;
+    //         if (i < n - 1)
+    //         {
+    //             while (true)
+    //             {
+    //                 insertionStep(initial, i + 1); // modified
+    //                 if (compare(initial, middle))
+    //                     i++;
+    //                 else
+    //                     break;
+    //             }
+                
+    //         }
+    //         cout << initial[0];
+    //         for (int i = 1; i < n; i++)
+    //             cout << " " << initial[i];
+    //         return 0;
+    //     }
+    // }
+
+    for (int i = 0; i < n; i++)
+        initial[i] = temp[i];
+
+
+    for (int i = (initial.size() - 1) / 2; i >= 0; i--)
+        HeapAdjust(initial, i, initial.size()-1);//建初始大堆
+
     for (int i = 1; i <= n; i++)
-        for (int j = 1; j <= n; j++)
+    {
+        Heap(initial, i);
+        if (compare(initial, middle))
         {
-            int x;
-            cin >> x;
-            a[i][j] = x;
+            cout << "Heap Sort" << endl;
+            Heap(initial, i + 1);
+            cout << initial[0];
+            for (int i = 1; i < n; i++)
+                cout << " " << initial[i];
+            return 0;
         }
-    for (int i = 1; i <= n; i++)
-        cout << a[i] << "\n";
-    for (int j = 1; j <= n; j++)//注意j循环在i循环外
-        for (int i = 1; i <= n; i++)
-            if (a[i][j])
-                a[i] |= a[j];//bitset也挺好写的
-    for (int i = 1; i <= n; i++)
-    {
-        for (int j = 1; j <= n; j++)
-            putchar(a[i][j] + '0'), putchar(' ');
-        putchar('\n');
     }
-    return 0;
 }
\ No newline at end of file
diff --git a/test.py b/test.py
index e69de29..607ec7e 100644
--- a/test.py
+++ b/test.py
@@ -0,0 +1 @@
+print("你好")
\ No newline at end of file
diff --git a/test2.cpp b/test2.cpp
index 54fef5c..dd40d4c 100644
--- a/test2.cpp
+++ b/test2.cpp
@@ -1,36 +1,73 @@
 #include <bits/stdc++.h>
 using namespace std;
-int n;
-bitset<110> a[110];
-int main()
+
+using ll = long long;
+using pii = pair<int, int>;
+const int N = 110;
+int a[N], b[N], backup[N], n;
+
+bool isInsert()
 {
-    ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
-    cin >> n;
-    for (int i = 1; i <= n; i++)
+    bool flag = false;
+    for (int i = 2; i <= n; i ++ )
     {
-        for (int j = 1; j <= n; j++)
-        {
-            bool x;
-            cin >> x;
-            cout << x << "\n";
-            a[i][j] = x;
-        }
+        a[0] = a[i];
+        int pos = i - 1;
+        while (a[pos] > a[0]) a[pos + 1] = a[pos], pos -- ;
+        a[pos + 1] = a[0]; 
+        if (flag) return true;
+        if (equal(b + 1, b + n + 1, a + 1)) flag = true;
     }
-    for (int j = 1; j <= n; j++)
+    return false;
+}
+
+void down(int u, int sz)
+{
+    int t = u;
+    if (u * 2 <= sz && a[u * 2] > a[t]) t = 2 * u;
+    if (u * 2 + 1 <= sz && a[u * 2 + 1] > a[t]) t = 2 * u + 1;
+    if (u != t)
     {
-        for (int i = 1; i <= n; i++)
-        {
-            if (a[i][j])
-                a[i] |= a[j]; // 好好体会位运算
-        }
+        swap(a[u], a[t]);
+        down(t, sz);
     }
-    for (int i = 1; i <= n; i++)
+}
+
+bool isHeap()
+{
+    memcpy(a, backup, sizeof a);
+    bool flag = false;
+    for (int sz = n; sz >= 2; sz -- )
     {
-        for (int j = 1; j <= n; j++)
+        if (sz == n)
         {
-            cout << a[i][j] + '0' << ' ';
+            for (int j = sz / 2; j; j -- )
+                down(j, sz);
         }
-        cout << endl;
+        else down(1, sz);
+        if (flag) return true;
+        if (equal(b + 1, b + n + 1, a + 1)) flag = true;
+        swap(a[1], a[sz]);
+    }
+    return false;
+}
+
+int main()
+{
+    ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
+
+    cin >> n;
+    for (int i = 1; i <= n; i ++ ) cin >> a[i];
+    for (int i = 1; i <= n; i ++ ) cin >> b[i];
+    memcpy(backup, a, sizeof a);
+    // if (isInsert()) cout << "Insertion Sort\n";
+    if (isHeap()) cout << "Heap Sort\n";
+
+    bool isfirst = true;
+    for (int i = 1; i <= n; i ++ )
+    {
+        if (isfirst) cout << a[i], isfirst = false;
+        else cout << ' ' << a[i];
     }
     return 0;
 }
\ No newline at end of file