diff --git a/.DS_Store b/.DS_Store index 2d2c3b3..3119428 100644 Binary files a/.DS_Store and b/.DS_Store differ diff --git a/FZOI/Tad 蛋糕.cpp b/FZOI/Tad 蛋糕.cpp new file mode 100644 index 0000000..231cfc1 --- /dev/null +++ b/FZOI/Tad 蛋糕.cpp @@ -0,0 +1,61 @@ +#include +#define int long long +#define endl "\n" +#define IMX LONG_LONG_MAX +#define IMN LONG_LONG_MIN +using namespace std; + +/* toothless. #17 */ + +const int N = 1e7 + 10; +const int M = 2e3 + 5; + +int n, m; +int a[1000000]; +int qzh[1000000]; +int st[1000000][25]; +int lg[1000000]; + +void init() +{ + lg[1] = 0; + for (int i = 2; i <= n; i++) + { + lg[i] = lg[i / 2] + 1; + } + for (int i = 1; i <= n; i++) + { + st[i][0] = qzh[i]; + } + for (int k = 1; k <= lg[m]; k++) + { + for (int i = 1; i <= n; i++) + { + st[i][k] = max(st[i][k - 1], st[min(i + (1 << (k - 1)), n)][k - 1]); + } + } +} +int quary(int l, int r) +{ + int k = lg[r - l + 1]; + return max(st[l][k], st[r - (1 << k) + 1][k]); +} +signed main() +{ + cin >> n >> m; + for (int i = 1; i <= n; i++) + { + cin >> a[i]; + qzh[i] = qzh[i - 1] + a[i]; + } + int maxl = -INT_MIN; + init(); + for (int i = 1; i <= n; i++) + { + int r = min(i + m - 1, n); + int s = quary(i, r) - qzh[i - 1]; + maxl = max(maxl, s); + } + cout << maxl; + return 0; +} \ No newline at end of file diff --git a/FZOI/The kth great number(对顶堆).cpp b/FZOI/The kth great number(对顶堆).cpp new file mode 100644 index 0000000..aea67d5 --- /dev/null +++ b/FZOI/The kth great number(对顶堆).cpp @@ -0,0 +1,34 @@ +#include +#define int long long +using namespace std; + +signed main() +{ + int n, k; + cin >> n >> k; + + vector nums; + + for (int i = 0; i < n; ++i) + { + char op; + cin >> op; + + if (op == 'I') + { + int num; + cin >> num; + nums.push_back(num); + } + else if (op == 'Q') + { + if (nums.size() >= k) + { + sort(nums.begin(), nums.end(), greater()); + cout << nums[k - 1] << endl; + } + } + } + + return 0; +} \ No newline at end of file diff --git a/FZOI/grouping.cpp b/FZOI/grouping.cpp new file mode 100644 index 0000000..c69c706 --- /dev/null +++ b/FZOI/grouping.cpp @@ -0,0 +1,40 @@ +#include +#define int long long +#define endl "\n" +#define IMX LONG_LONG_MAX +#define IMN LONG_LONG_MIN +using namespace std; + +/* toothless. #17 */ + +const int N = 1e7 + 10; +const int M = 2e3 + 5; + +int n, mn = IMX; +int a[N]; + +signed main() +{ + cin >> n; + for (int i = 0; i < n; i ++ ) + { + cin >> a[i]; + } + sort(a, a + n); + int cnt = 1; + for (int i = 1; i < n; i ++ ) + { + if (a[i] == a[i - 1] + 1) + { + cnt ++ ; + } + else + { + mn = min(mn, cnt); + cnt = 1; + } + } + mn = min(mn, cnt); + cout << mn << endl; + return 0; +} \ No newline at end of file diff --git a/FZOI/「NOIP2001」数的划分.cpp b/FZOI/「NOIP2001」数的划分.cpp new file mode 100644 index 0000000..448d714 --- /dev/null +++ b/FZOI/「NOIP2001」数的划分.cpp @@ -0,0 +1,24 @@ +#include +#define int long long +using namespace std; + +int n, k, cnt; + +void dfs(int last, int sum, int cur) +{ + if (cur == k) + { + if (sum == n) + cnt++; + return; + } + for (int i = last; sum + i * (k - cur) <= n; i++) + dfs(i, sum + i, cur + 1); +} + +signed main() +{ + cin >> n >> k ; + dfs(1, 0, 0); + cout << cnt ; +} \ No newline at end of file diff --git a/FZOI/「NOIP2004」合并果子.cpp b/FZOI/「NOIP2004」合并果子.cpp new file mode 100644 index 0000000..f30e077 --- /dev/null +++ b/FZOI/「NOIP2004」合并果子.cpp @@ -0,0 +1,59 @@ +#include +#define int long long +#define endl "\n" +using namespace std; + +const int N = 20005; + +int n, heap[N], size; + +void put(int t) +{ + int p1, p2; + heap[ ++ size] = t; + p1 = size; + while(p1 > 1) + { + p2 = p1 / 2; + if(heap[p1] >= heap[p2]) return ; + swap(heap[p1], heap[p2]); + p1 = p2; + } +} + +int out() +{ + int p = 1, g, ans; + ans = heap[1]; + heap[1] = heap[size -- ]; + while(p * 2 <= size) + { + g = p * 2; + if(g < size && heap[g + 1] < heap[g]) g ++ ; + if(heap[p] <= heap[g]) return ans; + swap(heap[p], heap[g]); + p = g; + } + return ans; +} + +signed main() +{ + int ans = 0; + cin >> n ; + for(int i = 1; i <= n; ++ i) + { + int q; + cin >> q ; + put(q); + } + size = n; + while(size > 1) + { + int x = out(), y = out(); + ans += (x + y); + put(x + y); + } + cout << ans ; + return 0; +} \ No newline at end of file diff --git a/FZOI/全排列问题.cpp b/FZOI/全排列问题.cpp new file mode 100644 index 0000000..07ba3eb --- /dev/null +++ b/FZOI/全排列问题.cpp @@ -0,0 +1,29 @@ +#include +#define int long long +using namespace std; + +int n, arr[1030], ans[10]; +void dfs(int i, int s) +{ + if (i > n) + { + for (int p = 1; p <= n; p++) printf("%5d", ans[p]); + cout << endl; + return; + } + for (int j = s; j > 0; j -= j & (-j)) + { + int temp = j & (-j); + ans[i] = arr[temp]; + dfs(i + 1, s - temp); + } +} +signed main() +{ + cin >> n; + arr[1] = 1; + for (int i = 2; i <= n; i++) + arr[1 << (i - 1)] = i; + dfs(1, (1 << n) - 1); + return 0; +} diff --git a/FZOI/分身术.cpp b/FZOI/分身术.cpp new file mode 100644 index 0000000..2ce02a6 --- /dev/null +++ b/FZOI/分身术.cpp @@ -0,0 +1,56 @@ +#include +#define int long long +#define endl "\n" +#define fp(_a, _b, _c, _d) for (int _a = _b; _a <= _c; _a += _d) +#define fm(_a, _b, _c, _d) for (int _a = _b; _a <= _c; _a -= _d) +#define fin(_a, _b) for (int ss = 1; ss <= _a; ss++) cin >> _b[ss]; +#define fout(_a, _b, _c) for (int ss = 1; ss <= _a; ss++) cout << _b[ss] << _c; +using namespace std; + +/* + @author: BunDragon126 + @link: https://www.setbun.com/ +*/ + +const int N = 1e7 + 10; +const int M = 2e3 + 5; + +int n, k; +vector a(N), b(N); +deque deque1, deque2; + +signed main() +{ + "toothless. #17"; + cin >> n >> k; + for (int i = 0; i < n; ++ i) cin >> a[i]; + for (int i = 0; i < n; ++ i) cin >> b[i]; + int mn = n + 1; + for (int l = 0, r = 0; r < n; ++ r) + { + while (!deque1.empty() && a[deque1.back()] >= a[r]) + deque1.pop_back(); + deque1.push_back(r); + while (!deque2.empty() && b[deque2.back()] <= b[r]) + deque2.pop_back(); + deque2.push_back(r); + while (a[deque1.front()] <= k && b[deque2.front()] > a[deque1.front()]) + { + mn = min(mn, r - l + 1); + if (deque1.front() == l) + deque1.pop_front(); + if (deque2.front() == l) + deque2.pop_front(); + ++ l; + } + } + if (mn == n + 1) + { + cout << "So Sad!" << endl; + } + else + { + cout << mn << endl; + } + return 0; +} \ No newline at end of file diff --git a/FZOI/四色问题.cpp b/FZOI/四色问题.cpp new file mode 100644 index 0000000..0503fa4 --- /dev/null +++ b/FZOI/四色问题.cpp @@ -0,0 +1,56 @@ +#include +#include +using namespace std; + +const int MAXN = 8; +int n; +int arr[MAXN][MAXN]; +int color[MAXN]; +int ans; + +bool f(int v, int c) +{ + for (int i = 0; i < n; i++) + { + if (arr[v][i] && color[i] == c) + { + return false; + } + } + return true; +} + +void dfs(int v) +{ + if (v == n) + { + ans++; + return; + } + for (int c = 1; c <= 4; c++) + { + if (f(v, c)) + { + color[v] = c; + dfs(v + 1); + color[v] = 0; + } + } +} + +int main() +{ + cin >> n; + for (int i = 0; i < n; i++) + { + for (int j = 0; j < n; j++) + { + cin >> arr[i][j]; + } + } + memset(color, 0, sizeof(color)); + ans = 0; + dfs(0); + cout << ans << endl; + return 0; +} \ No newline at end of file diff --git a/FZOI/堆【模板题】.cpp b/FZOI/堆【模板题】.cpp new file mode 100644 index 0000000..e61601e --- /dev/null +++ b/FZOI/堆【模板题】.cpp @@ -0,0 +1,57 @@ +#include +#define int long long +#define endl "\n" +using namespace std; + +const int N = 20005; + +int n, heap[N], size; + +void add(int t) +{ + int p1, p2; + heap[ ++ size] = t; + p1 = size; + while(p1 > 1) + { + p2 = p1 / 2; + if(heap[p1] >= heap[p2]) return ; + swap(heap[p1], heap[p2]); + p1 = p2; + } +} + +void out() +{ + int p = 1, g, ans; + ans = heap[1]; + heap[1] = heap[size -- ]; + while(p * 2 <= size) + { + g = p * 2; + if(g < size && heap[g + 1] < heap[g]) g ++ ; + if(heap[p] <= heap[g]) return ; + swap(heap[p], heap[g]); + p = g; + } +} + +signed main() +{ + int ans = 0; + cin >> n ; + for(int i = 1; i <= n; ++ i) + { + int q; + cin >> q ; + if(q == 1) + { + int x; + cin >> x; + add(x); + } + else if(q == 2) cout << heap[1] << endl ; + else out(); + } + return 0; +} \ No newline at end of file diff --git a/FZOI/奇怪的电梯.cpp b/FZOI/奇怪的电梯.cpp new file mode 100644 index 0000000..41a5ccf --- /dev/null +++ b/FZOI/奇怪的电梯.cpp @@ -0,0 +1,28 @@ +#include +#define int long long +using namespace std; + +int n, a, b, k[201], dis[201]; + +void dfs(int node, int step) +{ + dis[node] = step; + int v = node - k[node]; + if (1 <= v && step + 1 < dis[v]) + dfs(v, step + 1); + v = node + k[node]; + if (v <= n && step + 1 < dis[v]) + dfs(v, step + 1); + return; +} +signed main() +{ + memset(dis, 0x3f, sizeof(dis)); + cin >> n >> a >> b; + for (int i = 1; i <= n; i++) + cin >> k[i]; + dfs(a, 0); + if(dis[b] == 0x3f3f3f3f3f3f3f3f) cout << -1 << endl; + else cout << dis[b] << endl; + return 0; +} \ No newline at end of file diff --git a/FZOI/工作分配问题.cpp b/FZOI/工作分配问题.cpp new file mode 100644 index 0000000..b425cbb --- /dev/null +++ b/FZOI/工作分配问题.cpp @@ -0,0 +1,36 @@ +#include +#define int long long +using namespace std; + +const int MAXN = 20; +int n; +int arr[MAXN][MAXN]; +int mn; +void f() +{ + mn = INT_MAX; + for (int i = 0; i < n; i++) + { + int cnt = 0; + for (int j = 0; j < n; j++) + { + cnt += arr[j][i]; + } + mn = min(mn, cnt); + } +} + +signed main() +{ + cin >> n; + for (int i = 0; i < n; i++) + { + for (int j = 0; j < n; j++) + { + cin >> arr[i][j]; + } + } + f(); + cout << mn << endl; + return 0; +} \ No newline at end of file diff --git a/FZOI/括号匹配.cpp b/FZOI/括号匹配.cpp new file mode 100644 index 0000000..d962695 --- /dev/null +++ b/FZOI/括号匹配.cpp @@ -0,0 +1,50 @@ +#include +#define int long long +using namespace std; + +bool f(const string &s) +{ + stack st; + + for (char c : s) + { + if (c == '(' || c == '[') + { + st.push(c); + } + else if (c == ')' || c == ']') + { + if (st.empty()) + { + return false; + } + + char top = st.top(); + st.pop(); + + if ((c == ')' && top != '(') || (c == ']' && top != '[')) + { + return false; + } + } + } + + return st.empty(); +} + +signed main() +{ + string s; + cin >> s; + + if (f(s)) + { + cout << "OK" << endl; + } + else + { + cout << "Wrong" << endl; + } + + return 0; +} \ No newline at end of file diff --git a/FZOI/数位递增数.cpp b/FZOI/数位递增数.cpp new file mode 100644 index 0000000..ba1a452 --- /dev/null +++ b/FZOI/数位递增数.cpp @@ -0,0 +1,30 @@ +#include +#include + +using namespace std; + +bool isIncreasingNumber(int n) { + string num = to_string(n); + for (int i = 0; i < num.size() - 1; i++) { + if (num[i] > num[i + 1]) { + return false; + } + } + return true; +} + +int main() { + int n; + cin >> n; + + int count = 0; + for (int i = 10; i <= n; i++) { + if (isIncreasingNumber(i)) { + count++; + } + } + + cout << count << endl; + + return 0; +} diff --git a/FZOI/最少步数.cpp b/FZOI/最少步数.cpp new file mode 100644 index 0000000..b058f73 --- /dev/null +++ b/FZOI/最少步数.cpp @@ -0,0 +1,63 @@ +#include +#define int long long +using namespace std; + +const int N = 100; +const vector> knight_moves = {{2, 1}, {2, -1}, {-2, 1}, {-2, -1}, {1, 2}, {1, -2}, {-1, 2}, {-1, -2}}; +const vector> elephant_moves = {{2, 2}, {2, -2}, {-2, 2}, {-2, -2}}; + +int bfs(int start_x, int start_y) +{ + vector> vis(N + 1, vector(N + 1, false)); + queue> que; + que.push({start_x, start_y, 0}); + vis[start_x][start_y] = true; + + while (!que.empty()) + { + auto [x, y, steps] = que.front(); + que.pop(); + + if (x == 1 && y == 1) + { + return steps; + } + for (const auto &move : knight_moves) + { + int nx = x + move.first; + int ny = y + move.second; + if (nx >= 1 && nx <= N && ny >= 1 && ny <= N && !vis[nx][ny]) + { + vis[nx][ny] = true; + que.push({nx, ny, steps + 1}); + } + } + + for (const auto &move : elephant_moves) + { + int nx = x + move.first; + int ny = y + move.second; + if (nx >= 1 && nx <= N && ny >= 1 && ny <= N && !vis[nx][ny]) + { + vis[nx][ny] = true; + que.push({nx, ny, steps + 1}); + } + } + } + + return -1; +} + +signed main() +{ + int x, y, x1, y1; + cin >> x >> y >> x1 >> y1; + + int steps_A = bfs(x, y); + int steps_B = bfs(x1, y1); + + cout << steps_A << endl; + cout << steps_B << endl; + + return 0; +} \ No newline at end of file diff --git a/FZOI/素数环.cpp b/FZOI/素数环.cpp new file mode 100644 index 0000000..482708e --- /dev/null +++ b/FZOI/素数环.cpp @@ -0,0 +1,80 @@ +#include +using namespace std; + +const int MAXN = 16; +int n; +int arr[MAXN]; +bool vis[MAXN]; + +// 判断一个数是否为素数 +bool check(int num) +{ + if (num < 2) + { + return false; + } + for (int i = 2; i * i <= num; i++) + { + if (num % i == 0) + { + return false; + } + } + return true; +} + +// 回溯 +// cur表示当前位置 +void back(int cur) +{ + if (cur == n) // 检查最后一个数字和第一个数字之和是否为素数 + { + if (check(arr[0] + arr[n - 1])) + { + for (int i = 0; i < n; i++) + { + cout << arr[i] << " "; + } + cout << endl; + } + return; + } + for (int i = 2; i <= n; i++) + { + if (!vis[i] && check(arr[cur - 1] + i)) + { + vis[i] = true; + arr[cur] = i; + back(cur + 1); + vis[i] = false; + } + } +} + +int main() +{ + cin >> n; + memset(vis, false, sizeof(vis)); + vis[1] = true; + arr[0] = 1; + back(1); + return 0; +} + +/* +以下题解为后期由本人添加 +题目分析: +题目要求将整数 1 到 n 组成一个环,使得相邻的两个整数之和均为素数。 +我们需要找到所有满足条件的素数环,并按照要求输出。 +由于 n 的值不超过 16,我们可以使用回溯法来尝试所有可能的组合。 +代码解释: +check函数用于判断一个数是否为素数。 +back函数是回溯函数,用于尝试放置数字到环中。 +在back函数中,首先判断是否已经放置了 n 个数字。如果是,则检查最后一个数字和第一个数字之和是否为素数。如果是素数,则输出这个环。 +然后,从数字 2 到 n 依次尝试将每个数字放在环的下一个位置。如果该数字没有被使用过,并且与前一个数字之和为素数,就将其放在环的下一个位置,并标记为已使用,然后继续递归放置下一个数字。 +如果放置过程中出现相邻两个数字之和不是素数的情况,就回溯到上一个数字,重新选择下一个数字。 +在main函数中,读取输入的 n,初始化标记数组和环的第一个数字,然后调用back函数从数字 1 开始回溯。 +时间复杂度和空间复杂度: +时间复杂度:由于需要尝试所有可能的组合,时间复杂度为 O(n!),其中 n 是输入的数字个数。 +空间复杂度:主要是使用了标记数组和环数组,空间复杂度为 O(n)。 +*/ \ No newline at end of file diff --git a/FZOI/装载问题.cpp b/FZOI/装载问题.cpp new file mode 100644 index 0000000..99278c1 --- /dev/null +++ b/FZOI/装载问题.cpp @@ -0,0 +1,43 @@ +#include +#include +using namespace std; + +const int MAXN = 45; +const int MAXC = 2000; +int n, c; +int arr[MAXN]; +int dp[MAXN][MAXC]; + +int f() +{ + for (int i = 0; i <= c; i++) + { + dp[0][i] = 0; + } + for (int i = 1; i <= n; i++) + { + for (int j = 0; j <= c; j++) + { + if (arr[i - 1] > j) + { + dp[i][j] = dp[i - 1][j]; + } + else + { + dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - arr[i - 1]] + arr[i - 1]); + } + } + } + return dp[n][c]; +} + +int main() +{ + cin >> n >> c; + for (int i = 0; i < n; i++) + { + cin >> arr[i]; + } + cout << f() << endl; + return 0; +} \ No newline at end of file diff --git a/FZOI/非常可乐.cpp b/FZOI/非常可乐.cpp new file mode 100644 index 0000000..390cdaf --- /dev/null +++ b/FZOI/非常可乐.cpp @@ -0,0 +1,128 @@ +#include +using namespace std; + +struct Node +{ + int s, n, m, step; +}; + +bool vis[101][101][101]; + +int bfs(int s, int n, int m) +{ + queue que; + Node init = {s, 0, 0, 0}; + que.push(init); + vis[s][0][0] = true; + + while (!que.empty()) + { + Node cur = que.front(); + que.pop(); + + if (cur.s == cur.n && cur.s == cur.m) + { + return cur.step; + } + + if (cur.s > 0 && cur.n < n) + { + int pour = min(cur.s, n - cur.n); + int newS = cur.s - pour; + int newN = cur.n + pour; + if (!vis[newS][newN][cur.m]) + { + vis[newS][newN][cur.m] = true; + Node next = {newS, newN, cur.m, cur.step + 1}; + que.push(next); + } + } + + if (cur.s > 0 && cur.m < m) + { + int pour = min(cur.s, m - cur.m); + int newS = cur.s - pour; + int newM = cur.m + pour; + if (!vis[newS][cur.n][newM]) + { + vis[newS][cur.n][newM] = true; + Node next = {newS, cur.n, newM, cur.step + 1}; + que.push(next); + } + } + + if (cur.n > 0 && cur.s < s) + { + int pour = min(cur.n, s - cur.s); + int newS = cur.s + pour; + int newN = cur.n - pour; + if (!vis[newS][newN][cur.m]) + { + vis[newS][newN][cur.m] = true; + Node next = {newS, newN, cur.m, cur.step + 1}; + que.push(next); + } + } + + if (cur.n > 0 && cur.m < m) + { + int pour = min(cur.n, m - cur.m); + int newN = cur.n - pour; + int newM = cur.m + pour; + if (!vis[cur.s][newN][newM]) + { + vis[cur.s][newN][newM] = true; + Node next = {cur.s, newN, newM, cur.step + 1}; + que.push(next); + } + } + + if (cur.m > 0 && cur.s < s) + { + int pour = min(cur.m, s - cur.s); + int newS = cur.s + pour; + int newM = cur.m - pour; + if (!vis[newS][cur.n][newM]) + { + vis[newS][cur.n][newM] = true; + Node next = {newS, cur.n, newM, cur.step + 1}; + que.push(next); + } + } + + if (cur.m > 0 && cur.n < n) + { + int pour = min(cur.m, n - cur.n); + int newM = cur.m - pour; + int newN = cur.n + pour; + if (!vis[cur.s][newN][newM]) + { + vis[cur.s][newN][newM] = true; + Node next = {cur.s, newN, newM, cur.step + 1}; + que.push(next); + } + } + } + + return -1; +} + +int main() +{ + int s, n, m; + while (cin >> s >> n >> m && !(s == 0 && n == 0 && m == 0)) + { + memset(vis, false, sizeof(vis)); + int res = bfs(s, n, m); + if (res == -1) + { + cout << "NO" << endl; + } + else + { + cout << res << endl; + } + } + + return 0; +} \ No newline at end of file diff --git a/洛谷刷题/.DS_Store b/Luogu/.DS_Store similarity index 100% rename from 洛谷刷题/.DS_Store rename to Luogu/.DS_Store diff --git a/Luogu/F_Chips_on_a_Line.cpp b/Luogu/F_Chips_on_a_Line.cpp new file mode 100644 index 0000000..e69de29 diff --git a/Luogu/Luogu_P_5715.cpp b/Luogu/Luogu_P_5715.cpp new file mode 100644 index 0000000..285dc70 --- /dev/null +++ b/Luogu/Luogu_P_5715.cpp @@ -0,0 +1,21 @@ +#include +#define int long long +#define endl "\n" +using namespace std; + +int arr[1005]; +int n; + +signed main() +{ + for(int i = 1; i <= 3; i ++ ) + { + cin >> arr[i]; + } + sort(arr + 1, arr + 1 + 3); + for(int i = 1; i <= 3; i ++ ) + { + cout << arr[i] << " "; + } + return 0; +} \ No newline at end of file diff --git a/Luogu/Luogu_P_9912.cpp b/Luogu/Luogu_P_9912.cpp new file mode 100644 index 0000000..527c6ec --- /dev/null +++ b/Luogu/Luogu_P_9912.cpp @@ -0,0 +1,119 @@ +#include +#define int long long +#define endl "\n" +using namespace std; + +const int N = 10001005; + +int n, q, l, r, mid, x, sum, ma, ans[N], o, cnt, cnt1; +map m; + +// 结构体node用于存储线段树的节点信息 +struct node { + int l, r, len, lc, rc; // l和r表示区间左右边界, len表示区间内岛屿的数量, lc和rc表示区间左右端点的类别 +} a[N]; + +// 结构体deno用于存储高度信息 +struct deno { + int x, id; // x表示高度, id表示高度对应的位置索引 +} c[N]; + +// 结构体w用于存储查询信息 +struct w { + int l, r, x, id; // l和r表示查询区间, x表示海平面上升高度, id表示查询的索引 +} b[N]; + +// 更新节点信息函数 +void gx(int p) { + a[p].len = a[p * 2].len + a[p * 2 + 1].len; // 合并左右子区间的岛屿数量 + if (a[p * 2].rc == a[p * 2 + 1].lc) // 如果左右子区间的连接处高度相同,岛屿数量减少1 + a[p].len--; + a[p].lc = a[p * 2].lc; // 更新左端点类别 + a[p].rc = a[p * 2 + 1].rc; // 更新右端点类别 +} + +// 构建线段树函数 +void build(int p, int l, int r) { + a[p].l = l; + a[p].r = r; + if (l == r) { // 叶子节点 + a[p].len = a[p].lc = a[p].rc = 1; // 初始化为长度为1的岛屿 + return; + } + int mid = (l + r) / 2; + build(p * 2, l, mid); // 递归构建左子树 + build(p * 2 + 1, mid + 1, r); // 递归构建右子树 + gx(p); // 更新当前节点信息 +} + +// 更新线段树函数 +void change(int p, int l, int r) { + if (l <= a[p].l && a[p].r <= r) { // 完全覆盖 + a[p].len = 0; // 将区间内的长度设置为0 + a[p].lc = a[p].rc = ++cnt1; // 更新类别 + return; + } + int mid = (a[p].l + a[p].r) / 2; + if (l <= mid) change(p * 2, l, r); // 递归更新左子树 + if (mid < r) change(p * 2 + 1, l, r); // 递归更新右子树 + gx(p); // 更新当前节点信息 +} + +// 查询线段树函数 +int ask(int p, int l, int r) { + if (l <= a[p].l && a[p].r <= r) // 完全覆盖 + return a[p].len; + int mid = (a[p].l + a[p].r) / 2; + int ans1 = 0; + if (l <= mid && mid < r) { // 跨区间查询 + ans1 = ask(p * 2, l, r) + ask(p * 2 + 1, l, r); // 查询左右子树 + if (a[p * 2].rc == a[p * 2 + 1].lc) // 如果左右子树的连接处高度相同,岛屿数量减少1 + ans1--; + } else if (l <= mid) { // 查询左子区间 + ans1 = ask(p * 2, l, r); + } else if (mid < r) { // 查询右子区间 + ans1 = ask(p * 2 + 1, l, r); + } + return ans1; +} + +// 比较函数,用于排序查询 +bool cmp(w x, w y) { + return x.x < y.x; +} + +// 比较函数,用于排序高度 +bool cmp1(deno x, deno y) { + return x.x < y.x; +} + +signed main() { + ios::sync_with_stdio(0); + cin.tie(0); + cout.tie(0); + cin >> n >> q; + cnt1 = n + 1; + for (int i = 1; i <= n; i++) { + cin >> c[i].x; + c[i].id = i; + } + sort(c + 1, c + 1 + n, cmp1); // 按高度排序 + for (int i = 1; i <= q; i++) { + cin >> b[i].l >> b[i].r >> b[i].x; + b[i].id = i; + } + sort(b + 1, b + 1 + q, cmp); // 按海平面上升高度排序 + cnt = 1; + build(1, 1, n); // 构建线段树 + for (int i = 1; i <= q; i++) { + while (cnt <= n && c[cnt].x <= b[i].x) { + change(1, c[cnt].id, c[cnt].id); // 更新线段树 + cnt++; + } + ans[b[i].id] = ask(1, b[i].l, b[i].r); // 查询岛屿数量 + } + for (int i = 1; i <= q; i++) { + cout << ans[i] << endl; // 输出结果 + } + return 0; +} \ No newline at end of file diff --git a/Luogu/Luogu_U_458232.cpp b/Luogu/Luogu_U_458232.cpp new file mode 100644 index 0000000..dd644c9 --- /dev/null +++ b/Luogu/Luogu_U_458232.cpp @@ -0,0 +1,53 @@ +#include +#define int long long +#define endl "\n" +#define fp(_a, _b, _c, _d) for(int _a = _b; _a <= _c; _a += _d) +#define fm(_a, _b, _c, _d) for(int _a = _b; _a <= _c; _a -= _d) +#define fin(_a, _b) for(int ss = 1; ss <= _a; ss ++ ) cin >> _b[ss]; +#define fout(_a, _b, _c) for(int ss = 1; ss <= _a; ss ++ ) cout << _b[ss] << _c ; +using namespace std; + +/* + 2024/08/03 + Code-From-BunDragon126-At-VisualStudioCode + AlignPixel*GiveSupport + https://frederication.work/ +*/ + +const int N = 1e7 + 10; +const int M = 2e3 + 5; + +int t, n, m, k, a[N]; + +signed main() +{ + "toothless. #17"; + cin >> t; + fp(ss, 1, t, 1) + { + cin >> n >> m ; + if(m % 2 == 0) // 如果 m 是偶数且 n 为 0 ,输出 Yes。否则输出 No。 + { + if(n == 0) + { + cout << "Yes" << endl; + } + else + { + cout << "No" << endl; + } + } + else // 如果 m 是奇数且 n 为正整数,输出 Yes 。否则输出 No。 + { + if(n > 0) + { + cout << "Yes" << endl; + } + else + { + cout << "No" << endl; + } + } + } + return 0; +} \ No newline at end of file diff --git a/Luogu/Luogu_U_458233.cpp b/Luogu/Luogu_U_458233.cpp new file mode 100644 index 0000000..7a773a3 --- /dev/null +++ b/Luogu/Luogu_U_458233.cpp @@ -0,0 +1,58 @@ +#include +#define int long long +#define endl "\n" +#define fp(_a, _b, _c, _d) for(int _a = _b; _a <= _c; _a += _d) +#define fm(_a, _b, _c, _d) for(int _a = _b; _a <= _c; _a -= _d) +#define fin(_a, _b) for(int ss = 1; ss <= _a; ss ++ ) cin >> _b[ss]; +#define fout(_a, _b, _c) for(int ss = 1; ss <= _a; ss ++ ) cout << _b[ss] << _c ; +using namespace std; + +/* + + Code-From-BunDragon126-At-VisualStudioCode + AlignPixel*GetSupport + https://frederication.work/ +*/ + +const int N = 1e7 + 10; +const int M = 2e3 + 5; + +signed main() +{ + "toothless. #17"; + int n, m; + cin >> n >> m; + string a, b; + cin >> a >> b; + char current_char = 'a'; + fp(i, 0, m - 1, 1) + { + bool found = false; + fp(j, 0, n - 1, 1) + { + if (a[j] == '#') + { + a[j] = current_char; + found = true; + break; + } + } + if (!found) { + fp(j, 0, n - 1, 1) + { + if (b[j] == '#') + { + b[j] = current_char; + break; + } + } + } + current_char ++ ; + if(current_char > 'z') + { + current_char = 'a'; + } + } + cout << a << endl; + return 0; +} \ No newline at end of file diff --git a/Luogu/Luogu_U_458234.cpp b/Luogu/Luogu_U_458234.cpp new file mode 100644 index 0000000..ca92f54 --- /dev/null +++ b/Luogu/Luogu_U_458234.cpp @@ -0,0 +1,64 @@ +#include +#define int long long +#define endl "\n" +#define fp(_a, _b, _c, _d) for(int _a = _b; _a <= _c; _a += _d) +#define fm(_a, _b, _c, _d) for(int _a = _b; _a <= _c; _a -= _d) +#define fin(_a, _b) for(int ss = 1; ss <= _a; ss ++ ) cin >> _b[ss]; +#define fout(_a, _b, _c) for(int ss = 1; ss <= _a; ss ++ ) cout << _b[ss] << _c ; +using namespace std; + +/* + 2024/08/03 + Code-From-BunDragon126-At-VisualStudioCode + AlignPixel*GetSupport + https://frederication.work/ +*/ + +const int N = 1e7 + 10; +const int M = 2e3 + 5; + +int calculateUniqueBloomTimes(vector& times, int m, int offset = 0) { + unordered_map bloomCount; + for (int t : times) { + for (int i = 0; i < m; ++i) { + bloomCount[t + offset + i]++; + } + } + + int uniqueBloomTimes = 0; + for (auto& entry : bloomCount) { + if (entry.second == 1) { + uniqueBloomTimes++; + } + } + + return uniqueBloomTimes; +} + +signed main() +{ + "toothless. #17"; + int n, m; + cin >> n >> m; + + vector t(n); + for (int i = 0; i < n; ++i) { + cin >> t[i]; + } + + int maxUniqueBloomTimes = calculateUniqueBloomTimes(t, m); + + for (int i = 0; i < n; ++i) { + int originalTime = t[i]; + for (int newTime = 1; newTime <= *max_element(t.begin(), t.end()) + m; ++newTime) { + if (newTime != originalTime) { + t[i] = newTime; + maxUniqueBloomTimes = max(maxUniqueBloomTimes, calculateUniqueBloomTimes(t, m)); + } + } + t[i] = originalTime; + } + + cout << maxUniqueBloomTimes << endl; + return 0; +} \ No newline at end of file diff --git a/洛谷刷题/P1005 [NOIP2007 提高组] 矩阵取数游戏.cpp b/Luogu/P1005 [NOIP2007 提高组] 矩阵取数游戏.cpp similarity index 100% rename from 洛谷刷题/P1005 [NOIP2007 提高组] 矩阵取数游戏.cpp rename to Luogu/P1005 [NOIP2007 提高组] 矩阵取数游戏.cpp diff --git a/Luogu/P_1001_A_B_Problem.cpp b/Luogu/P_1001_A_B_Problem.cpp new file mode 100644 index 0000000..b06d0bd --- /dev/null +++ b/Luogu/P_1001_A_B_Problem.cpp @@ -0,0 +1,26 @@ +#include +#define int long long +#define endl "\n" +#define fp(_a, _b, _c, _d) for(int _a = _b; _a <= _c; _a += _d) +#define fm(_a, _b, _c, _d) for(int _a = _b; _a <= _c; _a -= _d) +#define fin(_a, _b) for(int ss = 1; ss <= _a; ss ++ ) cin >> _b[ss]; +#define fout(_a, _b, _c) for(int ss = 1; ss <= _a; ss ++ ) cout << _b[ss] << _c ; +using namespace std; + +/* + @author: BunDragon126 + @link: https://www.setbun.com/ +*/ + +const int N = 1e7 + 10; +const int M = 2e3 + 5; + +int a, b; + +signed main() +{ + "toothless. #17"; + cin >> a >> b ; + cout << a + b ; + return 0; +} \ No newline at end of file diff --git a/Luogu/P_1002_NOIP_2002_普及组_过河卒.cpp b/Luogu/P_1002_NOIP_2002_普及组_过河卒.cpp new file mode 100644 index 0000000..e69de29 diff --git a/Luogu/P_1085_NOIP_2004_普及组_不高兴的津津.cpp b/Luogu/P_1085_NOIP_2004_普及组_不高兴的津津.cpp new file mode 100644 index 0000000..e52d3ea --- /dev/null +++ b/Luogu/P_1085_NOIP_2004_普及组_不高兴的津津.cpp @@ -0,0 +1,22 @@ +#include +#define int long long +#define endl "\n" +#define fp(_a, _b, _c, _d) for(int _a = _b; _a <= _c; _a += _d) +#define fm(_a, _b, _c, _d) for(int _a = _b; _a <= _c; _a -= _d) +#define fin(_a, _b) for(int ss = 1; ss <= _a; ss ++ ) cin >> _b[ss]; +#define fout(_a, _b, _c) for(int ss = 1; ss <= _a; ss ++ ) cout << _b[ss] << _c ; +using namespace std; + +signed main() +{ + int a, b, s, max = 0, i, day = 0; + fp(i, 1, 8, 1) + { + cin >> a >> b; + s = a + b; + if ((s > max) && (s > 8)) + max = s, day = i; + } + cout << day; + return 0; +} \ No newline at end of file diff --git a/Luogu/P_1089_NOIP_2004_提高组_津津的储蓄计划.cpp b/Luogu/P_1089_NOIP_2004_提高组_津津的储蓄计划.cpp new file mode 100644 index 0000000..20c70eb --- /dev/null +++ b/Luogu/P_1089_NOIP_2004_提高组_津津的储蓄计划.cpp @@ -0,0 +1,46 @@ +#include +#define int long long +#define endl "\n" +#define fp(_a, _b, _c, _d) for (int _a = _b; _a <= _c; _a += _d) +#define fm(_a, _b, _c, _d) for (int _a = _b; _a <= _c; _a -= _d) +#define fin(_a, _b) for (int ss = 1; ss <= _a; ss++) cin >> _b[ss]; +#define fout(_a, _b, _c) for (int ss = 1; ss <= _a; ss++) cout << _b[ss] << _c; +using namespace std; + +/* + @author: BunDragon126 + @link: https://www.setbun.com/ +*/ + +const int N = 1e7 + 10; +const int M = 2e3 + 5; + +int arr[N]; + +signed main() +{ + "toothless. #17"; + for (int i = 0; i < 12; i++) + { + cin >> arr[i]; + } + + int money = 0; + int l = 0; + + for (int i = 0; i < 12; i++) + { + money += 300; + if (money < arr[i]) + { + cout << -(i + 1) << endl; + return 0; + } + money -= arr[i]; + int c = (money / 100) * 100; + l += c; + money -= c; + } + cout << money + l * 1.2 << endl; + return 0; +} diff --git a/Luogu/P_1150_Peter_的烟.cpp b/Luogu/P_1150_Peter_的烟.cpp new file mode 100644 index 0000000..309b44a --- /dev/null +++ b/Luogu/P_1150_Peter_的烟.cpp @@ -0,0 +1,32 @@ +#include +#define int long long +#define endl "\n" +#define fp(_a, _b, _c, _d) for(int _a = _b; _a <= _c; _a += _d) +#define fm(_a, _b, _c, _d) for(int _a = _b; _a <= _c; _a -= _d) +#define fin(_a, _b) for(int ss = 1; ss <= _a; ss ++ ) cin >> _b[ss]; +#define fout(_a, _b, _c) for(int ss = 1; ss <= _a; ss ++ ) cout << _b[ss] << _c ; +using namespace std; + +/* + @author: BunDragon126 + @link: https://www.setbun.com/ +*/ + +const int N = 1e7 + 10; +const int M = 2e3 + 5; + +int n, k; + +signed main() +{ + "toothless. #17"; + cin >> n >> k ; + int cnt = 0; + fp(i, 1, n, 1) + { + cnt ++ ; + if(cnt % k == 0) n ++ ; + } + cout << cnt ; + return 0; +} \ No newline at end of file diff --git a/Luogu/P_1739_表达式括号匹配.cpp b/Luogu/P_1739_表达式括号匹配.cpp new file mode 100644 index 0000000..e69de29 diff --git a/Luogu/U_458233_FLA_I_歌静河.cpp b/Luogu/U_458233_FLA_I_歌静河.cpp new file mode 100644 index 0000000..e69de29 diff --git a/Luogu/[COCI 2023:2024 #2] Zatopljenje.cpp b/Luogu/[COCI 2023:2024 #2] Zatopljenje.cpp new file mode 100644 index 0000000..4f9caad --- /dev/null +++ b/Luogu/[COCI 2023:2024 #2] Zatopljenje.cpp @@ -0,0 +1,54 @@ +#include +#define int long long +#define endl '\n' +using namespace std; + +const int N = 10005; + +vector h(N); + +int f(const vector &h, int l, int r, int x) +{ + int cnt = 0; + int start = l - 1; + bool flag = false; + + for (int i = start; i < r; ++i) + { + if (h[i] > x) + { + if (!flag) + { + cnt++; + flag = true; + } + } + else + { + flag = false; + } + } + return cnt; +} + +signed main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + cout.tie(0); + int n, q; + cin >> n >> q; + for (int i = 0; i < n; ++i) + { + cin >> h[i]; + } + + for (int i = 0; i < q; ++i) + { + int l, r, x; + cin >> l >> r >> x; + cout << f(h, l, r, x) << endl ; + } + + return 0; +} \ No newline at end of file diff --git a/Luogu/小鱼的航程.cpp b/Luogu/小鱼的航程.cpp new file mode 100644 index 0000000..bc82f0c --- /dev/null +++ b/Luogu/小鱼的航程.cpp @@ -0,0 +1,10 @@ +#include +using namespace std; +int main(){ + long long x, n; + cin >> x >> n; + long long week; + week = (x + n) / 7 * 2; + cout << (n - week) * 250; + return 0; +} \ No newline at end of file diff --git a/Luogu/数字三角形2.cpp b/Luogu/数字三角形2.cpp new file mode 100644 index 0000000..1dada6d --- /dev/null +++ b/Luogu/数字三角形2.cpp @@ -0,0 +1,36 @@ +#include +#define int long long +#define endl "\n" +using namespace std; + +const int N = 2e3 + 10; + +int mp[N][N], d[N][N]; + +signed main() +{ + int n; + cin >> n ; + for(int i = 1; i <= n; ++ i) + { + for(int j = 1; j <= i; ++ j) + { + cin >> mp[i][j] ; + } + } + for(int i = 1; i <= n; ++ i) + { + for(int j = 1; j <= i; ++ j) + { + d[i][j] = max(d[i - 1][j - 1] + mp[i][j] % 100, d[i - 1][j] + mp[i][j] % 100); + } + } + int ans = 0; + for(int i = 1; i <= n; ++ i) + { + // cout << d[n][i] << " " ; + ans = max(ans, d[n][i] % 100); + } + cout << ans ; + return 0; +} \ No newline at end of file diff --git a/Luogu/素数环【洛谷版UVa】.cpp b/Luogu/素数环【洛谷版UVa】.cpp new file mode 100644 index 0000000..55f8f7e --- /dev/null +++ b/Luogu/素数环【洛谷版UVa】.cpp @@ -0,0 +1,85 @@ +#include +using namespace std; + +const int MAXN = 16; +int n; +int arr[MAXN]; +bool vis[MAXN]; + +// 判断一个数是否为素数 +bool check(int num) +{ + if (num < 2) + { + return false; + } + for (int i = 2; i * i <= num; i++) + { + if (num % i == 0) + { + return false; + } + } + return true; +} + +// 回溯 +// cur表示当前位置 +void back(int cur) +{ + if (cur == n) // 检查最后一个数字和第一个数字之和是否为素数 + { + if (check(arr[0] + arr[n - 1])) + { + for (int i = 0; i < n; i++) + { + cout << arr[i] << " "; + } + cout << endl; + } + return; + } + for (int i = 2; i <= n; i++) + { + if (!vis[i] && check(arr[cur - 1] + i)) + { + vis[i] = true; + arr[cur] = i; + back(cur + 1); + vis[i] = false; + } + } +} + +int main() +{ + int idx = 1; + while(n != EOF) + { + cin >> n; + cout << "Case " << idx++ << ":" << endl; + memset(vis, false, sizeof(vis)); + vis[1] = true; + arr[0] = 1; + back(1); + } + return 0; +} + +/* +以下题解为后期由本人添加 +题目分析: +题目要求将整数 1 到 n 组成一个环,使得相邻的两个整数之和均为素数。 +我们需要找到所有满足条件的素数环,并按照要求输出。 +由于 n 的值不超过 16,我们可以使用回溯法来尝试所有可能的组合。 +代码解释: +check函数用于判断一个数是否为素数。 +back函数是回溯函数,用于尝试放置数字到环中。 +在back函数中,首先判断是否已经放置了 n 个数字。如果是,则检查最后一个数字和第一个数字之和是否为素数。如果是素数,则输出这个环。 +然后,从数字 2 到 n 依次尝试将每个数字放在环的下一个位置。如果该数字没有被使用过,并且与前一个数字之和为素数,就将其放在环的下一个位置,并标记为已使用,然后继续递归放置下一个数字。 +如果放置过程中出现相邻两个数字之和不是素数的情况,就回溯到上一个数字,重新选择下一个数字。 +在main函数中,读取输入的 n,初始化标记数组和环的第一个数字,然后调用back函数从数字 1 开始回溯。 +时间复杂度和空间复杂度: +时间复杂度:由于需要尝试所有可能的组合,时间复杂度为 O(n!),其中 n 是输入的数字个数。 +空间复杂度:主要是使用了标记数组和环数组,空间复杂度为 O(n)。 +*/ \ No newline at end of file diff --git a/Luogu/螺旋矩阵.cpp b/Luogu/螺旋矩阵.cpp new file mode 100644 index 0000000..b9a7be8 --- /dev/null +++ b/Luogu/螺旋矩阵.cpp @@ -0,0 +1,48 @@ +#include +#define int long long +#define endl "\n" +using namespace std; +signed main() +{ + int n, x, y; + cin >> n >> x >> y ; + int rx = 1, num = y; // rx为模拟到的行数,num为目前加到的数,也是最终输出的结果 + int up = 1, down = n, zuo = 1, you = n; // x、y在下一次绕圈时上下左右的边界值(可以理解为将要被蛇形填数的一圈的边界) + bool fx = true; // 方向(true往下,false往上) + while (rx != x) // 一直模拟到刚好到达 + { + if (fx) // 特判 + { + if (you == y) // 如果是往下走直线 + { + num += x - rx; // 加上最后的数 + break; + } + } + else + { + if (zuo == y) // 如果是向上走直线 + { + num += rx - x; + break; + } + } + if (fx) // 普通情况,如果向下走 + { + num += (you - y + 1) * 2 + down - rx - 2; // 绕一圈 + rx = down; // 更新所在的行 + you -- ; // 右边界往回退一格 + up ++ ; // 上边界往回退一格 + } + else // 往上走 + { + num += (y - zuo + 1) * 2 + rx - up - 2; // 绕一圈 + rx = up; // 更新行 + zuo ++ ; // 左边界向内 + down -- ; // 下边界向内 + } + fx = !fx; // 方向调转 + } + cout << num ; + return 0; +} \ No newline at end of file diff --git a/XSM OJ 重庆小码王集团OJ/.DS_Store b/XSM OJ 重庆小码王集团OJ/.DS_Store index d5255b7..d353c8e 100644 Binary files a/XSM OJ 重庆小码王集团OJ/.DS_Store and b/XSM OJ 重庆小码王集团OJ/.DS_Store differ