跳到正文

个人记录

8月21日总结

Gold_Coin约 13 分钟
原文
文章信息与版本

文章信息

发布
2025年8月21日
收录
2025年12月12日
更新
2026年11月10日
篇幅
约 13 分钟

版本

  1. v1当前

P9123 Watching Mooloo B

  1. 时间复杂度:

    O(n)O(n)

  2. 正确思路:

    贪心,每个次查找看看是直接购买当天的便宜还是连着上一次购买便宜

  3. 正确代码:

C++
#include <bits/stdc++.h>
using namespace std;

const int N = 5e7 + 10;

long long n, k, a[N];
int main() {
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	cin >> n >> k;
	for (long long i = 1; i <= n; i++) {
		cin >> a[i];
	}
	long long ans = 1 + k;
	for (long long i = 2; i <= n; i++) {
		long long t1 = a[i] - a[i - 1];
		long long t2 = 1 + k;
		ans += min(t1, t2); 
	}
	cout << ans;
	return 0;
}

P1067 多项式输出

  1. 时间复杂度:

    O(n)O(n)

  2. 正确思路:

    根据题目给出的要求进行模拟即可

  3. 正确代码:

C++
#include <bits/stdc++.h>
using namespace std;

int n, a;

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	cin >> n;
	for (int i = n; i >= 0; i--) {
		cin >> a;
		if (a != 0) {
			if (i != n && a > 0) {
				cout << "+";
			}
			if (abs(a) > 1 || i == 0) {
				cout << a;
			} else if (a == -1) {
				cout << "-";
			}
			if (i > 1) {
				cout << "x^" << i;
			} 
			if (i == 1) {
				cout << "x";
			}
		}
	}
	return 0;
}

T656903 玩具谜题

  1. 时间复杂度:

    O(n(a+b)+n)O(n(a + b) + n)

  2. 正确思路:

    使用并查集,先按秩合并两个集合,然后在每次询问时只需要查找两玩具是否在同一连通分量即可

  3. 正确代码:

C++
#include <bits/stdc++.h>
using namespace std;

const int MAXN = 1005;

int fa[MAXN];
int a[MAXN];

int find(int x) {
    if (fa[x] != x) {
        fa[x] = find(fa[x]);
    }
    return fa[x];
}

void unionn(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y) return; // 已在同一集合中
    // 合并较小的集合到较大的集合
    if (a[x] < a[y]) {
        fa[x] = y;
        a[y] += a[x];
    } else {
        fa[y] = x;
        a[x] += a[y];
    }
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

    int n, m, q;
    cin >> n >> m >> q;

    for (int i = 1; i <= n; ++i) {
        fa[i] = i;
        a[i] = 1;
    }

    for (int k = 0; k < m; ++k) {
        vector<int> p(n + 1); 
        for (int i = 1; i <= n; ++i) {
            cin >> p[i];
        }
        // 合并i和p[i],因为它们可以相互到达
        for (int i = 1; i <= n; ++i) {
            unionn(i, p[i]);
        }
    }

    while (q--) {
        int a, b;
        cin >> a >> b;
        // 检查a和b是否在同一连通分块
        if (find(a) == find(b)) {
            cout << "DA\n";
        } else {
            cout << "NE\n";
        }
    }

    return 0;
}

T656906 小樱的魔法

  1. 时间复杂度:

    O(m(2∗r+1)2+2n2)O(m(2 * r + 1)^2 + 2n^2)

  2. 正确代码:

C++
#include <bits/stdc++.h>
using namespace std;

const int MAXN = 505;

int main() {
    int n, m;
    cin >> n >> m;
    
    // 填充
    int a[MAXN][MAXN];
    int num = 1;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            a[i][j] = num++;
        }
    }
    
    // 处理每个旋转操作
    for (int op = 0; op < m; op++) {
        int x, y, r, z;
        cin >> x >> y >> r >> z;
        
        x--; y--;
        
        int start1 = x - r;
        int start2 = y - r;
        int size = 2 * r + 1;
        
        // 创建子矩阵的副本,便于后续操作
        int b[MAXN][MAXN];
        for (int i = 0; i < size; i++) {
            for (int j = 0; j < size; j++) {
                b[i][j] = a[start1 + i][start2 + j];
            }
        }
        
        if (z == 0) {  // 顺时针旋转90度
            for (int i = 0; i < size; i++) {
                for (int j = 0; j < size; j++) {
                    a[start1 + i][start2 + j] = b[size - 1 - j][i];
                }
            }
        } else {  // 逆时针旋转90度
            for (int i = 0; i < size; i++) {
                for (int j = 0; j < size; j++) {
                    a[start1 + i][start2 + j] = b[j][size - 1 - i];
                }
            }
        }
    }
    
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            cout << a[i][j];
            if (j < n - 1) cout << " ";
        }
        cout << endl;
    }
    
    return 0;
}

maa的游戏

  1. 时间复杂度:

    O(nloga)O (n log a)

  2. 正确思路:

    • 对于固定的终点 ii,随着起点 l 的减小,区间的 gcdgcd 是非递增的。这就说明着 gcdgcd 值只会保持不变或减小,不会变大。

    • 对于每个终点 ii,我们维护一个列表,记录所有可能的 gcdgcd 值和其对应的起点的范围。

    • 对于每个终点 ii 和其对应的 gcdgcd 列表,我们计算每个 gcdgcd 值对应的有效起点范围,并根据条件计算这些区间的长度之和。

  3. 正确代码:

C++
#include <bits/stdc++.h>
using namespace std;

const int N = 6e5 + 7;
int a[N];
long long ans = 0;

int gcd(int x, int y) {
    while (y) {
        x %= y;
        swap(x, y);
    }
    return x;
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

    int n;
    cin >> n;
    for (int i = 1; i <= n; ++i) {
        cin >> a[i];
    }

    vector<tup<int, int, int>> pre;

    for (int i = 1; i <= n; ++i) {
        vector<tup<int, int, int>> curr;

        curr.emplace_back(a[i], i, i);

        for (auto p : pre) {
            int g = get<0>(p);
            int s = get<1>(p);
            int e = get<2>(p);
            int new_g = gcd(g, a[i]);
            if (new_g == get<0>(curr.back())) {
                get<1>(curr.back()) = s;
            } else {
                curr.emplace_back(new_g, s, i); //在尾部构造元素
            }
        }

        int m = curr.size();
        for (int j = 0; j < m; ++j) {
            int g = get<0>(curr[j]);
            int s = get<1>(curr[j]);
            int e = get<2>(curr[j]);
            int l_start = s;
            int l_end;

            if (j == 0) {
                l_end = e; 
            } else {
                l_end = get<1>(curr[j-1]) - 1;
            }

            if (l_start > l_end) {
                continue; 
            }

            int t;
            if (g == 3) {
                t = i % 2;
            } else if (g == 2) {
                t = (i - 1) % 2;
            } else {
                continue;
            }

            int a1, an;
            if (l_start % 2 == t) {
                a1 = l_start;
            } else {
                a1 = l_start + 1;
                if (a1 > l_end) {
                    continue;
                }
            }

            if (l_end % 2 == t) {
                an = l_end;
            } else {
                an = l_end - 1;
                if (an < l_start) {
                    continue;
                }
            }

            int count = (an - a1) / 2 + 1;
            long long sum_l = (long long)(a1 + an) * count / 2;
            long long sum_len = (long long)(i + 1) * count - sum_l;

            ans += sum_len;
        }

        pre = move(curr);
    }

    cout << ans << '\n';

    return 0;
}
原文